use_packwerk 0.65.0 → 0.67.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: 3044a336515f7443c6cfcf3814d066d8b2cfdf812de19214cfacddc49dbfcb8d
4
+ data.tar.gz: c249aea974522df11a037f8b1e46b956d27d2f06ad2dfab9d8076e5399031a15
5
5
  SHA512:
6
- metadata.gz: e39623119c6f9f1507ebb1a1f7ef7a5eb979ce10af57ee86ac517d74a46f0f96560b333688948a88cac0ac0fcedd43711fdd07f23490de20ea324da924b3a786
7
- data.tar.gz: aa492df65946f5d872a28c2328d609a4873c0c6c0183dd0d730de657ca42215d1a7be78e298ac8ca040bbef5432e8d5ce16dc8631364df153dbf8006178dc569
6
+ metadata.gz: fb2a5a22e390444fcb14f9ef039dfaaa2678c018cbe478794c8fba1ec40ccbcb713ab996611bbbdb6ed2f13a39c773e5a3d080ff1d35276ba0b0e24bd755c516
7
+ data.tar.gz: 806f18afe7e10d280dcd179539401a12dc3078d79b60d44731c3bb608e639f1a10986e077dcaec578b5a6f2d706de746d39e495ff075f03cf1442f141ec48ece
@@ -77,5 +77,18 @@ module UsePackwerk
77
77
  per_file_processors: [UsePackwerk::RubocopPostProcessor.new, UsePackwerk::CodeOwnershipPostProcessor.new]
78
78
  )
79
79
  end
80
+
81
+ desc 'lint_deprecated_references_yml_files', 'Ensures `deprecated_references.yml` files are up to date'
82
+ sig { void }
83
+ def lint_deprecated_references_yml_files
84
+ UsePackwerk.lint_deprecated_references_yml_files!
85
+ end
86
+
87
+ desc 'lint_package_yml_files [ packs/my_pack packs/my_other_pack ]', 'Lint `package.yml` files'
88
+ sig { params(pack_names: String).void }
89
+ def lint_package_yml_files(*pack_names)
90
+ packages = pack_names.empty? ? ParsePackwerk.all : pack_names.map { |p| ParsePackwerk.find(p.gsub(%r{/$}, '')) }.compact
91
+ UsePackwerk.lint_package_yml_files!(packages)
92
+ end
80
93
  end
81
94
  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) }
@@ -12,16 +12,7 @@ module UsePackwerk
12
12
  sig { override.params(prompt: TTY::Prompt).void }
13
13
  def perform!(prompt)
14
14
  packs = PackSelector.single_or_all_pack_multi_select(prompt, question_text: 'Please select the packs you want to lint package.yml files for')
15
- packs.each do |p|
16
- new_package = ParsePackwerk::Package.new(
17
- name: p.name,
18
- enforce_privacy: p.enforce_privacy,
19
- enforce_dependencies: p.enforce_dependencies,
20
- dependencies: p.dependencies.uniq.sort,
21
- metadata: p.metadata
22
- )
23
- ParsePackwerk.write_package_yml!(new_package)
24
- end
15
+ UsePackwerk.lint_package_yml_files!(packs)
25
16
  end
26
17
 
27
18
  sig { override.returns(String) }
@@ -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,54 @@ 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_yml_files!
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
284
+
285
+ sig { params(packs: T::Array[ParsePackwerk::Package]).void }
286
+ def self.lint_package_yml_files!(packs)
287
+ packs.each do |p|
288
+ new_package = ParsePackwerk::Package.new(
289
+ name: p.name,
290
+ enforce_privacy: p.enforce_privacy,
291
+ enforce_dependencies: p.enforce_dependencies,
292
+ dependencies: p.dependencies.uniq.sort,
293
+ metadata: p.metadata
294
+ )
295
+ ParsePackwerk.write_package_yml!(new_package)
296
+ end
297
+ end
248
298
  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.67.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