archunit 1.0.0 → 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.
- checksums.yaml +4 -4
- data/API.md +213 -5
- data/CHANGELOG.md +41 -1
- data/CODE_OF_CONDUCT.md +40 -0
- data/CONTRIBUTING.md +75 -0
- data/GUIDE.md +804 -0
- data/README.md +198 -562
- data/SECURITY.md +64 -0
- data/SUPPORT.md +23 -0
- data/exe/archunit +7 -0
- data/lib/archunit/cli/analysis_report.rb +62 -0
- data/lib/archunit/cli/analysis_text.rb +25 -0
- data/lib/archunit/cli/baseline_context.rb +42 -0
- data/lib/archunit/cli/baseline_document.rb +58 -0
- data/lib/archunit/cli/baseline_fingerprint.rb +36 -0
- data/lib/archunit/cli/baseline_identity.rb +85 -0
- data/lib/archunit/cli/baseline_ledger.rb +38 -0
- data/lib/archunit/cli/baseline_object.rb +15 -0
- data/lib/archunit/cli/baseline_options.rb +48 -0
- data/lib/archunit/cli/baseline_run.rb +85 -0
- data/lib/archunit/cli/baseline_schema.rb +53 -0
- data/lib/archunit/cli/baseline_text.rb +31 -0
- data/lib/archunit/cli/configuration.rb +93 -0
- data/lib/archunit/cli/options.rb +117 -0
- data/lib/archunit/cli/report.rb +121 -0
- data/lib/archunit/cli/runner.rb +108 -0
- data/lib/archunit/cli.rb +15 -0
- data/lib/archunit/common/extraction/import_kind.rb +3 -1
- data/lib/archunit/common/extraction/source_exclusions.rb +43 -0
- data/lib/archunit/common/fluentapi/check_options.rb +13 -3
- data/lib/archunit/extraction/analysis_observer.rb +24 -0
- data/lib/archunit/extraction/analysis_profile.rb +30 -0
- data/lib/archunit/extraction/analysis_snapshot.rb +51 -0
- data/lib/archunit/extraction/constant_visitor.rb +8 -0
- data/lib/archunit/extraction/dependency_context.rb +30 -0
- data/lib/archunit/extraction/enumerate_source_files.rb +17 -5
- data/lib/archunit/extraction/extract_dependencies.rb +5 -14
- data/lib/archunit/extraction/extract_graph.rb +45 -55
- data/lib/archunit/extraction/extract_imports.rb +11 -14
- data/lib/archunit/extraction/graph_assembly.rb +32 -0
- data/lib/archunit/extraction/graph_cache_key.rb +27 -0
- data/lib/archunit/extraction/import_calls.rb +93 -0
- data/lib/archunit/extraction/import_resolver.rb +11 -2
- data/lib/archunit/extraction/import_source_policy.rb +123 -0
- data/lib/archunit/extraction/import_visitor.rb +31 -46
- data/lib/archunit/extraction/load_arguments.rb +34 -0
- data/lib/archunit/extraction/local_string_tracker.rb +4 -3
- data/lib/archunit/extraction/resolve_load_paths.rb +5 -0
- data/lib/archunit/extraction/source_analysis.rb +26 -0
- data/lib/archunit/extraction/static_import_path.rb +111 -0
- data/lib/archunit/layers/fluentapi/architecture_preset.rb +49 -0
- data/lib/archunit/layers/fluentapi/preset_definitions.rb +39 -0
- data/lib/archunit/layers/fluentapi/preset_options.rb +69 -0
- data/lib/archunit/layers/fluentapi/preset_rule.rb +45 -0
- data/lib/archunit/metrics/extraction/extract_distance_info.rb +1 -1
- data/lib/archunit/metrics/extraction/extract_project_info.rb +8 -4
- data/lib/archunit/metrics/fluentapi/metric_report_builder.rb +4 -3
- data/lib/archunit/metrics/fluentapi/metrics_builder.rb +3 -3
- data/lib/archunit/metrics/reporting/metrics_export_options.rb +6 -2
- data/lib/archunit/version.rb +1 -1
- data/lib/archunit.rb +2 -0
- metadata +48 -8
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ArchUnit
|
|
4
|
+
module Extraction
|
|
5
|
+
# Immutable facts about one analysis, not a claim of semantic completeness.
|
|
6
|
+
# @api private
|
|
7
|
+
module AnalysisSnapshot
|
|
8
|
+
COUNTERS = %i[
|
|
9
|
+
source_files imports unresolved_imports unresolved_constant_references
|
|
10
|
+
ambiguous_constant_references
|
|
11
|
+
].freeze
|
|
12
|
+
private_constant :COUNTERS
|
|
13
|
+
|
|
14
|
+
def self.graph(root, patterns, options, counters: nil, status: 'analyzed')
|
|
15
|
+
settings = {
|
|
16
|
+
kind: 'dependency_graph', dependency_analysis: options.dependency_analysis.to_s.freeze,
|
|
17
|
+
rails_imports: options.rails_imports, load_paths: options.load_paths
|
|
18
|
+
}
|
|
19
|
+
build(root, patterns, settings, graph_counters(options, counters), status)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.source(discovery, count: nil, status: 'analyzed')
|
|
23
|
+
settings = {
|
|
24
|
+
kind: 'source_metrics', dependency_analysis: nil, rails_imports: nil, load_paths: nil
|
|
25
|
+
}
|
|
26
|
+
counters = COUNTERS.to_h { |key| [key, key == :source_files ? count : nil] }
|
|
27
|
+
build(discovery.root, discovery.patterns, settings, counters, status)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.graph_counters(options, values)
|
|
31
|
+
counters = COUNTERS.to_h { |key| [key, values&.fetch(key)] }
|
|
32
|
+
unless options.constant_references?
|
|
33
|
+
counters[:unresolved_constant_references] = nil
|
|
34
|
+
counters[:ambiguous_constant_references] = nil
|
|
35
|
+
end
|
|
36
|
+
counters
|
|
37
|
+
end
|
|
38
|
+
private_class_method :graph_counters
|
|
39
|
+
|
|
40
|
+
def self.build(root, patterns, settings, counters, status)
|
|
41
|
+
{
|
|
42
|
+
project_root: root.to_s.tr('\\', '/').freeze,
|
|
43
|
+
effective_source_exclusions: patterns.sort.freeze,
|
|
44
|
+
status:, counters: counters.freeze, **settings
|
|
45
|
+
}.freeze
|
|
46
|
+
end
|
|
47
|
+
private_class_method :build
|
|
48
|
+
end
|
|
49
|
+
private_constant :AnalysisSnapshot
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -21,11 +21,13 @@ module ArchUnit
|
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def visit_class_node(node)
|
|
24
|
+
visit_namespace_receiver(node.constant_path)
|
|
24
25
|
visit(node.superclass)
|
|
25
26
|
visit_namespace(node, inherited: !node.superclass.nil?)
|
|
26
27
|
end
|
|
27
28
|
|
|
28
29
|
def visit_module_node(node)
|
|
30
|
+
visit_namespace_receiver(node.constant_path)
|
|
29
31
|
visit_namespace(node, inherited: false)
|
|
30
32
|
end
|
|
31
33
|
|
|
@@ -35,6 +37,7 @@ module ArchUnit
|
|
|
35
37
|
end
|
|
36
38
|
|
|
37
39
|
def visit_constant_path_write_node(node)
|
|
40
|
+
visit_namespace_receiver(node.target)
|
|
38
41
|
name = static_name(node.target)
|
|
39
42
|
define_constant(declaration_name(name), node) if name
|
|
40
43
|
visit(node.value)
|
|
@@ -73,6 +76,11 @@ module ArchUnit
|
|
|
73
76
|
|
|
74
77
|
private
|
|
75
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
|
+
|
|
76
84
|
def visit_namespace(node, inherited:)
|
|
77
85
|
name = static_name(node.constant_path)
|
|
78
86
|
return unless name
|
|
@@ -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
|
|
@@ -5,6 +5,7 @@ require 'pathname'
|
|
|
5
5
|
require_relative '../error/technical_error'
|
|
6
6
|
require_relative '../error/user_error'
|
|
7
7
|
require_relative 'arch_ignore'
|
|
8
|
+
require_relative '../common/fluentapi/check_options'
|
|
8
9
|
|
|
9
10
|
module ArchUnit
|
|
10
11
|
# Ruby-specific project discovery and source extraction.
|
|
@@ -27,13 +28,24 @@ module ArchUnit
|
|
|
27
28
|
tmp
|
|
28
29
|
vendor
|
|
29
30
|
].freeze
|
|
31
|
+
SourceDiscovery = Data.define(:root, :patterns, :files)
|
|
32
|
+
private_constant :SourceDiscovery
|
|
30
33
|
|
|
31
34
|
module_function
|
|
32
35
|
|
|
33
|
-
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)
|
|
34
43
|
root = source_root(project_root)
|
|
35
|
-
patterns = resolve_project_exclude_patterns(
|
|
36
|
-
|
|
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))
|
|
37
49
|
end
|
|
38
50
|
|
|
39
51
|
def enumerate_source_files_with_patterns(root, patterns)
|
|
@@ -45,9 +57,9 @@ module ArchUnit
|
|
|
45
57
|
end
|
|
46
58
|
private_class_method :enumerate_source_files_with_patterns
|
|
47
59
|
|
|
48
|
-
def resolve_project_exclude_patterns(root, value)
|
|
60
|
+
def resolve_project_exclude_patterns(root, value, source_exclusions: [])
|
|
49
61
|
configured = resolve_exclude_patterns(value)
|
|
50
|
-
resolve_exclude_patterns([*configured, *ArchIgnore.patterns(root)])
|
|
62
|
+
resolve_exclude_patterns([*configured, *ArchIgnore.patterns(root), *source_exclusions])
|
|
51
63
|
end
|
|
52
64
|
private_class_method :resolve_project_exclude_patterns
|
|
53
65
|
|
|
@@ -7,20 +7,16 @@ require_relative 'enumerate_source_files'
|
|
|
7
7
|
require_relative 'extract_imports'
|
|
8
8
|
require_relative 'resolve_load_paths'
|
|
9
9
|
require_relative 'constant_index'
|
|
10
|
+
require_relative 'dependency_context'
|
|
10
11
|
|
|
11
12
|
module ArchUnit
|
|
12
13
|
# Turns statically resolvable Ruby imports into dependency edges.
|
|
13
14
|
module Extraction
|
|
14
|
-
DependencyContext = Data.define(
|
|
15
|
-
:root, :targets, :load_paths, :profile, :cache, :include_evidence
|
|
16
|
-
)
|
|
17
|
-
private_constant :DependencyContext
|
|
18
|
-
|
|
19
15
|
module_function
|
|
20
16
|
|
|
21
17
|
def extract_dependencies(project_root, exclude_patterns: nil, options: nil)
|
|
22
18
|
options = Common::FluentApi::CheckOptions.resolve(options)
|
|
23
|
-
source_files = enumerate_source_files(project_root, exclude_patterns:)
|
|
19
|
+
source_files = enumerate_source_files(project_root, exclude_patterns:, options:)
|
|
24
20
|
root = Pathname.new(path_value(project_root)).expand_path.realpath
|
|
25
21
|
load_paths = LoadPaths.resolve(
|
|
26
22
|
root, explicit_paths: options.load_paths, source_files:
|
|
@@ -29,9 +25,9 @@ module ArchUnit
|
|
|
29
25
|
end
|
|
30
26
|
|
|
31
27
|
def extract_dependencies_from(root, source_files, load_paths:, options:, profile: nil)
|
|
32
|
-
context = DependencyContext.
|
|
28
|
+
context = DependencyContext.build(
|
|
33
29
|
root:, targets: profiled_target_index(root, source_files, profile), load_paths:,
|
|
34
|
-
profile:,
|
|
30
|
+
profile:, options:
|
|
35
31
|
)
|
|
36
32
|
|
|
37
33
|
analyses = options.constant_references? ? parse_sources(root, source_files, profile) : {}
|
|
@@ -64,12 +60,7 @@ module ArchUnit
|
|
|
64
60
|
|
|
65
61
|
def dependencies_for_source(source, context, parse_result)
|
|
66
62
|
imports = extract_imports_from(
|
|
67
|
-
context.root.join(source),
|
|
68
|
-
project_root: context.root,
|
|
69
|
-
load_paths: context.load_paths,
|
|
70
|
-
profile: context.profile,
|
|
71
|
-
resolution_cache: context.cache,
|
|
72
|
-
parse_result:
|
|
63
|
+
context.root.join(source), **context.extractor_options, parse_result:
|
|
73
64
|
)
|
|
74
65
|
context.profile&.increment(:imports, imports.length)
|
|
75
66
|
classify_imports(source, imports, context)
|
|
@@ -10,12 +10,18 @@ require_relative 'extract_dependencies'
|
|
|
10
10
|
require_relative 'extraction_profile'
|
|
11
11
|
require_relative 'locate_project'
|
|
12
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'
|
|
13
18
|
|
|
14
19
|
module ArchUnit
|
|
15
20
|
# Assembles the normalized dependency graph for a Ruby project.
|
|
16
21
|
module Extraction
|
|
17
22
|
GRAPH_CACHE_MUTEX = Mutex.new
|
|
18
|
-
|
|
23
|
+
CachedGraph = Data.define(:graph, :analysis)
|
|
24
|
+
private_constant :CachedGraph
|
|
19
25
|
|
|
20
26
|
module_function
|
|
21
27
|
|
|
@@ -29,10 +35,7 @@ module ArchUnit
|
|
|
29
35
|
root = ExtractionProfile.measure(profile, :project_discovery) do
|
|
30
36
|
Pathname.new(locate_project(locator, working_directory:))
|
|
31
37
|
end
|
|
32
|
-
|
|
33
|
-
key = graph_cache_key(root, exclude_patterns: patterns, options:)
|
|
34
|
-
|
|
35
|
-
Common::Logging::Inspection.graph(fetch_graph(root, patterns, options, key, profile), root:)
|
|
38
|
+
resolved_graph(root, exclude_patterns, options, profile)
|
|
36
39
|
end
|
|
37
40
|
end
|
|
38
41
|
|
|
@@ -41,20 +44,48 @@ module ArchUnit
|
|
|
41
44
|
nil
|
|
42
45
|
end
|
|
43
46
|
|
|
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
|
|
57
|
+
|
|
44
58
|
def fetch_graph(root, patterns, options, cache_key, profile)
|
|
45
59
|
GRAPH_CACHE_MUTEX.synchronize do
|
|
46
|
-
|
|
47
|
-
cached = graph_cache[cache_key]
|
|
48
|
-
if cached
|
|
49
|
-
profile&.record_cache_hit
|
|
50
|
-
return cached
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
graph_cache[cache_key] = extract_graph_uncached(root, patterns, options, profile)
|
|
60
|
+
cached_graph(root, patterns, options, cache_key, profile)
|
|
54
61
|
end
|
|
62
|
+
rescue StandardError
|
|
63
|
+
AnalysisObserver.record(AnalysisSnapshot.graph(root, patterns, options, status: 'error'))
|
|
64
|
+
raise
|
|
55
65
|
end
|
|
56
66
|
private_class_method :fetch_graph
|
|
57
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
|
|
74
|
+
|
|
75
|
+
entry = build_graph_entry(root, patterns, options, profile)
|
|
76
|
+
graph_cache[cache_key] = entry
|
|
77
|
+
[entry, false]
|
|
78
|
+
end
|
|
79
|
+
private_class_method :cached_graph
|
|
80
|
+
|
|
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
|
|
88
|
+
|
|
58
89
|
def extract_graph_uncached(root, exclude_patterns, options, profile)
|
|
59
90
|
source_files = ExtractionProfile.measure(profile, :source_enumeration) do
|
|
60
91
|
enumerate_source_files_with_patterns(root, exclude_patterns)
|
|
@@ -64,7 +95,7 @@ module ArchUnit
|
|
|
64
95
|
LoadPaths.resolve(root, explicit_paths: options.load_paths, source_files:)
|
|
65
96
|
end
|
|
66
97
|
edges = extraction_edges(root, source_files, load_paths, options, profile)
|
|
67
|
-
|
|
98
|
+
GraphAssembly.build(edges, profile)
|
|
68
99
|
end
|
|
69
100
|
private_class_method :extract_graph_uncached
|
|
70
101
|
|
|
@@ -77,33 +108,6 @@ module ArchUnit
|
|
|
77
108
|
end
|
|
78
109
|
private_class_method :extraction_edges
|
|
79
110
|
|
|
80
|
-
def graph_from_edges(edges, profile)
|
|
81
|
-
merged = ExtractionProfile.measure(profile, :edge_merge) do
|
|
82
|
-
merge_parallel_edges(edges)
|
|
83
|
-
end
|
|
84
|
-
profile&.increment(:merged_edges, merged.length)
|
|
85
|
-
Common::Extraction::Graph.new(merged)
|
|
86
|
-
end
|
|
87
|
-
private_class_method :graph_from_edges
|
|
88
|
-
|
|
89
|
-
def graph_cache_key(project_root, exclude_patterns:, options:)
|
|
90
|
-
[
|
|
91
|
-
project_root.to_s.tr('\\', '/').freeze,
|
|
92
|
-
exclude_patterns.sort.freeze,
|
|
93
|
-
graph_analysis_toggles(options)
|
|
94
|
-
].freeze
|
|
95
|
-
end
|
|
96
|
-
private_class_method :graph_cache_key
|
|
97
|
-
|
|
98
|
-
def graph_analysis_toggles(options)
|
|
99
|
-
options.class.members.filter_map do |member|
|
|
100
|
-
next if GRAPH_INDEPENDENT_CHECK_OPTIONS.include?(member)
|
|
101
|
-
|
|
102
|
-
[member, options.public_send(member)].freeze
|
|
103
|
-
end.freeze
|
|
104
|
-
end
|
|
105
|
-
private_class_method :graph_analysis_toggles
|
|
106
|
-
|
|
107
111
|
def graph_cache
|
|
108
112
|
@graph_cache ||= {}
|
|
109
113
|
end
|
|
@@ -115,19 +119,5 @@ module ArchUnit
|
|
|
115
119
|
end
|
|
116
120
|
end
|
|
117
121
|
private_class_method :self_edges
|
|
118
|
-
|
|
119
|
-
def merge_parallel_edges(edges)
|
|
120
|
-
edges.group_by { |edge| [edge.source, edge.target] }.values.map do |parallel_edges|
|
|
121
|
-
first = parallel_edges.first
|
|
122
|
-
Common::Extraction::Edge.new(
|
|
123
|
-
source: first.source,
|
|
124
|
-
target: first.target,
|
|
125
|
-
external: first.external,
|
|
126
|
-
import_kinds: parallel_edges.flat_map(&:import_kinds),
|
|
127
|
-
evidence: parallel_edges.flat_map(&:evidence)
|
|
128
|
-
)
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
private_class_method :merge_parallel_edges
|
|
132
122
|
end
|
|
133
123
|
end
|
|
@@ -17,13 +17,14 @@ module ArchUnit
|
|
|
17
17
|
|
|
18
18
|
module_function
|
|
19
19
|
|
|
20
|
-
def extract_imports(
|
|
21
|
-
source_file, project_root:, load_paths: nil, profile: nil, resolution_cache: nil
|
|
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
|
|
22
23
|
)
|
|
23
24
|
source_path = ExtractionPaths.source_file(source_file)
|
|
24
25
|
root = ExtractionPaths.project_root(project_root)
|
|
25
26
|
extract_imports_from(
|
|
26
|
-
source_path, project_root: root, load_paths:, profile:, resolution_cache:
|
|
27
|
+
source_path, project_root: root, load_paths:, profile:, resolution_cache:, rails_imports:
|
|
27
28
|
)
|
|
28
29
|
rescue UserError
|
|
29
30
|
raise
|
|
@@ -34,23 +35,19 @@ module ArchUnit
|
|
|
34
35
|
# Share the parsed AST with constant analysis.
|
|
35
36
|
def extract_imports_from( # rubocop:disable Metrics/ParameterLists
|
|
36
37
|
source_path, project_root:, load_paths: nil, profile: nil, resolution_cache: nil,
|
|
37
|
-
parse_result: nil
|
|
38
|
+
parse_result: nil, rails_imports: false, helper_load_paths: nil
|
|
38
39
|
)
|
|
39
|
-
resolver =
|
|
40
|
-
source_path:, project_root:, load_paths:, profile:,
|
|
40
|
+
resolver = ImportResolver.new(
|
|
41
|
+
source_path:, project_root:, load_paths:, profile:,
|
|
42
|
+
cache: resolution_cache, helper_load_paths:
|
|
41
43
|
)
|
|
42
44
|
parse_result ||= parse_source(source_path, profile)
|
|
43
|
-
resolve_imports(parsed_imports(parse_result, profile), resolver)
|
|
45
|
+
resolve_imports(parsed_imports(parse_result, profile, source_path:, rails_imports:), resolver)
|
|
44
46
|
rescue SystemCallError => e
|
|
45
47
|
raise TechnicalError, "could not parse Ruby source file: #{e.message}"
|
|
46
48
|
end
|
|
47
49
|
private_class_method :extract_imports_from
|
|
48
50
|
|
|
49
|
-
def build_import_resolver(**)
|
|
50
|
-
ImportResolver.new(**)
|
|
51
|
-
end
|
|
52
|
-
private_class_method :build_import_resolver
|
|
53
|
-
|
|
54
51
|
def resolve_imports(imports, resolver)
|
|
55
52
|
imports.map { |import| resolved_import(import, resolver) }.freeze
|
|
56
53
|
end
|
|
@@ -68,9 +65,9 @@ module ArchUnit
|
|
|
68
65
|
end
|
|
69
66
|
private_class_method :parse_source
|
|
70
67
|
|
|
71
|
-
def parsed_imports(parse_result, profile)
|
|
68
|
+
def parsed_imports(parse_result, profile, source_path:, rails_imports:)
|
|
72
69
|
ExtractionProfile.measure(profile, :import_extraction) do
|
|
73
|
-
visitor = ImportVisitor.new
|
|
70
|
+
visitor = ImportVisitor.new(source_path:, syntax_tree: parse_result.value, rails_imports:)
|
|
74
71
|
visitor.visit(parse_result.value)
|
|
75
72
|
profile&.increment(:unresolved_imports, visitor.unresolved_imports)
|
|
76
73
|
reject_ignored_imports(visitor.imports, parse_result.comments)
|
|
@@ -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
|
|
@@ -8,10 +8,13 @@ module ArchUnit
|
|
|
8
8
|
module Extraction
|
|
9
9
|
# Resolves one source file's imports and reuses context-independent feature lookups.
|
|
10
10
|
class ImportResolver
|
|
11
|
-
def initialize(
|
|
11
|
+
def initialize( # rubocop:disable Metrics/ParameterLists
|
|
12
|
+
source_path:, project_root:, load_paths:, profile: nil, cache: nil, helper_load_paths: nil
|
|
13
|
+
)
|
|
12
14
|
@source_path = source_path
|
|
13
15
|
@project_root = project_root
|
|
14
16
|
@load_paths = load_paths
|
|
17
|
+
@helper_load_paths = helper_load_paths || load_paths
|
|
15
18
|
@profile = profile
|
|
16
19
|
@cache = cache
|
|
17
20
|
end
|
|
@@ -40,11 +43,17 @@ module ArchUnit
|
|
|
40
43
|
source_file: @source_path,
|
|
41
44
|
project_root: @project_root,
|
|
42
45
|
import_kind:,
|
|
43
|
-
load_paths:
|
|
46
|
+
load_paths: resolution_paths(import_kind)
|
|
44
47
|
)
|
|
45
48
|
end
|
|
46
49
|
end
|
|
47
50
|
|
|
51
|
+
def resolution_paths(kind)
|
|
52
|
+
return @helper_load_paths if kind == Common::Extraction::ImportKind::REQUIRE_DEPENDENCY
|
|
53
|
+
|
|
54
|
+
@load_paths
|
|
55
|
+
end
|
|
56
|
+
|
|
48
57
|
def cache_key(module_name, import_kind)
|
|
49
58
|
source_context = if import_kind == Common::Extraction::ImportKind::REQUIRE_RELATIVE
|
|
50
59
|
File.dirname(@source_path.to_s)
|