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
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ArchUnit
4
+ module Extraction
5
+ # Keeps local-string inference inside straight-line code and lexical scopes.
6
+ module ConservativeLocalFlow
7
+ def visit_def_node(node)
8
+ @local_strings.isolated_scope { super }
9
+ end
10
+
11
+ def visit_class_node(node)
12
+ @local_strings.isolated_scope { super }
13
+ end
14
+
15
+ def visit_module_node(node)
16
+ @local_strings.isolated_scope { super }
17
+ end
18
+
19
+ def visit_singleton_class_node(node)
20
+ @local_strings.isolated_scope { super }
21
+ end
22
+
23
+ def visit_block_node(node)
24
+ @local_strings.isolated_scope { super }
25
+ @local_strings.capture_scopes
26
+ end
27
+
28
+ def visit_lambda_node(node)
29
+ @local_strings.isolated_scope { super }
30
+ @local_strings.capture_scopes
31
+ end
32
+
33
+ def visit_local_variable_operator_write_node(node)
34
+ super
35
+ @local_strings.assign(node.name, nil)
36
+ end
37
+
38
+ def visit_local_variable_or_write_node(node)
39
+ super
40
+ @local_strings.assign(node.name, nil)
41
+ end
42
+
43
+ def visit_local_variable_and_write_node(node)
44
+ super
45
+ @local_strings.assign(node.name, nil)
46
+ end
47
+
48
+ def visit_multi_write_node(node)
49
+ super
50
+ @local_strings.invalidate_all
51
+ end
52
+
53
+ def visit_post_execution_node(node)
54
+ @local_strings.uncertain_flow { super }
55
+ @local_strings.capture_scopes
56
+ end
57
+
58
+ def visit_match_write_node(node)
59
+ super
60
+ @local_strings.invalidate_all
61
+ end
62
+
63
+ def visit_instance_variable_write_node(node)
64
+ super
65
+ @local_strings.invalidate_escaped(node.value)
66
+ end
67
+
68
+ def visit_global_variable_write_node(node)
69
+ super
70
+ @local_strings.invalidate_escaped(node.value)
71
+ end
72
+
73
+ def visit_class_variable_write_node(node)
74
+ super
75
+ @local_strings.invalidate_escaped(node.value)
76
+ end
77
+
78
+ def visit_constant_write_node(node)
79
+ super
80
+ @local_strings.invalidate_escaped(node.value)
81
+ end
82
+
83
+ def visit_constant_path_write_node(node)
84
+ super
85
+ @local_strings.invalidate_escaped(node.value)
86
+ end
87
+
88
+ # These constructs require control-flow analysis beyond the supported straight-line subset.
89
+ %i[
90
+ begin defined pre_execution if unless and or case case_match while until for
91
+ rescue rescue_modifier
92
+ ].each do |kind|
93
+ define_method("visit_#{kind}_node") do |node|
94
+ @local_strings.uncertain_flow { super(node) }
95
+ end
96
+ end
97
+ end
98
+ private_constant :ConservativeLocalFlow
99
+ end
100
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/extraction/edge'
4
+ require_relative 'constant_visitor'
5
+
6
+ module ArchUnit
7
+ module Extraction
8
+ # Bounded project definition index. Unknown/ambiguous names never invent file targets.
9
+ class ConstantIndex
10
+ Location = Data.define(:source, :line_number)
11
+ MAX_NAMESPACE_PASSES = 8
12
+
13
+ def initialize(analyses, profile: nil)
14
+ @analyses = analyses
15
+ @profile = profile
16
+ @visitors = stabilized_visitors
17
+ @definitions = definition_locations
18
+ @namespaces = namespace_names(@visitors)
19
+ @inherited_namespaces = @visitors.values.flat_map(&:inherited_namespaces).to_set
20
+ end
21
+
22
+ def edges
23
+ @visitors.flat_map do |source, visitor|
24
+ visitor.references.filter_map { |reference| resolve_edge(source, reference) }
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def stabilized_visitors
31
+ visitors = collect
32
+ MAX_NAMESPACE_PASSES.times do
33
+ names = namespace_names(visitors)
34
+ visitors = collect(namespace_names: names)
35
+ return visitors if namespace_names(visitors) == names
36
+ end
37
+ unresolved_count = visitors.values.sum { |visitor| visitor.references.length }
38
+ @profile&.increment(:unresolved_constant_references, unresolved_count)
39
+ {}
40
+ end
41
+
42
+ def collect(namespace_names: nil)
43
+ @analyses.to_h do |source, result|
44
+ visitor = ConstantVisitor.new(namespace_names:)
45
+ visitor.visit(result.value)
46
+ [source, visitor]
47
+ end
48
+ end
49
+
50
+ def namespace_names(visitors)
51
+ visitors.values.flat_map(&:definitions).each_with_object(Set.new) do |definition, names|
52
+ parts = definition.name.split('::')
53
+ parts.length.times { |index| names << parts.take(index + 1).join('::') }
54
+ end
55
+ end
56
+
57
+ def definition_locations
58
+ map = Hash.new { |hash, key| hash[key] = [] }
59
+ @visitors.each do |source, visitor|
60
+ visitor.definitions.each do |definition|
61
+ map[definition.name] << Location.new(source:, line_number: definition.line_number)
62
+ end
63
+ end
64
+ map
65
+ end
66
+
67
+ def resolve_edge(source, reference)
68
+ name = resolve_name(reference)
69
+ locations = name ? @definitions.fetch(name, []) : []
70
+ return unresolved(:unresolved_constant_references) if locations.empty?
71
+ return unresolved(:ambiguous_constant_references) if locations.map(&:source).uniq.length > 1
72
+
73
+ location = locations.first
74
+ return if source == location.source
75
+
76
+ evidence_edge(source, reference, location)
77
+ end
78
+
79
+ def resolve_name(reference)
80
+ name = reference.name
81
+ return name.delete_prefix('::') if name.start_with?('::')
82
+
83
+ head = name.split('::').first
84
+ owner = reference.nesting.find { |scope| @namespaces.include?("#{scope}::#{head}") }
85
+ return "#{owner}::#{name}" if owner
86
+ return if @inherited_namespaces.include?(reference.inherited_scope)
87
+
88
+ name
89
+ end
90
+
91
+ def unresolved(counter)
92
+ @profile&.increment(counter)
93
+ nil
94
+ end
95
+
96
+ def evidence_edge(source, reference, location)
97
+ kind = Common::Extraction::ImportKind::CONSTANT_REFERENCE
98
+ evidence = Common::Extraction::DependencyEvidence.new(
99
+ source:, kind:, name: reference.name, line_number: reference.line_number,
100
+ end_line: reference.end_line, target_line_number: location.line_number
101
+ )
102
+ Common::Extraction::Edge.new(
103
+ source:, target: location.source, external: false,
104
+ import_kinds: [kind], evidence: [evidence]
105
+ )
106
+ end
107
+ end
108
+ private_constant :ConstantIndex
109
+ end
110
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'prism'
4
+
5
+ module ArchUnit
6
+ module Extraction
7
+ # Collects declarations and uses with Ruby's lexical (not textual path) nesting.
8
+ class ConstantVisitor < Prism::Visitor
9
+ Definition = Data.define(:name, :line_number)
10
+ Reference = Data.define(:name, :nesting, :inherited_scope, :line_number, :end_line)
11
+
12
+ attr_reader :definitions, :references, :inherited_namespaces
13
+
14
+ def initialize(namespace_names: nil)
15
+ super()
16
+ @namespace_names = namespace_names
17
+ @definitions = []
18
+ @references = []
19
+ @nesting = []
20
+ @inherited_namespaces = []
21
+ end
22
+
23
+ def visit_class_node(node)
24
+ visit_namespace_receiver(node.constant_path)
25
+ visit(node.superclass)
26
+ visit_namespace(node, inherited: !node.superclass.nil?)
27
+ end
28
+
29
+ def visit_module_node(node)
30
+ visit_namespace_receiver(node.constant_path)
31
+ visit_namespace(node, inherited: false)
32
+ end
33
+
34
+ def visit_constant_write_node(node)
35
+ define_constant(qualify(node.name.to_s), node)
36
+ visit(node.value)
37
+ end
38
+
39
+ def visit_constant_path_write_node(node)
40
+ visit_namespace_receiver(node.target)
41
+ name = static_name(node.target)
42
+ define_constant(declaration_name(name), node) if name
43
+ visit(node.value)
44
+ end
45
+
46
+ # ||= introduces a possible declaration, like a conditional ordinary assignment.
47
+ # This records ownership, not whether the branch executes or what value it stores.
48
+ alias visit_constant_or_write_node visit_constant_write_node
49
+ alias visit_constant_path_or_write_node visit_constant_path_write_node
50
+
51
+ def visit_constant_read_node(node)
52
+ record_reference(node.name.to_s, node)
53
+ end
54
+
55
+ def visit_constant_path_node(node)
56
+ name = static_name(node)
57
+ name ? record_reference(name, node) : super
58
+ end
59
+
60
+ def visit_call_node(node)
61
+ if %i[include prepend extend const_set remove_const autoload].include?(node.name) &&
62
+ !@nesting.empty?
63
+ @inherited_namespaces << @nesting.first
64
+ end
65
+ super
66
+ end
67
+
68
+ def visit_singleton_class_node(node)
69
+ visit(node.expression)
70
+ previous = @dynamic_scope
71
+ @dynamic_scope = true
72
+ visit(node.body)
73
+ ensure
74
+ @dynamic_scope = previous
75
+ end
76
+
77
+ private
78
+
79
+ def visit_namespace_receiver(path)
80
+ # Only the owner is read; the final name is being declared, not referenced.
81
+ visit(path.parent) if path.is_a?(Prism::ConstantPathNode) && static_name(path)
82
+ end
83
+
84
+ def visit_namespace(node, inherited:)
85
+ name = static_name(node.constant_path)
86
+ return unless name
87
+
88
+ full_name = declaration_name(name)
89
+ define_constant(full_name, node.constant_path)
90
+ @inherited_namespaces << full_name if inherited
91
+ previous = @nesting
92
+ @nesting = [full_name, *previous]
93
+ visit(node.body)
94
+ ensure
95
+ @nesting = previous if previous
96
+ end
97
+
98
+ def declaration_name(name)
99
+ return name.delete_prefix('::') if name.start_with?('::') || @nesting.empty?
100
+ return qualify(name) unless name.include?('::')
101
+
102
+ head = name.split('::').first
103
+ owner = @nesting.find { |scope| @namespace_names&.include?("#{scope}::#{head}") }
104
+ owner ? "#{owner}::#{name}" : name
105
+ end
106
+
107
+ def qualify(name)
108
+ @nesting.empty? ? name : "#{@nesting.first}::#{name}"
109
+ end
110
+
111
+ def define_constant(name, node)
112
+ return if @dynamic_scope
113
+
114
+ @definitions << Definition.new(name:, line_number: node.location.start_line)
115
+ end
116
+
117
+ def record_reference(name, node)
118
+ return if @dynamic_scope && !name.start_with?('::')
119
+
120
+ @references << Reference.new(
121
+ name:, nesting: @nesting.dup.freeze, inherited_scope: @nesting.first,
122
+ line_number: node.location.start_line, end_line: node.location.end_line
123
+ )
124
+ end
125
+
126
+ def static_name(node)
127
+ return node.name.to_s if node.is_a?(Prism::ConstantReadNode)
128
+ return unless node.is_a?(Prism::ConstantPathNode)
129
+ return "::#{node.name}" if node.parent.nil?
130
+
131
+ parent = static_name(node.parent)
132
+ "#{parent}::#{node.name}" if parent
133
+ end
134
+ end
135
+ private_constant :ConstantVisitor
136
+ end
137
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'resolve_load_paths'
4
+
5
+ module ArchUnit
6
+ module Extraction
7
+ # Per-graph extraction state; keeps helper search roots separate from ordinary Ruby imports.
8
+ DependencyContext = Data.define(
9
+ :root, :targets, :load_paths, :profile, :cache, :include_evidence,
10
+ :rails_imports, :helper_load_paths
11
+ ) do
12
+ def self.build(root:, targets:, load_paths:, profile:, options:)
13
+ helpers = if options.rails_imports?
14
+ LoadPaths.with_explicit_first(root, load_paths, options.load_paths)
15
+ else
16
+ load_paths
17
+ end
18
+ new(root:, targets:, load_paths:, profile:, cache: {},
19
+ include_evidence: options.constant_references?, rails_imports: options.rails_imports,
20
+ helper_load_paths: helpers)
21
+ end
22
+
23
+ def extractor_options
24
+ { project_root: root, load_paths:, profile:, resolution_cache: cache,
25
+ rails_imports:, helper_load_paths: }
26
+ end
27
+ end
28
+ private_constant :DependencyContext
29
+ end
30
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'find'
4
+ require 'pathname'
5
+ require_relative 'enumerate_source_files'
6
+
7
+ module ArchUnit
8
+ module Extraction
9
+ # Discovers gem-adjacent lib directories without evaluating gemspecs.
10
+ module GemspecLoadPaths
11
+ module_function
12
+
13
+ def resolve(root, source_files: nil)
14
+ paths = if source_files
15
+ discover_from_sources(root, source_files)
16
+ else
17
+ discover_with_walk(root)
18
+ end
19
+ paths.uniq.sort_by { |path| normalized(path) }
20
+ end
21
+
22
+ def discover_from_sources(root, source_files)
23
+ roots = source_files.flat_map { |source| source_lib_roots(root, source) }.uniq
24
+ roots.flat_map do |gem_root|
25
+ gemspecs_in(gem_root).filter_map { |entry| gemspec_lib_path(entry, root) }
26
+ end
27
+ end
28
+ private_class_method :discover_from_sources
29
+
30
+ def source_lib_roots(root, source)
31
+ segments = source.split('/')
32
+ segments.each_index.filter_map do |index|
33
+ next unless segments[index] == 'lib'
34
+
35
+ index.zero? ? root : root.join(*segments.take(index))
36
+ end
37
+ end
38
+ private_class_method :source_lib_roots
39
+
40
+ def gemspecs_in(directory)
41
+ directory.children.select do |entry|
42
+ entry.file? && entry.extname == '.gemspec'
43
+ end
44
+ end
45
+ private_class_method :gemspecs_in
46
+
47
+ def discover_with_walk(root)
48
+ paths = []
49
+ Find.find(root.to_s) do |entry|
50
+ next if directory_entry?(entry, root)
51
+
52
+ lib = gemspec_lib_path(Pathname.new(entry), root)
53
+ paths << lib if lib
54
+ end
55
+ paths
56
+ end
57
+ private_class_method :discover_with_walk
58
+
59
+ def directory_entry?(entry, root)
60
+ return false unless File.directory?(entry)
61
+
62
+ Find.prune if entry != root.to_s && excluded_directory?(entry)
63
+ true
64
+ end
65
+ private_class_method :directory_entry?
66
+
67
+ def gemspec_lib_path(entry, root)
68
+ return unless entry.extname == '.gemspec'
69
+
70
+ lib = entry.dirname.join('lib')
71
+ return unless lib.directory?
72
+
73
+ canonical = lib.realpath
74
+ canonical if within_project?(canonical, root)
75
+ end
76
+ private_class_method :gemspec_lib_path
77
+
78
+ def excluded_directory?(path)
79
+ Extraction::DEFAULT_EXCLUDED_DIRECTORIES.include?(File.basename(path))
80
+ end
81
+ private_class_method :excluded_directory?
82
+
83
+ def within_project?(path, root)
84
+ path.relative_path_from(root).each_filename.first != '..'
85
+ rescue ArgumentError
86
+ false
87
+ end
88
+ private_class_method :within_project?
89
+
90
+ def normalized(path)
91
+ path.to_s.tr('\\', '/')
92
+ end
93
+ private_class_method :normalized
94
+ end
95
+ end
96
+ end
@@ -4,9 +4,12 @@ require 'find'
4
4
  require 'pathname'
5
5
  require_relative '../error/technical_error'
6
6
  require_relative '../error/user_error'
7
+ require_relative 'arch_ignore'
8
+ require_relative '../common/fluentapi/check_options'
7
9
 
8
10
  module ArchUnit
9
11
  # Ruby-specific project discovery and source extraction.
12
+ # rubocop:disable-next Metrics/ModuleLength -- One source policy keeps graphs and metrics aligned.
10
13
  module Extraction
11
14
  DEFAULT_EXCLUDED_DIRECTORIES = %w[
12
15
  .bundle
@@ -25,27 +28,49 @@ module ArchUnit
25
28
  tmp
26
29
  vendor
27
30
  ].freeze
31
+ SourceDiscovery = Data.define(:root, :patterns, :files)
32
+ private_constant :SourceDiscovery
28
33
 
29
34
  module_function
30
35
 
31
- def enumerate_source_files(project_root, exclude_patterns: nil)
36
+ def enumerate_source_files(project_root, exclude_patterns: nil, options: nil)
37
+ discover_source_files(project_root, exclude_patterns:, options:).files
38
+ end
39
+
40
+ # @api private
41
+ def discover_source_files(project_root, exclude_patterns: nil, options: nil)
42
+ options = Common::FluentApi::CheckOptions.resolve(options)
32
43
  root = source_root(project_root)
33
- patterns = resolve_exclude_patterns(exclude_patterns)
44
+ patterns = resolve_project_exclude_patterns(
45
+ root, exclude_patterns, source_exclusions: options.source_exclusions
46
+ )
47
+ SourceDiscovery.new(root: root.freeze, patterns:,
48
+ files: enumerate_source_files_with_patterns(root, patterns))
49
+ end
50
+
51
+ def enumerate_source_files_with_patterns(root, patterns)
34
52
  source_files_under(root, patterns).sort.freeze
35
53
  rescue UserError, TechnicalError
36
54
  raise
37
55
  rescue SystemCallError => e
38
56
  raise TechnicalError, "could not enumerate Ruby source files: #{e.message}"
39
57
  end
58
+ private_class_method :enumerate_source_files_with_patterns
59
+
60
+ def resolve_project_exclude_patterns(root, value, source_exclusions: [])
61
+ configured = resolve_exclude_patterns(value)
62
+ resolve_exclude_patterns([*configured, *ArchIgnore.patterns(root), *source_exclusions])
63
+ end
64
+ private_class_method :resolve_project_exclude_patterns
40
65
 
41
66
  def source_files_under(root, exclude_patterns)
42
67
  Find.find(root.to_s).filter_map do |entry|
43
- if File.directory?(entry)
44
- Find.prune if excluded_path?(entry, root, exclude_patterns)
45
- next
46
- end
68
+ stat = File.lstat(entry)
69
+ next if stat.symlink?
47
70
 
48
- if ruby_source_file?(entry) && !excluded_path?(entry, root, exclude_patterns)
71
+ if stat.directory?
72
+ Find.prune if excluded_path?(entry, root, exclude_patterns)
73
+ elsif ruby_source_file?(entry, stat) && !excluded_path?(entry, root, exclude_patterns)
49
74
  relative_identifier(entry, root).freeze
50
75
  end
51
76
  end
@@ -85,8 +110,8 @@ module ArchUnit
85
110
  end
86
111
  private_class_method :normalize_exclude_pattern
87
112
 
88
- def ruby_source_file?(entry)
89
- File.file?(entry) && File.extname(entry) == '.rb'
113
+ def ruby_source_file?(entry, stat)
114
+ stat.file? && File.extname(entry) == '.rb'
90
115
  end
91
116
  private_class_method :ruby_source_file?
92
117
 
@@ -100,6 +125,8 @@ module ArchUnit
100
125
  raise UserError, 'project_root must be an existing directory' unless root.directory?
101
126
 
102
127
  root.realpath
128
+ rescue SystemCallError => e
129
+ raise TechnicalError, "could not enumerate Ruby source files: #{e.message}"
103
130
  end
104
131
  private_class_method :source_root
105
132
 
@@ -2,31 +2,82 @@
2
2
 
3
3
  require 'pathname'
4
4
  require_relative '../common/extraction/edge'
5
+ require_relative '../common/fluentapi/check_options'
5
6
  require_relative 'enumerate_source_files'
6
7
  require_relative 'extract_imports'
8
+ require_relative 'resolve_load_paths'
9
+ require_relative 'constant_index'
10
+ require_relative 'dependency_context'
7
11
 
8
12
  module ArchUnit
9
13
  # Turns statically resolvable Ruby imports into dependency edges.
10
14
  module Extraction
11
15
  module_function
12
16
 
13
- def extract_dependencies(project_root, exclude_patterns: nil)
14
- source_files = enumerate_source_files(project_root, exclude_patterns:)
17
+ def extract_dependencies(project_root, exclude_patterns: nil, options: nil)
18
+ options = Common::FluentApi::CheckOptions.resolve(options)
19
+ source_files = enumerate_source_files(project_root, exclude_patterns:, options:)
15
20
  root = Pathname.new(path_value(project_root)).expand_path.realpath
16
- extract_dependencies_from(root, source_files)
21
+ load_paths = LoadPaths.resolve(
22
+ root, explicit_paths: options.load_paths, source_files:
23
+ )
24
+ extract_dependencies_from(root, source_files, load_paths:, options:)
17
25
  end
18
26
 
19
- def extract_dependencies_from(root, source_files)
20
- internal_targets = internal_target_index(root, source_files)
27
+ def extract_dependencies_from(root, source_files, load_paths:, options:, profile: nil)
28
+ context = DependencyContext.build(
29
+ root:, targets: profiled_target_index(root, source_files, profile), load_paths:,
30
+ profile:, options:
31
+ )
21
32
 
22
- source_files.flat_map do |source|
23
- extract_imports(root.join(source), project_root: root).map do |import|
24
- dependency_edge(source, import, internal_targets)
25
- end
26
- end.freeze
33
+ analyses = options.constant_references? ? parse_sources(root, source_files, profile) : {}
34
+ edges = source_files.flat_map do |source|
35
+ dependencies_for_source(source, context, analyses[source])
36
+ end
37
+ edges.concat(constant_edges(analyses, profile)) if options.constant_references?
38
+ edges.freeze
27
39
  end
28
40
  private_class_method :extract_dependencies_from
29
41
 
42
+ def profiled_target_index(root, source_files, profile)
43
+ ExtractionProfile.measure(profile, :target_index) do
44
+ internal_target_index(root, source_files)
45
+ end
46
+ end
47
+ private_class_method :profiled_target_index
48
+
49
+ def parse_sources(root, sources, profile)
50
+ sources.to_h { |source| [source, parse_source(root.join(source), profile)] }
51
+ end
52
+ private_class_method :parse_sources
53
+
54
+ def constant_edges(analyses, profile)
55
+ ExtractionProfile.measure(profile, :constant_indexing) do
56
+ ConstantIndex.new(analyses, profile:).edges
57
+ end
58
+ end
59
+ private_class_method :constant_edges
60
+
61
+ def dependencies_for_source(source, context, parse_result)
62
+ imports = extract_imports_from(
63
+ context.root.join(source), **context.extractor_options, parse_result:
64
+ )
65
+ context.profile&.increment(:imports, imports.length)
66
+ classify_imports(source, imports, context)
67
+ end
68
+ private_class_method :dependencies_for_source
69
+
70
+ def classify_imports(source, imports, context)
71
+ ExtractionProfile.measure(context.profile, :edge_classification) do
72
+ imports.map do |import|
73
+ dependency_edge(
74
+ source, import, context.targets, include_evidence: context.include_evidence
75
+ )
76
+ end
77
+ end
78
+ end
79
+ private_class_method :classify_imports
80
+
30
81
  def path_value(value)
31
82
  value.respond_to?(:to_path) ? value.to_path : value
32
83
  end
@@ -39,22 +90,30 @@ module ArchUnit
39
90
  end
40
91
  private_class_method :internal_target_index
41
92
 
42
- def dependency_edge(source, import, internal_targets)
93
+ def dependency_edge(source, import, internal_targets, include_evidence:)
43
94
  target = import.resolved_path && internal_targets[canonical_path(import.resolved_path)]
44
95
 
45
96
  Common::Extraction::Edge.new(
46
97
  source: source,
47
98
  target: target || import.module_name,
48
99
  external: target.nil?,
49
- import_kinds: [import.import_kind]
100
+ import_kinds: [import.import_kind],
101
+ evidence: include_evidence ? [import_evidence(source, import)] : []
50
102
  )
51
103
  end
52
104
  private_class_method :dependency_edge
53
105
 
106
+ def import_evidence(source, import)
107
+ Common::Extraction::DependencyEvidence.new(
108
+ source:, kind: import.import_kind, name: import.module_name,
109
+ line_number: import.line_number, end_line: import.end_line
110
+ )
111
+ end
112
+ private_class_method :import_evidence
113
+
54
114
  def canonical_path(path)
55
- Pathname.new(path).realpath.to_s.tr('\\', '/')
56
- rescue SystemCallError
57
- Pathname.new(path).expand_path.to_s.tr('\\', '/')
115
+ normalized = Pathname.new(path).expand_path.cleanpath.to_s.tr('\\', '/')
116
+ File::FNM_SYSCASE.zero? ? normalized : normalized.downcase
58
117
  end
59
118
  private_class_method :canonical_path
60
119
  end