archunit 0.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/API.md +498 -0
  3. data/CHANGELOG.md +85 -0
  4. data/CODE_OF_CONDUCT.md +40 -0
  5. data/CONTRIBUTING.md +75 -0
  6. data/GUIDE.md +804 -0
  7. data/README.md +224 -289
  8. data/SECURITY.md +64 -0
  9. data/SUPPORT.md +23 -0
  10. data/assets/logo-rounded.png +0 -0
  11. data/exe/archunit +7 -0
  12. data/lib/archunit/cli/analysis_report.rb +62 -0
  13. data/lib/archunit/cli/analysis_text.rb +25 -0
  14. data/lib/archunit/cli/baseline_context.rb +42 -0
  15. data/lib/archunit/cli/baseline_document.rb +58 -0
  16. data/lib/archunit/cli/baseline_fingerprint.rb +36 -0
  17. data/lib/archunit/cli/baseline_identity.rb +85 -0
  18. data/lib/archunit/cli/baseline_ledger.rb +38 -0
  19. data/lib/archunit/cli/baseline_object.rb +15 -0
  20. data/lib/archunit/cli/baseline_options.rb +48 -0
  21. data/lib/archunit/cli/baseline_run.rb +85 -0
  22. data/lib/archunit/cli/baseline_schema.rb +53 -0
  23. data/lib/archunit/cli/baseline_text.rb +31 -0
  24. data/lib/archunit/cli/configuration.rb +93 -0
  25. data/lib/archunit/cli/options.rb +117 -0
  26. data/lib/archunit/cli/report.rb +121 -0
  27. data/lib/archunit/cli/runner.rb +108 -0
  28. data/lib/archunit/cli.rb +15 -0
  29. data/lib/archunit/common/extraction/dependency_evidence.rb +44 -0
  30. data/lib/archunit/common/extraction/edge.rb +7 -2
  31. data/lib/archunit/common/extraction/import_kind.rb +4 -1
  32. data/lib/archunit/common/extraction/parse_diagnostic.rb +23 -0
  33. data/lib/archunit/common/extraction/source_exclusions.rb +43 -0
  34. data/lib/archunit/common/fluentapi/check_options.rb +53 -2
  35. data/lib/archunit/common/fluentapi/checkable.rb +25 -1
  36. data/lib/archunit/common/logging/check_logger.rb +8 -0
  37. data/lib/archunit/common/logging/inspection.rb +59 -0
  38. data/lib/archunit/common/logging/logging_options.rb +5 -0
  39. data/lib/archunit/common/pattern.rb +18 -8
  40. data/lib/archunit/common/projection/cycles/johnson_cycles.rb +35 -19
  41. data/lib/archunit/common/projection/cycles/tarjan_scc.rb +30 -10
  42. data/lib/archunit/error/source_parse_error.rb +38 -0
  43. data/lib/archunit/extraction/analysis_observer.rb +24 -0
  44. data/lib/archunit/extraction/analysis_profile.rb +30 -0
  45. data/lib/archunit/extraction/analysis_snapshot.rb +51 -0
  46. data/lib/archunit/extraction/arch_ignore.rb +91 -0
  47. data/lib/archunit/extraction/conservative_local_flow.rb +100 -0
  48. data/lib/archunit/extraction/constant_index.rb +110 -0
  49. data/lib/archunit/extraction/constant_visitor.rb +137 -0
  50. data/lib/archunit/extraction/dependency_context.rb +30 -0
  51. data/lib/archunit/extraction/discover_gemspec_load_paths.rb +96 -0
  52. data/lib/archunit/extraction/enumerate_source_files.rb +36 -9
  53. data/lib/archunit/extraction/extract_dependencies.rb +74 -15
  54. data/lib/archunit/extraction/extract_graph.rb +74 -40
  55. data/lib/archunit/extraction/extract_imports.rb +57 -124
  56. data/lib/archunit/extraction/extraction_profile.rb +105 -0
  57. data/lib/archunit/extraction/graph_assembly.rb +32 -0
  58. data/lib/archunit/extraction/graph_cache_key.rb +27 -0
  59. data/lib/archunit/extraction/import_calls.rb +93 -0
  60. data/lib/archunit/extraction/import_resolver.rb +65 -0
  61. data/lib/archunit/extraction/import_source_policy.rb +123 -0
  62. data/lib/archunit/extraction/import_visitor.rb +89 -0
  63. data/lib/archunit/extraction/load_arguments.rb +34 -0
  64. data/lib/archunit/extraction/local_string_tracker.rb +81 -0
  65. data/lib/archunit/extraction/resolve_import.rb +5 -3
  66. data/lib/archunit/extraction/resolve_load_paths.rb +98 -0
  67. data/lib/archunit/extraction/resolved_import.rb +5 -2
  68. data/lib/archunit/extraction/source_analysis.rb +26 -0
  69. data/lib/archunit/extraction/static_import_path.rb +111 -0
  70. data/lib/archunit/extraction/validate_extraction_paths.rb +37 -0
  71. data/lib/archunit/files/extraction/extract_file_info.rb +14 -5
  72. data/lib/archunit/files/extraction/file_info.rb +1 -2
  73. data/lib/archunit/files/fluentapi/depend_on_external_module_condition.rb +2 -0
  74. data/lib/archunit/files/fluentapi/depend_on_external_module_condition_builder.rb +2 -0
  75. data/lib/archunit/files/fluentapi/depend_on_file_condition.rb +6 -0
  76. data/lib/archunit/files/fluentapi/depend_on_file_condition_builder.rb +6 -0
  77. data/lib/archunit/files/fluentapi/file_condition_builder.rb +12 -0
  78. data/lib/archunit/files/fluentapi/file_rule_support.rb +8 -1
  79. data/lib/archunit/files/fluentapi/files.rb +6 -0
  80. data/lib/archunit/files/fluentapi/match_pattern_file_condition_builder.rb +14 -0
  81. data/lib/archunit/files/fluentapi/positive_match_pattern_file_condition_builder.rb +2 -0
  82. data/lib/archunit/graph/fluentapi/graph.rb +5 -0
  83. data/lib/archunit/graph/fluentapi/project_graph_builder.rb +44 -0
  84. data/lib/archunit/graph/projection/aggregate_edges.rb +4 -2
  85. data/lib/archunit/graph/projection/graph_query_options.rb +1 -2
  86. data/lib/archunit/graph/projection/graph_report_edge.rb +12 -2
  87. data/lib/archunit/graph/rendering/json_renderer.rb +3 -1
  88. data/lib/archunit/layers/fluentapi/architecture_preset.rb +49 -0
  89. data/lib/archunit/layers/fluentapi/layer_definition_builder.rb +4 -0
  90. data/lib/archunit/layers/fluentapi/layer_dependency_rule_builder.rb +4 -0
  91. data/lib/archunit/layers/fluentapi/layered_architecture.rb +5 -0
  92. data/lib/archunit/layers/fluentapi/layers.rb +5 -0
  93. data/lib/archunit/layers/fluentapi/preset_definitions.rb +39 -0
  94. data/lib/archunit/layers/fluentapi/preset_options.rb +69 -0
  95. data/lib/archunit/layers/fluentapi/preset_rule.rb +45 -0
  96. data/lib/archunit/metrics/extraction/extract_distance_info.rb +2 -3
  97. data/lib/archunit/metrics/extraction/extract_project_info.rb +14 -10
  98. data/lib/archunit/metrics/extraction/metric_info.rb +2 -4
  99. data/lib/archunit/metrics/extraction/source_metrics_visitor.rb +2 -4
  100. data/lib/archunit/metrics/fluentapi/custom_metric_builder.rb +2 -0
  101. data/lib/archunit/metrics/fluentapi/custom_metric_condition.rb +4 -1
  102. data/lib/archunit/metrics/fluentapi/logged_metric.rb +23 -0
  103. data/lib/archunit/metrics/fluentapi/metric_predicate_condition.rb +4 -1
  104. data/lib/archunit/metrics/fluentapi/metric_report_builder.rb +6 -3
  105. data/lib/archunit/metrics/fluentapi/metric_selection.rb +18 -2
  106. data/lib/archunit/metrics/fluentapi/metric_threshold_condition.rb +2 -1
  107. data/lib/archunit/metrics/fluentapi/metrics.rb +5 -0
  108. data/lib/archunit/metrics/fluentapi/metrics_builder.rb +18 -3
  109. data/lib/archunit/metrics/reporting/metrics_export_options.rb +6 -2
  110. data/lib/archunit/slices/fluentapi/diagram_slice_condition.rb +1 -0
  111. data/lib/archunit/slices/fluentapi/forbidden_slice_dependency_condition.rb +1 -0
  112. data/lib/archunit/slices/fluentapi/negative_slice_condition_builder.rb +2 -0
  113. data/lib/archunit/slices/fluentapi/positive_slice_condition_builder.rb +8 -0
  114. data/lib/archunit/slices/fluentapi/slice_scope_builder.rb +12 -0
  115. data/lib/archunit/slices/fluentapi/slices.rb +5 -0
  116. data/lib/archunit/testing/assert_passes.rb +4 -0
  117. data/lib/archunit/testing/result_factory.rb +16 -4
  118. data/lib/archunit/testing/rspec_adapter.rb +1 -2
  119. data/lib/archunit/testing.rb +7 -3
  120. data/lib/archunit/version.rb +1 -1
  121. data/lib/archunit.rb +11 -0
  122. metadata +67 -9
@@ -4,29 +4,38 @@ require 'pathname'
4
4
  require_relative '../common/extraction/edge'
5
5
  require_relative '../common/extraction/graph'
6
6
  require_relative '../common/fluentapi/check_options'
7
+ require_relative '../common/logging/inspection'
7
8
  require_relative 'enumerate_source_files'
8
9
  require_relative 'extract_dependencies'
10
+ require_relative 'extraction_profile'
9
11
  require_relative 'locate_project'
12
+ require_relative 'resolve_load_paths'
13
+ require_relative 'graph_cache_key'
14
+ require_relative 'analysis_profile'
15
+ require_relative 'analysis_snapshot'
16
+ require_relative 'analysis_observer'
17
+ require_relative 'graph_assembly'
10
18
 
11
19
  module ArchUnit
12
20
  # Assembles the normalized dependency graph for a Ruby project.
13
21
  module Extraction
14
22
  GRAPH_CACHE_MUTEX = Mutex.new
15
- GRAPH_INDEPENDENT_CHECK_OPTIONS = %i[allow_empty_tests logging clear_cache].freeze
23
+ CachedGraph = Data.define(:graph, :analysis)
24
+ private_constant :CachedGraph
16
25
 
17
26
  module_function
18
27
 
19
28
  def extract_graph(
20
- locator = nil, exclude_patterns: nil, options: nil, working_directory: Dir.pwd
29
+ locator = nil, exclude_patterns: nil, options: nil, working_directory: Dir.pwd,
30
+ profile: nil
21
31
  )
32
+ ExtractionProfile.validate(profile)
22
33
  options = Common::FluentApi::CheckOptions.resolve(options)
23
- root = Pathname.new(locate_project(locator, working_directory:))
24
- patterns = resolve_exclude_patterns(exclude_patterns)
25
- cache_key = graph_cache_key(root, exclude_patterns: patterns, options:)
26
-
27
- GRAPH_CACHE_MUTEX.synchronize do
28
- graph_cache.delete(cache_key) if options.clear_cache?
29
- graph_cache[cache_key] ||= extract_graph_uncached(root, patterns)
34
+ ExtractionProfile.measure(profile, :total) do
35
+ root = ExtractionProfile.measure(profile, :project_discovery) do
36
+ Pathname.new(locate_project(locator, working_directory:))
37
+ end
38
+ resolved_graph(root, exclude_patterns, options, profile)
30
39
  end
31
40
  end
32
41
 
@@ -35,31 +44,69 @@ module ArchUnit
35
44
  nil
36
45
  end
37
46
 
38
- def extract_graph_uncached(root, exclude_patterns)
39
- source_files = enumerate_source_files(root, exclude_patterns:)
40
- edges = self_edges(source_files) + extract_dependencies_from(root, source_files)
47
+ def resolved_graph(root, exclude_patterns, options, profile)
48
+ patterns = resolve_project_exclude_patterns(
49
+ root, exclude_patterns, source_exclusions: options.source_exclusions
50
+ )
51
+ key = GraphCacheKey.build(root, patterns, options)
52
+ entry, cache_hit = fetch_graph(root, patterns, options, key, profile)
53
+ AnalysisObserver.record(entry.analysis, cache_hit:)
54
+ Common::Logging::Inspection.graph(entry.graph, root:)
55
+ end
56
+ private_class_method :resolved_graph
41
57
 
42
- Common::Extraction::Graph.new(merge_parallel_edges(edges))
58
+ def fetch_graph(root, patterns, options, cache_key, profile)
59
+ GRAPH_CACHE_MUTEX.synchronize do
60
+ cached_graph(root, patterns, options, cache_key, profile)
61
+ end
62
+ rescue StandardError
63
+ AnalysisObserver.record(AnalysisSnapshot.graph(root, patterns, options, status: 'error'))
64
+ raise
43
65
  end
44
- private_class_method :extract_graph_uncached
66
+ private_class_method :fetch_graph
67
+
68
+ def cached_graph(root, patterns, options, cache_key, profile)
69
+ graph_cache.delete(cache_key) if options.clear_cache?
70
+ if (cached = graph_cache[cache_key])
71
+ profile&.record_cache_hit
72
+ return [cached, true]
73
+ end
45
74
 
46
- def graph_cache_key(project_root, exclude_patterns:, options:)
47
- [
48
- project_root.to_s.tr('\\', '/').freeze,
49
- exclude_patterns.sort.freeze,
50
- graph_analysis_toggles(options)
51
- ].freeze
75
+ entry = build_graph_entry(root, patterns, options, profile)
76
+ graph_cache[cache_key] = entry
77
+ [entry, false]
52
78
  end
53
- private_class_method :graph_cache_key
79
+ private_class_method :cached_graph
54
80
 
55
- def graph_analysis_toggles(options)
56
- options.class.members.filter_map do |member|
57
- next if GRAPH_INDEPENDENT_CHECK_OPTIONS.include?(member)
81
+ def build_graph_entry(root, patterns, options, profile)
82
+ recorder = AnalysisProfile.new(profile)
83
+ graph = extract_graph_uncached(root, patterns, options, recorder)
84
+ analysis = AnalysisSnapshot.graph(root, patterns, options, counters: recorder.counters)
85
+ CachedGraph.new(graph:, analysis:)
86
+ end
87
+ private_class_method :build_graph_entry
58
88
 
59
- [member, options.public_send(member)].freeze
60
- end.freeze
89
+ def extract_graph_uncached(root, exclude_patterns, options, profile)
90
+ source_files = ExtractionProfile.measure(profile, :source_enumeration) do
91
+ enumerate_source_files_with_patterns(root, exclude_patterns)
92
+ end
93
+ profile&.increment(:source_files, source_files.length)
94
+ load_paths = ExtractionProfile.measure(profile, :load_path_discovery) do
95
+ LoadPaths.resolve(root, explicit_paths: options.load_paths, source_files:)
96
+ end
97
+ edges = extraction_edges(root, source_files, load_paths, options, profile)
98
+ GraphAssembly.build(edges, profile)
99
+ end
100
+ private_class_method :extract_graph_uncached
101
+
102
+ def extraction_edges(root, source_files, load_paths, options, profile)
103
+ edges = self_edges(source_files) + extract_dependencies_from(
104
+ root, source_files, load_paths:, profile:, options:
105
+ )
106
+ profile&.increment(:raw_edges, edges.length)
107
+ edges
61
108
  end
62
- private_class_method :graph_analysis_toggles
109
+ private_class_method :extraction_edges
63
110
 
64
111
  def graph_cache
65
112
  @graph_cache ||= {}
@@ -72,18 +119,5 @@ module ArchUnit
72
119
  end
73
120
  end
74
121
  private_class_method :self_edges
75
-
76
- def merge_parallel_edges(edges)
77
- edges.group_by { |edge| [edge.source, edge.target] }.values.map do |parallel_edges|
78
- first = parallel_edges.first
79
- Common::Extraction::Edge.new(
80
- source: first.source,
81
- target: first.target,
82
- external: first.external,
83
- import_kinds: parallel_edges.flat_map(&:import_kinds)
84
- )
85
- end
86
- end
87
- private_class_method :merge_parallel_edges
88
122
  end
89
123
  end
@@ -1,109 +1,77 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'pathname'
4
3
  require 'prism'
5
4
  require_relative '../error/technical_error'
5
+ require_relative '../error/source_parse_error'
6
6
  require_relative '../error/user_error'
7
- require_relative 'resolve_import'
7
+ require_relative 'extraction_profile'
8
+ require_relative 'import_resolver'
9
+ require_relative 'import_visitor'
8
10
  require_relative 'resolved_import'
11
+ require_relative 'validate_extraction_paths'
9
12
 
10
13
  module ArchUnit
11
14
  # Parses Ruby source without executing it and resolves literal imports.
12
15
  module Extraction
13
16
  IGNORE_DIRECTIVE = /\A#\s*archunit:\s*ignore(?:\s+(?<modules>.+?))?\s*\z/
14
17
 
15
- # Collects supported dependency calls from a Prism syntax tree.
16
- class ImportVisitor < Prism::Visitor
17
- CALL_KINDS = {
18
- require: Common::Extraction::ImportKind::REQUIRE,
19
- require_relative: Common::Extraction::ImportKind::REQUIRE_RELATIVE,
20
- autoload: Common::Extraction::ImportKind::AUTOLOAD,
21
- load: Common::Extraction::ImportKind::LOAD
22
- }.freeze
23
-
24
- attr_reader :imports
25
-
26
- def initialize
27
- super
28
- @imports = []
29
- end
30
-
31
- def visit_call_node(node)
32
- record_import(node)
33
- super
34
- end
35
-
36
- private
37
-
38
- def record_import(node)
39
- import_kind = CALL_KINDS[node.name]
40
- return unless import_kind && supported_receiver?(node, import_kind)
41
-
42
- arguments = node.arguments&.arguments || []
43
- argument = import_argument(arguments, import_kind)
44
- return unless argument.is_a?(Prism::StringNode)
45
- return unless valid_module_name?(argument.unescaped)
46
-
47
- @imports << located_import(argument, import_kind, node)
48
- end
49
-
50
- def located_import(argument, import_kind, node)
51
- [argument.unescaped, import_kind, node.location.start_line, node.location.end_line]
52
- end
53
-
54
- def valid_module_name?(module_name)
55
- !module_name.empty? && !module_name.include?("\0")
56
- end
57
-
58
- def supported_receiver?(node, import_kind)
59
- return true if node.receiver.nil?
60
- if import_kind == Common::Extraction::ImportKind::AUTOLOAD
61
- return constant_receiver?(node.receiver)
62
- end
63
-
64
- node.receiver.respond_to?(:full_name) &&
65
- node.receiver.full_name.delete_prefix('::') == 'Kernel'
66
- end
67
-
68
- def constant_receiver?(receiver)
69
- receiver.is_a?(Prism::ConstantReadNode) || receiver.is_a?(Prism::ConstantPathNode)
70
- end
71
-
72
- def import_argument(arguments, import_kind)
73
- if import_kind == Common::Extraction::ImportKind::AUTOLOAD
74
- return unless arguments.length == 2
75
-
76
- arguments[1]
77
- else
78
- return unless arguments.length == 1
79
-
80
- arguments[0]
81
- end
82
- end
83
- end
84
- private_constant :ImportVisitor
85
-
86
18
  module_function
87
19
 
88
- def extract_imports(source_file, project_root:)
89
- source_path = import_source_path(source_file)
90
- root = import_project_root(project_root)
91
- parsed_imports(source_path).map do |located_import|
92
- resolved_import(located_import, source_path, root)
93
- end.freeze
20
+ def extract_imports( # rubocop:disable Metrics/ParameterLists
21
+ source_file, project_root:, load_paths: nil, profile: nil, resolution_cache: nil,
22
+ rails_imports: false
23
+ )
24
+ source_path = ExtractionPaths.source_file(source_file)
25
+ root = ExtractionPaths.project_root(project_root)
26
+ extract_imports_from(
27
+ source_path, project_root: root, load_paths:, profile:, resolution_cache:, rails_imports:
28
+ )
94
29
  rescue UserError
95
30
  raise
96
31
  rescue SystemCallError => e
97
32
  raise TechnicalError, "could not parse Ruby source file: #{e.message}"
98
33
  end
99
34
 
100
- def parsed_imports(source_path)
101
- parse_result = Prism.parse_file(source_path.to_s)
102
- return [] unless parse_result.success?
35
+ # Share the parsed AST with constant analysis.
36
+ def extract_imports_from( # rubocop:disable Metrics/ParameterLists
37
+ source_path, project_root:, load_paths: nil, profile: nil, resolution_cache: nil,
38
+ parse_result: nil, rails_imports: false, helper_load_paths: nil
39
+ )
40
+ resolver = ImportResolver.new(
41
+ source_path:, project_root:, load_paths:, profile:,
42
+ cache: resolution_cache, helper_load_paths:
43
+ )
44
+ parse_result ||= parse_source(source_path, profile)
45
+ resolve_imports(parsed_imports(parse_result, profile, source_path:, rails_imports:), resolver)
46
+ rescue SystemCallError => e
47
+ raise TechnicalError, "could not parse Ruby source file: #{e.message}"
48
+ end
49
+ private_class_method :extract_imports_from
103
50
 
104
- visitor = ImportVisitor.new
105
- visitor.visit(parse_result.value)
106
- reject_ignored_imports(visitor.imports, parse_result.comments)
51
+ def resolve_imports(imports, resolver)
52
+ imports.map { |import| resolved_import(import, resolver) }.freeze
53
+ end
54
+ private_class_method :resolve_imports
55
+
56
+ def parse_source(source_path, profile)
57
+ source = ExtractionProfile.measure(profile, :file_read) { File.binread(source_path) }
58
+ result = ExtractionProfile.measure(profile, :prism_parse) do
59
+ Prism.parse(source, filepath: source_path.to_s)
60
+ end
61
+ profile&.increment(:malformed_files) unless result.success?
62
+ SourceParseError.validate!(result, path: source_path)
63
+ rescue SystemCallError => e
64
+ raise TechnicalError, "could not parse Ruby source file: #{e.message}"
65
+ end
66
+ private_class_method :parse_source
67
+
68
+ def parsed_imports(parse_result, profile, source_path:, rails_imports:)
69
+ ExtractionProfile.measure(profile, :import_extraction) do
70
+ visitor = ImportVisitor.new(source_path:, syntax_tree: parse_result.value, rails_imports:)
71
+ visitor.visit(parse_result.value)
72
+ profile&.increment(:unresolved_imports, visitor.unresolved_imports)
73
+ reject_ignored_imports(visitor.imports, parse_result.comments)
74
+ end
107
75
  end
108
76
  private_class_method :parsed_imports
109
77
 
@@ -147,51 +115,16 @@ module ArchUnit
147
115
  end
148
116
  private_class_method :ignored_module?
149
117
 
150
- def resolved_import(located_import, source_path, root)
151
- module_name, import_kind, line_number, = located_import
118
+ def resolved_import(located_import, resolver)
119
+ module_name, import_kind, line_number, end_line = located_import
152
120
  ResolvedImport.new(
153
121
  module_name: module_name,
154
122
  import_kind: import_kind,
155
123
  line_number: line_number,
156
- resolved_path: resolve_import_for(module_name, import_kind, source_path, root)
124
+ end_line: end_line,
125
+ resolved_path: resolver.resolve(module_name, import_kind)
157
126
  )
158
127
  end
159
128
  private_class_method :resolved_import
160
-
161
- def resolve_import_for(module_name, import_kind, source_path, root)
162
- resolve_import(
163
- module_name,
164
- source_file: source_path,
165
- project_root: root,
166
- import_kind: import_kind
167
- )
168
- end
169
- private_class_method :resolve_import_for
170
-
171
- def import_source_path(value)
172
- path = extraction_path(value, 'source_file')
173
- raise UserError, 'source_file must be an existing file' unless path.file?
174
-
175
- path
176
- end
177
- private_class_method :import_source_path
178
-
179
- def import_project_root(value)
180
- path = extraction_path(value, 'project_root')
181
- raise UserError, 'project_root must be an existing directory' unless path.directory?
182
-
183
- path.realpath
184
- end
185
- private_class_method :import_project_root
186
-
187
- def extraction_path(value, attribute)
188
- value = value.to_path if value.respond_to?(:to_path)
189
- unless value.is_a?(String) && !value.empty?
190
- raise UserError, "#{attribute} must be a non-empty path"
191
- end
192
-
193
- Pathname.new(value).expand_path
194
- end
195
- private_class_method :extraction_path
196
129
  end
197
130
  end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ArchUnit
4
+ module Extraction
5
+ # Mutable per-run timing and counter collector for cold/warm extraction benchmarks.
6
+ class ExtractionProfile
7
+ STAGES = %i[
8
+ total
9
+ project_discovery
10
+ source_enumeration
11
+ load_path_discovery
12
+ file_read
13
+ prism_parse
14
+ import_extraction
15
+ constant_indexing
16
+ target_resolution
17
+ target_index
18
+ edge_classification
19
+ edge_merge
20
+ ].freeze
21
+ COUNTERS = %i[
22
+ source_files
23
+ malformed_files
24
+ imports
25
+ unresolved_imports
26
+ unresolved_constant_references
27
+ ambiguous_constant_references
28
+ resolution_cache_hits
29
+ resolution_cache_misses
30
+ raw_edges
31
+ merged_edges
32
+ ].freeze
33
+
34
+ attr_reader :stage_seconds, :counters
35
+
36
+ def self.measure(profile, stage, &operation)
37
+ return operation.call unless profile
38
+
39
+ profile.measure(stage, &operation)
40
+ end
41
+
42
+ def self.validate(value)
43
+ return if value.nil? || value.is_a?(self)
44
+
45
+ raise ArgumentError, 'profile must be an ExtractionProfile value or nil'
46
+ end
47
+
48
+ def initialize(clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
49
+ raise ArgumentError, 'clock must respond to call' unless clock.respond_to?(:call)
50
+
51
+ @clock = clock
52
+ @stage_seconds = STAGES.to_h { |stage| [stage, 0.0] }
53
+ @counters = COUNTERS.to_h { |counter| [counter, 0] }
54
+ @cache_hit = false
55
+ end
56
+
57
+ def measure(stage)
58
+ validate_stage(stage)
59
+ started_at = @clock.call
60
+ yield
61
+ ensure
62
+ @stage_seconds[stage] += @clock.call - started_at if started_at
63
+ end
64
+
65
+ def increment(counter, amount = 1)
66
+ validate_counter(counter)
67
+ unless amount.is_a?(Integer) && !amount.negative?
68
+ raise ArgumentError, 'counter amount must be a non-negative Integer'
69
+ end
70
+
71
+ @counters[counter] += amount
72
+ end
73
+
74
+ def record_cache_hit
75
+ @cache_hit = true
76
+ end
77
+
78
+ def cache_hit?
79
+ @cache_hit
80
+ end
81
+
82
+ def to_h
83
+ {
84
+ cache_hit: cache_hit?,
85
+ stage_seconds: stage_seconds.transform_values { |value| value.round(6) },
86
+ counters: counters.dup
87
+ }
88
+ end
89
+
90
+ private
91
+
92
+ def validate_stage(stage)
93
+ return if STAGES.include?(stage)
94
+
95
+ raise ArgumentError, "unknown extraction stage: #{stage.inspect}"
96
+ end
97
+
98
+ def validate_counter(counter)
99
+ return if COUNTERS.include?(counter)
100
+
101
+ raise ArgumentError, "unknown extraction counter: #{counter.inspect}"
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/extraction/edge'
4
+ require_relative '../common/extraction/graph'
5
+ require_relative 'extraction_profile'
6
+
7
+ module ArchUnit
8
+ module Extraction
9
+ # Final graph construction shared with the counted extraction pipeline.
10
+ # @api private
11
+ module GraphAssembly
12
+ def self.build(edges, profile)
13
+ merged = ExtractionProfile.measure(profile, :edge_merge) { merge_parallel_edges(edges) }
14
+ profile&.increment(:merged_edges, merged.length)
15
+ Common::Extraction::Graph.new(merged)
16
+ end
17
+
18
+ def self.merge_parallel_edges(edges)
19
+ edges.group_by { |edge| [edge.source, edge.target] }.values.map do |parallel_edges|
20
+ first = parallel_edges.first
21
+ Common::Extraction::Edge.new(
22
+ source: first.source, target: first.target, external: first.external,
23
+ import_kinds: parallel_edges.flat_map(&:import_kinds),
24
+ evidence: parallel_edges.flat_map(&:evidence)
25
+ )
26
+ end
27
+ end
28
+ private_class_method :merge_parallel_edges
29
+ end
30
+ private_constant :GraphAssembly
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ArchUnit
4
+ module Extraction
5
+ # Execution-only options are excluded; source exclusions already live in the pattern key.
6
+ module GraphCacheKey
7
+ REPRESENTED_ELSEWHERE = %i[
8
+ allow_empty_tests logging clear_cache source_exclusions
9
+ ].freeze
10
+ private_constant :REPRESENTED_ELSEWHERE
11
+
12
+ def self.build(root, patterns, options)
13
+ [root.to_s.tr('\\', '/').freeze, patterns.sort.freeze, analysis_toggles(options)].freeze
14
+ end
15
+
16
+ def self.analysis_toggles(options)
17
+ options.class.members.filter_map do |member|
18
+ next if REPRESENTED_ELSEWHERE.include?(member)
19
+
20
+ [member, options.public_send(member)].freeze
21
+ end.freeze
22
+ end
23
+ private_class_method :analysis_toggles
24
+ end
25
+ private_constant :GraphCacheKey
26
+ end
27
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'prism'
4
+ require_relative '../common/extraction/import_kind'
5
+ require_relative 'load_arguments'
6
+
7
+ module ArchUnit
8
+ module Extraction
9
+ # Matches supported syntax, separating explicit framework vocabulary from default Ruby calls.
10
+ class ImportCalls
11
+ KINDS = {
12
+ require: Common::Extraction::ImportKind::REQUIRE,
13
+ require_relative: Common::Extraction::ImportKind::REQUIRE_RELATIVE,
14
+ autoload: Common::Extraction::ImportKind::AUTOLOAD,
15
+ load: Common::Extraction::ImportKind::LOAD,
16
+ require_dependency: Common::Extraction::ImportKind::REQUIRE_DEPENDENCY
17
+ }.freeze
18
+ private_constant :KINDS
19
+
20
+ def initialize(rails_imports:, source_policy:)
21
+ unless [true, false].include?(rails_imports)
22
+ raise ArgumentError, 'rails_imports must be true or false'
23
+ end
24
+
25
+ @rails_imports = rails_imports
26
+ @source_policy = source_policy
27
+ end
28
+
29
+ def kind(node)
30
+ value = KINDS[node.name]
31
+ return unless value && enabled?(value) && supported_receiver?(node.receiver, value)
32
+
33
+ value if argument(node.arguments&.arguments || [], value)
34
+ end
35
+
36
+ def path(arguments, kind)
37
+ return if uncertain?(kind)
38
+ if [Common::Extraction::ImportKind::LOAD,
39
+ Common::Extraction::ImportKind::REQUIRE_DEPENDENCY].include?(kind)
40
+ return LoadArguments.path(arguments)
41
+ end
42
+
43
+ argument(arguments, kind)
44
+ end
45
+
46
+ def uncertain?(kind)
47
+ kind == Common::Extraction::ImportKind::REQUIRE_DEPENDENCY &&
48
+ !@source_policy.allowed?(:rails_helpers)
49
+ end
50
+
51
+ private
52
+
53
+ def enabled?(kind)
54
+ @rails_imports || kind != Common::Extraction::ImportKind::REQUIRE_DEPENDENCY
55
+ end
56
+
57
+ def argument(arguments, kind)
58
+ case kind
59
+ when Common::Extraction::ImportKind::AUTOLOAD
60
+ arguments[1] if arguments.length == 2
61
+ when Common::Extraction::ImportKind::LOAD, Common::Extraction::ImportKind::REQUIRE_DEPENDENCY
62
+ variable_argument(arguments, kind)
63
+ else
64
+ arguments.first if arguments.length == 1
65
+ end
66
+ end
67
+
68
+ def variable_argument(arguments, kind)
69
+ limit = kind == Common::Extraction::ImportKind::LOAD ? 2 : 1
70
+ arguments.first if LoadArguments.possible?(arguments, max: limit)
71
+ end
72
+
73
+ def supported_receiver?(receiver, kind)
74
+ return true if receiver.nil?
75
+ if kind == Common::Extraction::ImportKind::REQUIRE_DEPENDENCY
76
+ return receiver.is_a?(Prism::SelfNode)
77
+ end
78
+ if kind == Common::Extraction::ImportKind::AUTOLOAD
79
+ return receiver.is_a?(Prism::ConstantReadNode) || receiver.is_a?(Prism::ConstantPathNode)
80
+ end
81
+
82
+ kernel_receiver?(receiver)
83
+ end
84
+
85
+ def kernel_receiver?(receiver)
86
+ return receiver.name == :Kernel if receiver.is_a?(Prism::ConstantReadNode)
87
+
88
+ receiver.is_a?(Prism::ConstantPathNode) && receiver.parent.nil? && receiver.name == :Kernel
89
+ end
90
+ end
91
+ private_constant :ImportCalls
92
+ end
93
+ end