packwerk 1.1.0 → 1.1.1

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: 91ec92f15d9d87fa8c54861afd424fe6149ef8f8e05f114b6ee04559fa6053fc
4
- data.tar.gz: b83cc17ff9cd7d2d37db4456017d429b4a8f8e45766cdfa4551ace2e32c58183
3
+ metadata.gz: da27cf2b0d19f98831fc32b685ab58ab5a1abeacad532eff698787ef0dd8892d
4
+ data.tar.gz: 2b7d574417307d4b2d4aa0ae8e85665e5a701bc304307abd0302d02ebb4b6060
5
5
  SHA512:
6
- metadata.gz: 596f4f1a6b026e96ab6299e9ba7ceb63b7d709bd49c0629390f50e62e751174e4a92557243bef3a8f7f36aaf416e54fbf1fdb9f368fc2e478eb033e208358dc6
7
- data.tar.gz: fdca4107315ea6140d7b92e5cd7b53216a73f65fac4c1c3147c798a1d407f75d3898b31517bec6bf2452e7ca0a3d1ad87599142250cf75684ccc9a48436bcd91
6
+ metadata.gz: 730635a8851f68e9b43b25454f56bfe7296a42e76da335ab4b14b5fb2562a09c758107b9b25ab8d8455bd722e28079023568d84a6621d30b77b5d709c4dd5bd9
7
+ data.tar.gz: 97e9941ee9756c33dc952dca17ca6fe255ad8e2605584f3715616ff8e4a2a1caf380a079c0954435fff54ff83a7a92000176d9d6aa30858ef67f263958d2d105
@@ -85,7 +85,7 @@ GIT
85
85
  PATH
86
86
  remote: .
87
87
  specs:
88
- packwerk (1.1.0)
88
+ packwerk (1.1.1)
89
89
  activesupport (>= 5.2)
90
90
  ast
91
91
  better_html
@@ -3,9 +3,11 @@
3
3
  require "benchmark"
4
4
  require "sorbet-runtime"
5
5
 
6
+ require "packwerk/application_load_paths"
6
7
  require "packwerk/application_validator"
7
8
  require "packwerk/configuration"
8
9
  require "packwerk/files_for_processing"
10
+ require "packwerk/formatters/offenses_formatter"
9
11
  require "packwerk/formatters/progress_formatter"
10
12
  require "packwerk/inflector"
11
13
  require "packwerk/output_styles"
@@ -13,6 +15,7 @@ require "packwerk/run_context"
13
15
  require "packwerk/updating_deprecated_references"
14
16
  require "packwerk/checking_deprecated_references"
15
17
  require "packwerk/commands/detect_stale_violations_command"
18
+ require "packwerk/commands/update_deprecations_command"
16
19
  require "packwerk/commands/offense_progress_marker"
17
20
 
18
21
  module Packwerk
@@ -101,7 +104,7 @@ module Packwerk
101
104
 
102
105
  def generate_configs
103
106
  configuration_file = Packwerk::Generators::ConfigurationFile.generate(
104
- load_paths: @configuration.load_paths,
107
+ load_paths: Packwerk::ApplicationLoadPaths.extract_relevant_paths,
105
108
  root: @configuration.root_path,
106
109
  out: @out
107
110
  )
@@ -134,33 +137,16 @@ module Packwerk
134
137
  end
135
138
 
136
139
  def update_deprecations(paths)
137
- updating_deprecated_references = ::Packwerk::UpdatingDeprecatedReferences.new(@configuration.root_path)
138
- @run_context = Packwerk::RunContext.from_configuration(
139
- @configuration,
140
- reference_lister: updating_deprecated_references
140
+ update_deprecations = Commands::UpdateDeprecationsCommand.new(
141
+ files: fetch_files_to_process(paths),
142
+ configuration: @configuration,
143
+ offenses_formatter: offenses_formatter,
144
+ progress_formatter: @progress_formatter
141
145
  )
142
-
143
- files = fetch_files_to_process(paths)
144
-
145
- @progress_formatter.started(files)
146
-
147
- all_offenses = T.let([], T.untyped)
148
- execution_time = Benchmark.realtime do
149
- all_offenses = files.flat_map do |path|
150
- @run_context.process_file(file: path).tap do |offenses|
151
- mark_progress(offenses: offenses, progress_formatter: @progress_formatter)
152
- end
153
- end
154
-
155
- updating_deprecated_references.dump_deprecated_references_files
156
- end
157
-
158
- @out.puts # put a new line after the progress dots
159
- show_offenses(all_offenses)
160
- @progress_formatter.finished(execution_time)
161
- @out.puts("✅ `deprecated_references.yml` has been updated.")
162
-
163
- all_offenses.empty?
146
+ result = update_deprecations.run
147
+ @out.puts
148
+ @out.puts(result.message)
149
+ result.status
164
150
  end
165
151
 
166
152
  def check(paths)
@@ -181,15 +167,15 @@ module Packwerk
181
167
  @out.puts("Manually interrupted. Violations caught so far are listed below:")
182
168
  end
183
169
 
184
- @out.puts # put a new line after the progress dots
185
- show_offenses(all_offenses)
186
170
  @progress_formatter.finished(execution_time)
171
+ @out.puts
172
+ @out.puts(offenses_formatter.show_offenses(all_offenses))
187
173
 
188
174
  all_offenses.empty?
189
175
  end
190
176
 
191
177
  def detect_stale_violations(paths)
192
- detect_stale_violations = DetectStaleViolationsCommand.new(
178
+ detect_stale_violations = Commands::DetectStaleViolationsCommand.new(
193
179
  files: fetch_files_to_process(paths),
194
180
  configuration: @configuration,
195
181
  progress_formatter: @progress_formatter
@@ -225,19 +211,6 @@ module Packwerk
225
211
  end
226
212
  end
227
213
 
228
- def show_offenses(offenses)
229
- if offenses.empty?
230
- @out.puts("No offenses detected 🎉")
231
- else
232
- offenses.each do |offense|
233
- @out.puts(offense.to_s(@style))
234
- end
235
-
236
- offenses_string = Inflector.default.pluralize("offense", offenses.length)
237
- @out.puts("#{offenses.length} #{offenses_string} detected")
238
- end
239
- end
240
-
241
214
  def list_validation_errors(result)
242
215
  @out.puts
243
216
  if result.ok?
@@ -256,5 +229,9 @@ module Packwerk
256
229
  false
257
230
  end
258
231
  end
232
+
233
+ def offenses_formatter
234
+ @offenses_formatter ||= Formatters::OffensesFormatter.new(style: @style)
235
+ end
259
236
  end
260
237
  end
@@ -1,63 +1,60 @@
1
1
  # typed: true
2
2
  # frozen_string_literal: true
3
- require "packwerk/cli"
4
3
  require "sorbet-runtime"
5
4
  require "benchmark"
6
- require "packwerk/configuration"
7
- require "packwerk/formatters/progress_formatter"
8
5
  require "packwerk/run_context"
9
6
  require "packwerk/detect_stale_deprecated_references"
10
7
  require "packwerk/commands/offense_progress_marker"
8
+ require "packwerk/commands/result"
11
9
 
12
10
  module Packwerk
13
- class DetectStaleViolationsCommand
14
- extend T::Sig
15
- include OffenseProgressMarker
16
- Result = Struct.new(:message, :status)
17
-
18
- def initialize(files:, configuration:, run_context: nil, progress_formatter: nil, reference_lister: nil)
19
- @configuration = configuration
20
- @run_context = run_context
21
- @reference_lister = reference_lister
22
- @progress_formatter = progress_formatter
23
- @files = files
24
- end
11
+ module Commands
12
+ class DetectStaleViolationsCommand
13
+ extend T::Sig
14
+ include OffenseProgressMarker
15
+ def initialize(files:, configuration:, run_context: nil, progress_formatter: nil, reference_lister: nil)
16
+ @configuration = configuration
17
+ @run_context = run_context
18
+ @reference_lister = reference_lister
19
+ @progress_formatter = progress_formatter
20
+ @files = files
21
+ end
25
22
 
26
- sig { returns(Result) }
27
- def run
28
- @progress_formatter.started(@files)
23
+ sig { returns(Result) }
24
+ def run
25
+ @progress_formatter.started(@files)
29
26
 
30
- all_offenses = T.let([], T.untyped)
31
- execution_time = Benchmark.realtime do
32
- all_offenses = @files.flat_map do |path|
33
- run_context.process_file(file: path).tap do |offenses|
34
- mark_progress(offenses: offenses, progress_formatter: @progress_formatter)
27
+ execution_time = Benchmark.realtime do
28
+ @files.flat_map do |path|
29
+ run_context.process_file(file: path).tap do |offenses|
30
+ mark_progress(offenses: offenses, progress_formatter: @progress_formatter)
31
+ end
35
32
  end
36
33
  end
37
- end
38
34
 
39
- @progress_formatter.finished(execution_time)
40
- calculate_result
41
- end
35
+ @progress_formatter.finished(execution_time)
36
+ calculate_result
37
+ end
42
38
 
43
- private
39
+ private
44
40
 
45
- def run_context
46
- @run_context ||= Packwerk::RunContext.from_configuration(@configuration, reference_lister: reference_lister)
47
- end
41
+ def run_context
42
+ @run_context ||= Packwerk::RunContext.from_configuration(@configuration, reference_lister: reference_lister)
43
+ end
48
44
 
49
- def reference_lister
50
- @reference_lister ||= ::Packwerk::DetectStaleDeprecatedReferences.new(@configuration.root_path)
51
- end
45
+ def reference_lister
46
+ @reference_lister ||= ::Packwerk::DetectStaleDeprecatedReferences.new(@configuration.root_path)
47
+ end
52
48
 
53
- sig { returns Result }
54
- def calculate_result
55
- result_status = !reference_lister.stale_violations?
56
- message = "There were stale violations found, please run `packwerk update`"
57
- if result_status
58
- message = "No stale violations detected"
49
+ sig { returns(Result) }
50
+ def calculate_result
51
+ result_status = !reference_lister.stale_violations?
52
+ message = "There were stale violations found, please run `packwerk update`"
53
+ if result_status
54
+ message = "No stale violations detected"
55
+ end
56
+ Result.new(message: message, status: result_status)
59
57
  end
60
- Result.new(message, result_status)
61
58
  end
62
59
  end
63
60
  end
@@ -0,0 +1,13 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "sorbet-runtime"
5
+
6
+ module Packwerk
7
+ module Commands
8
+ class Result < T::Struct
9
+ prop :message, String
10
+ prop :status, T::Boolean
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,81 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "sorbet-runtime"
5
+ require "benchmark"
6
+
7
+ require "packwerk/commands/offense_progress_marker"
8
+ require "packwerk/commands/result"
9
+ require "packwerk/run_context"
10
+ require "packwerk/updating_deprecated_references"
11
+
12
+ module Packwerk
13
+ module Commands
14
+ class UpdateDeprecationsCommand
15
+ extend T::Sig
16
+ include OffenseProgressMarker
17
+
18
+ sig do
19
+ params(
20
+ files: T::Enumerable[String],
21
+ configuration: Configuration,
22
+ offenses_formatter: Formatters::OffensesFormatter,
23
+ progress_formatter: Formatters::ProgressFormatter
24
+ ).void
25
+ end
26
+ def initialize(files:, configuration:, offenses_formatter:, progress_formatter:)
27
+ @files = files
28
+ @configuration = configuration
29
+ @progress_formatter = progress_formatter
30
+ @offenses_formatter = offenses_formatter
31
+ @updating_deprecated_references = T.let(nil, T.nilable(UpdatingDeprecatedReferences))
32
+ @run_context = T.let(nil, T.nilable(RunContext))
33
+ end
34
+
35
+ sig { returns(Result) }
36
+ def run
37
+ @progress_formatter.started(@files)
38
+
39
+ all_offenses = T.let([], T.untyped)
40
+ execution_time = Benchmark.realtime do
41
+ all_offenses = @files.flat_map do |path|
42
+ run_context.process_file(file: path).tap do |offenses|
43
+ mark_progress(offenses: offenses, progress_formatter: @progress_formatter)
44
+ end
45
+ end
46
+
47
+ updating_deprecated_references.dump_deprecated_references_files
48
+ end
49
+
50
+ @progress_formatter.finished(execution_time)
51
+ calculate_result(all_offenses)
52
+ end
53
+
54
+ private
55
+
56
+ sig { returns(RunContext) }
57
+ def run_context
58
+ @run_context ||= RunContext.from_configuration(
59
+ @configuration,
60
+ reference_lister: updating_deprecated_references
61
+ )
62
+ end
63
+
64
+ sig { returns(UpdatingDeprecatedReferences) }
65
+ def updating_deprecated_references
66
+ @updating_deprecated_references ||= UpdatingDeprecatedReferences.new(@configuration.root_path)
67
+ end
68
+
69
+ sig { params(all_offenses: T::Array[T.nilable(::Packwerk::Offense)]).returns(Result) }
70
+ def calculate_result(all_offenses)
71
+ result_status = all_offenses.empty?
72
+ message = <<~EOS
73
+ #{@offenses_formatter.show_offenses(all_offenses)}
74
+ ✅ `deprecated_references.yml` has been updated.
75
+ EOS
76
+
77
+ Result.new(message: message, status: result_status)
78
+ end
79
+ end
80
+ end
81
+ end
@@ -22,7 +22,10 @@ module Packwerk
22
22
  private
23
23
 
24
24
  def from_packwerk_config(path)
25
- new(YAML.load_file(path), config_path: path)
25
+ new(
26
+ YAML.load_file(path) || {},
27
+ config_path: path
28
+ )
26
29
  end
27
30
  end
28
31
 
@@ -42,7 +45,7 @@ module Packwerk
42
45
  @root_path = File.expand_path(root)
43
46
  @package_paths = configs["package_paths"] || "**/"
44
47
  @custom_associations = configs["custom_associations"] || []
45
- @load_paths = configs["load_paths"]
48
+ @load_paths = configs["load_paths"] || []
46
49
  @inflections_file = File.expand_path(configs["inflections_file"] || "config/inflections.yml", @root_path)
47
50
 
48
51
  @config_path = config_path
@@ -0,0 +1,49 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "benchmark"
5
+ require "sorbet-runtime"
6
+
7
+ require "packwerk/inflector"
8
+ require "packwerk/output_styles"
9
+
10
+ module Packwerk
11
+ module Formatters
12
+ class OffensesFormatter
13
+ extend T::Sig
14
+
15
+ sig do
16
+ params(style: T.any(T.class_of(OutputStyles::Plain), T.class_of(OutputStyles::Coloured))).void
17
+ end
18
+ def initialize(style: OutputStyles::Plain)
19
+ @style = style
20
+ end
21
+
22
+ sig { params(offenses: T::Array[T.nilable(Offense)]).returns(String) }
23
+ def show_offenses(offenses)
24
+ return "No offenses detected 🎉" if offenses.empty?
25
+
26
+ <<~EOS
27
+ #{offenses_list(offenses)}
28
+ #{offenses_summary(offenses)}
29
+ EOS
30
+ end
31
+
32
+ private
33
+
34
+ sig { params(offenses: T::Array[T.nilable(Offense)]).returns(String) }
35
+ def offenses_list(offenses)
36
+ offenses
37
+ .compact
38
+ .map { |offense| offense.to_s(@style) }
39
+ .join("\n")
40
+ end
41
+
42
+ sig { params(offenses: T::Array[T.nilable(Offense)]).returns(String) }
43
+ def offenses_summary(offenses)
44
+ offenses_string = Inflector.default.pluralize("offense", offenses.length)
45
+ "#{offenses.length} #{offenses_string} detected"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -27,7 +27,7 @@ module Packwerk
27
27
  case type_of(constant_node)
28
28
  when CONSTANT_ROOT_NAMESPACE
29
29
  ""
30
- when CONSTANT, CONSTANT_ASSIGNMENT
30
+ when CONSTANT, CONSTANT_ASSIGNMENT, SELF
31
31
  # (const nil :Foo)
32
32
  # "Foo"
33
33
  # (const (cbase) :Foo)
@@ -40,6 +40,8 @@ module Packwerk
40
40
  # "::Foo = 1"
41
41
  # (casgn (lvar :a) :Foo (int 1))
42
42
  # "a::Foo = 1"
43
+ # (casgn (self) :Foo (int 1))
44
+ # "self::Foo = 1"
43
45
  namespace, name = constant_node.children
44
46
  if namespace
45
47
  [constant_name(namespace), name].join("::")
@@ -204,12 +206,13 @@ module Packwerk
204
206
  HASH_PAIR = :pair
205
207
  METHOD_CALL = :send
206
208
  MODULE = :module
209
+ SELF = :self
207
210
  STRING = :str
208
211
  SYMBOL = :sym
209
212
 
210
213
  private_constant(
211
214
  :BLOCK, :CLASS, :CONSTANT, :CONSTANT_ASSIGNMENT, :CONSTANT_ROOT_NAMESPACE, :HASH, :HASH_PAIR, :METHOD_CALL,
212
- :MODULE, :STRING, :SYMBOL,
215
+ :MODULE, :SELF, :STRING, :SYMBOL,
213
216
  )
214
217
 
215
218
  def type_of(node)
@@ -28,8 +28,8 @@ module Packwerk
28
28
 
29
29
  fully_qualified_constant_name = "::#{constant_name}"
30
30
 
31
- possible_namespaces = namespace_path.reduce([""]) do |acc, current|
32
- acc << acc.last + "::" + current
31
+ possible_namespaces = namespace_path.each_with_object([""]) do |current, acc|
32
+ acc << "#{acc.last}::#{current}" if acc.last && current
33
33
  end
34
34
 
35
35
  possible_namespaces.map { |namespace| namespace + fully_qualified_constant_name }
@@ -31,7 +31,7 @@ module Packwerk
31
31
  end
32
32
 
33
33
  def erb_parser_class
34
- @erb_parser_class || Erb
34
+ @erb_parser_class ||= Erb
35
35
  end
36
36
 
37
37
  def erb_parser_class=(klass)
@@ -1,4 +1,4 @@
1
- # typed: ignore
1
+ # typed: false
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "packwerk/application_validator"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- # typed: ignore
2
+ # typed: false
3
3
 
4
4
  require "spring/commands"
5
5
 
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Packwerk
5
- VERSION = "1.1.0"
5
+ VERSION = "1.1.1"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: packwerk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-24 00:00:00.000000000 Z
11
+ date: 2020-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -192,6 +192,8 @@ files:
192
192
  - lib/packwerk/cli.rb
193
193
  - lib/packwerk/commands/detect_stale_violations_command.rb
194
194
  - lib/packwerk/commands/offense_progress_marker.rb
195
+ - lib/packwerk/commands/result.rb
196
+ - lib/packwerk/commands/update_deprecations_command.rb
195
197
  - lib/packwerk/configuration.rb
196
198
  - lib/packwerk/const_node_inspector.rb
197
199
  - lib/packwerk/constant_discovery.rb
@@ -201,6 +203,7 @@ files:
201
203
  - lib/packwerk/detect_stale_deprecated_references.rb
202
204
  - lib/packwerk/file_processor.rb
203
205
  - lib/packwerk/files_for_processing.rb
206
+ - lib/packwerk/formatters/offenses_formatter.rb
204
207
  - lib/packwerk/formatters/progress_formatter.rb
205
208
  - lib/packwerk/generators/application_validation.rb
206
209
  - lib/packwerk/generators/configuration_file.rb