use_packwerk 0.65.0 → 0.66.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '09fba875e35dbc9bec54523ce0d6b778624029daa850c4daf2320dc753064205'
4
- data.tar.gz: f56b46f76d3111e04f1a09f642843fd48be5a656c336366ecfe2db796f26f0ff
3
+ metadata.gz: 2c36d9aa08d6196fcfc0a96e3846d0a44f8637f5dc800ac6a0778b7deac637df
4
+ data.tar.gz: 07b0910be565a9df52b31c63982ad8e8b75808b80de5b77ad78120293a48c890
5
5
  SHA512:
6
- metadata.gz: e39623119c6f9f1507ebb1a1f7ef7a5eb979ce10af57ee86ac517d74a46f0f96560b333688948a88cac0ac0fcedd43711fdd07f23490de20ea324da924b3a786
7
- data.tar.gz: aa492df65946f5d872a28c2328d609a4873c0c6c0183dd0d730de657ca42215d1a7be78e298ac8ca040bbef5432e8d5ce16dc8631364df153dbf8006178dc569
6
+ metadata.gz: 46635409558c2c7c0557fb6164a979988558c4d47d0b2467dd97728bd67a6b0c6b77c4614e2c25b49ac075b74de392d7b062b435e0b81b1f60cf2c0c4a044f05
7
+ data.tar.gz: 6ebd403c29e4a432d43e7ace961e68c84192e238f0ad3172ad6af8fc1800c6900684cf5e010eb2bd679870258e3ea7b8ddd0b6d9a87612d03c9809db303ccd39
@@ -77,5 +77,11 @@ module UsePackwerk
77
77
  per_file_processors: [UsePackwerk::RubocopPostProcessor.new, UsePackwerk::CodeOwnershipPostProcessor.new]
78
78
  )
79
79
  end
80
+
81
+ desc 'lint_deprecated_references', 'Ensures `deprecated_references.yml` files are up to date'
82
+ sig { void }
83
+ def lint_deprecated_references
84
+ UsePackwerk.lint_deprecated_references!
85
+ end
80
86
  end
81
87
  end
@@ -13,10 +13,18 @@ module UsePackwerk
13
13
  sig { returns(UserEventLogger) }
14
14
  attr_accessor :user_event_logger
15
15
 
16
+ OnDeprecatedReferencesLintFailure = T.type_alias do
17
+ T.proc.params(output: String).void
18
+ end
19
+
20
+ sig { returns(OnDeprecatedReferencesLintFailure) }
21
+ attr_accessor :on_deprecated_references_lint_failure
22
+
16
23
  sig { void }
17
24
  def initialize
18
25
  @enforce_dependencies = T.let(default_enforce_dependencies, T::Boolean)
19
26
  @user_event_logger = T.let(DefaultUserEventLogger.new, UserEventLogger)
27
+ @on_deprecated_references_lint_failure = T.let(->(output) {}, OnDeprecatedReferencesLintFailure)
20
28
  end
21
29
 
22
30
  sig { returns(T::Boolean) }
@@ -389,6 +389,52 @@ module UsePackwerk
389
389
  # otherwise `PackageProtections.config` will attempt to reload the client configuratoin.
390
390
  @loaded_client_configuration = false
391
391
  end
392
+
393
+ sig { returns(T::Hash[String, String]) }
394
+ def self.get_deprecated_references_contents
395
+ deprecated_references = {}
396
+ ParsePackwerk.all.each do |package|
397
+ deprecated_references_yml = ParsePackwerk::DeprecatedReferences.for(package).pathname
398
+ if deprecated_references_yml.exist?
399
+ deprecated_references[deprecated_references_yml.to_s] = deprecated_references_yml.read
400
+ end
401
+ end
402
+
403
+ deprecated_references
404
+ end
405
+
406
+ DeprecatedReferencesFiles = T.type_alias do
407
+ T::Hash[String, T.nilable(String)]
408
+ end
409
+
410
+ sig { params(before: DeprecatedReferencesFiles, after: DeprecatedReferencesFiles).returns(String) }
411
+ def self.diff_deprecated_references_yml(before, after)
412
+ dir_containing_contents_before = Dir.mktmpdir
413
+ dir_containing_contents_after = Dir.mktmpdir
414
+ begin
415
+ write_deprecated_references_to_tmp_folder(before, dir_containing_contents_before)
416
+ write_deprecated_references_to_tmp_folder(after, dir_containing_contents_after)
417
+
418
+ diff = `diff -r #{dir_containing_contents_before}/ #{dir_containing_contents_after}/`
419
+ # For ease of reading, sub out the tmp directory from the diff
420
+ diff.gsub(dir_containing_contents_before, '').gsub(dir_containing_contents_after, '')
421
+ ensure
422
+ FileUtils.remove_entry dir_containing_contents_before
423
+ FileUtils.remove_entry dir_containing_contents_after
424
+ end
425
+ end
426
+
427
+ sig { params(deprecated_references_files: DeprecatedReferencesFiles, tmp_folder: String).void }
428
+ def self.write_deprecated_references_to_tmp_folder(deprecated_references_files, tmp_folder)
429
+ deprecated_references_files.each do |filename, contents|
430
+ next if contents.nil?
431
+
432
+ tmp_folder_pathname = Pathname.new(tmp_folder)
433
+ temp_deprecated_references_yml = tmp_folder_pathname.join(filename)
434
+ FileUtils.mkdir_p(temp_deprecated_references_yml.dirname)
435
+ temp_deprecated_references_yml.write(contents)
436
+ end
437
+ end
392
438
  end
393
439
 
394
440
  private_constant :Private
data/lib/use_packwerk.rb CHANGED
@@ -245,4 +245,40 @@ module UsePackwerk
245
245
  Private::PackwerkWrapper.packwerk_cli_execute_safely(argv, formatter)
246
246
  formatter.aggregated_offenses.compact
247
247
  end
248
+
249
+ sig { void }
250
+ def self.lint_deprecated_references!
251
+ contents_before = Private.get_deprecated_references_contents
252
+ UsePackwerk.execute(['update-deprecations'])
253
+ contents_after = Private.get_deprecated_references_contents
254
+ diff = Private.diff_deprecated_references_yml(contents_before, contents_after)
255
+
256
+ if diff == ''
257
+ # No diff generated by `update-deprecations`
258
+ exit 0
259
+ else
260
+ output = <<~OUTPUT
261
+ All `deprecated_references.yml` files must be up-to-date and that no diff is generated when running `bin/packwerk update-deprecations`.
262
+ This helps ensure a high quality signal in other engineers' PRs when inspecting new violations by ensuring there are no unrelated changes.
263
+
264
+ There are three main reasons there may be a diff:
265
+ 1) Most likely, you may have stale violations, meaning there are old violations that no longer apply.
266
+ 2) You may have some sort of auto-formatter set up somewhere (e.g. something that reformats YML files) that is, for example, changing double quotes to single quotes. Ensure this is turned off for these auto-generated files.
267
+ 3) You may have edited these files manually. It's recommended to use the `bin/packwerk update-deprecations` command to make changes to `deprecated_references.yml` files.
268
+
269
+ In all cases, you can run `bin/packwerk update-deprecations` to update these files.
270
+
271
+ Here is the diff generated after running `update-deprecations`:
272
+ ```
273
+ #{diff}
274
+ ```
275
+
276
+ OUTPUT
277
+
278
+ puts output
279
+ UsePackwerk.config.on_deprecated_references_lint_failure.call(output)
280
+
281
+ exit 1
282
+ end
283
+ end
248
284
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: use_packwerk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.65.0
4
+ version: 0.66.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gusto Engineers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-16 00:00:00.000000000 Z
11
+ date: 2022-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: code_ownership