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.
- checksums.yaml +4 -4
- data/API.md +498 -0
- data/CHANGELOG.md +85 -0
- data/CODE_OF_CONDUCT.md +40 -0
- data/CONTRIBUTING.md +75 -0
- data/GUIDE.md +804 -0
- data/README.md +224 -289
- data/SECURITY.md +64 -0
- data/SUPPORT.md +23 -0
- data/assets/logo-rounded.png +0 -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/dependency_evidence.rb +44 -0
- data/lib/archunit/common/extraction/edge.rb +7 -2
- data/lib/archunit/common/extraction/import_kind.rb +4 -1
- data/lib/archunit/common/extraction/parse_diagnostic.rb +23 -0
- data/lib/archunit/common/extraction/source_exclusions.rb +43 -0
- data/lib/archunit/common/fluentapi/check_options.rb +53 -2
- data/lib/archunit/common/fluentapi/checkable.rb +25 -1
- data/lib/archunit/common/logging/check_logger.rb +8 -0
- data/lib/archunit/common/logging/inspection.rb +59 -0
- data/lib/archunit/common/logging/logging_options.rb +5 -0
- data/lib/archunit/common/pattern.rb +18 -8
- data/lib/archunit/common/projection/cycles/johnson_cycles.rb +35 -19
- data/lib/archunit/common/projection/cycles/tarjan_scc.rb +30 -10
- data/lib/archunit/error/source_parse_error.rb +38 -0
- 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/arch_ignore.rb +91 -0
- data/lib/archunit/extraction/conservative_local_flow.rb +100 -0
- data/lib/archunit/extraction/constant_index.rb +110 -0
- data/lib/archunit/extraction/constant_visitor.rb +137 -0
- data/lib/archunit/extraction/dependency_context.rb +30 -0
- data/lib/archunit/extraction/discover_gemspec_load_paths.rb +96 -0
- data/lib/archunit/extraction/enumerate_source_files.rb +36 -9
- data/lib/archunit/extraction/extract_dependencies.rb +74 -15
- data/lib/archunit/extraction/extract_graph.rb +74 -40
- data/lib/archunit/extraction/extract_imports.rb +57 -124
- data/lib/archunit/extraction/extraction_profile.rb +105 -0
- 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 +65 -0
- data/lib/archunit/extraction/import_source_policy.rb +123 -0
- data/lib/archunit/extraction/import_visitor.rb +89 -0
- data/lib/archunit/extraction/load_arguments.rb +34 -0
- data/lib/archunit/extraction/local_string_tracker.rb +81 -0
- data/lib/archunit/extraction/resolve_import.rb +5 -3
- data/lib/archunit/extraction/resolve_load_paths.rb +98 -0
- data/lib/archunit/extraction/resolved_import.rb +5 -2
- data/lib/archunit/extraction/source_analysis.rb +26 -0
- data/lib/archunit/extraction/static_import_path.rb +111 -0
- data/lib/archunit/extraction/validate_extraction_paths.rb +37 -0
- data/lib/archunit/files/extraction/extract_file_info.rb +14 -5
- data/lib/archunit/files/extraction/file_info.rb +1 -2
- data/lib/archunit/files/fluentapi/depend_on_external_module_condition.rb +2 -0
- data/lib/archunit/files/fluentapi/depend_on_external_module_condition_builder.rb +2 -0
- data/lib/archunit/files/fluentapi/depend_on_file_condition.rb +6 -0
- data/lib/archunit/files/fluentapi/depend_on_file_condition_builder.rb +6 -0
- data/lib/archunit/files/fluentapi/file_condition_builder.rb +12 -0
- data/lib/archunit/files/fluentapi/file_rule_support.rb +8 -1
- data/lib/archunit/files/fluentapi/files.rb +6 -0
- data/lib/archunit/files/fluentapi/match_pattern_file_condition_builder.rb +14 -0
- data/lib/archunit/files/fluentapi/positive_match_pattern_file_condition_builder.rb +2 -0
- data/lib/archunit/graph/fluentapi/graph.rb +5 -0
- data/lib/archunit/graph/fluentapi/project_graph_builder.rb +44 -0
- data/lib/archunit/graph/projection/aggregate_edges.rb +4 -2
- data/lib/archunit/graph/projection/graph_query_options.rb +1 -2
- data/lib/archunit/graph/projection/graph_report_edge.rb +12 -2
- data/lib/archunit/graph/rendering/json_renderer.rb +3 -1
- data/lib/archunit/layers/fluentapi/architecture_preset.rb +49 -0
- data/lib/archunit/layers/fluentapi/layer_definition_builder.rb +4 -0
- data/lib/archunit/layers/fluentapi/layer_dependency_rule_builder.rb +4 -0
- data/lib/archunit/layers/fluentapi/layered_architecture.rb +5 -0
- data/lib/archunit/layers/fluentapi/layers.rb +5 -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 +2 -3
- data/lib/archunit/metrics/extraction/extract_project_info.rb +14 -10
- data/lib/archunit/metrics/extraction/metric_info.rb +2 -4
- data/lib/archunit/metrics/extraction/source_metrics_visitor.rb +2 -4
- data/lib/archunit/metrics/fluentapi/custom_metric_builder.rb +2 -0
- data/lib/archunit/metrics/fluentapi/custom_metric_condition.rb +4 -1
- data/lib/archunit/metrics/fluentapi/logged_metric.rb +23 -0
- data/lib/archunit/metrics/fluentapi/metric_predicate_condition.rb +4 -1
- data/lib/archunit/metrics/fluentapi/metric_report_builder.rb +6 -3
- data/lib/archunit/metrics/fluentapi/metric_selection.rb +18 -2
- data/lib/archunit/metrics/fluentapi/metric_threshold_condition.rb +2 -1
- data/lib/archunit/metrics/fluentapi/metrics.rb +5 -0
- data/lib/archunit/metrics/fluentapi/metrics_builder.rb +18 -3
- data/lib/archunit/metrics/reporting/metrics_export_options.rb +6 -2
- data/lib/archunit/slices/fluentapi/diagram_slice_condition.rb +1 -0
- data/lib/archunit/slices/fluentapi/forbidden_slice_dependency_condition.rb +1 -0
- data/lib/archunit/slices/fluentapi/negative_slice_condition_builder.rb +2 -0
- data/lib/archunit/slices/fluentapi/positive_slice_condition_builder.rb +8 -0
- data/lib/archunit/slices/fluentapi/slice_scope_builder.rb +12 -0
- data/lib/archunit/slices/fluentapi/slices.rb +5 -0
- data/lib/archunit/testing/assert_passes.rb +4 -0
- data/lib/archunit/testing/result_factory.rb +16 -4
- data/lib/archunit/testing/rspec_adapter.rb +1 -2
- data/lib/archunit/testing.rb +7 -3
- data/lib/archunit/version.rb +1 -1
- data/lib/archunit.rb +11 -0
- metadata +67 -9
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ArchUnit
|
|
4
|
+
module CLI
|
|
5
|
+
# Accepted debt remains visible, including stale entries after an architectural improvement.
|
|
6
|
+
# @api private
|
|
7
|
+
module BaselineText
|
|
8
|
+
def self.write(baseline, out)
|
|
9
|
+
return unless baseline
|
|
10
|
+
|
|
11
|
+
return created(baseline, out) if baseline[:status] == 'created'
|
|
12
|
+
|
|
13
|
+
checked(baseline, out)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.created(baseline, out)
|
|
17
|
+
out.puts("Baseline created: #{baseline[:path]} (#{baseline[:accepted_count]} violations).")
|
|
18
|
+
out.puts('Review accepted debt before committing; this is not a clean architecture result.')
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.checked(baseline, out)
|
|
22
|
+
out.puts("Baseline: #{baseline[:accepted_count]} accepted, #{baseline[:new_count]} new, " \
|
|
23
|
+
"#{baseline[:stale_count]} stale. #{baseline[:path]}")
|
|
24
|
+
baseline[:stale_entries].each do |entry|
|
|
25
|
+
out.puts(" Stale: #{entry['rule']} — #{entry['type']} (#{entry['count']})")
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
private_class_method :created, :checked
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../common/fluentapi/checkable'
|
|
4
|
+
require_relative '../files/fluentapi/files'
|
|
5
|
+
require_relative '../layers/fluentapi/layers'
|
|
6
|
+
require_relative '../layers/fluentapi/architecture_preset'
|
|
7
|
+
require_relative '../slices/fluentapi/slices'
|
|
8
|
+
require_relative '../metrics/fluentapi/metrics'
|
|
9
|
+
|
|
10
|
+
module ArchUnit
|
|
11
|
+
module CLI
|
|
12
|
+
# Trusted executable Ruby configuration. This context is deliberately not a sandbox.
|
|
13
|
+
# @api private
|
|
14
|
+
class Configuration
|
|
15
|
+
Rule = Data.define(:name, :checkable)
|
|
16
|
+
STARTER = <<~RUBY
|
|
17
|
+
# frozen_string_literal: true
|
|
18
|
+
|
|
19
|
+
# Trusted Ruby configuration: review this file before running archunit.
|
|
20
|
+
# Customize the scope and add named rules for your project's boundaries.
|
|
21
|
+
rule 'Library files have no dependency cycles' do
|
|
22
|
+
project_files.in_path('lib/**/*.rb').should.have_no_cycles
|
|
23
|
+
end
|
|
24
|
+
RUBY
|
|
25
|
+
|
|
26
|
+
attr_reader :rules
|
|
27
|
+
|
|
28
|
+
def initialize(project_root)
|
|
29
|
+
@project_root = project_root
|
|
30
|
+
@rules = []
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def load(path)
|
|
34
|
+
instance_eval(File.read(path), path, 1)
|
|
35
|
+
raise ArgumentError, 'Configuration must declare at least one rule' if rules.empty?
|
|
36
|
+
|
|
37
|
+
rules.freeze
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def rule(name, &block)
|
|
41
|
+
name = validate_name(name, block)
|
|
42
|
+
checkable = instance_eval(&block)
|
|
43
|
+
validate_checkable(checkable, name)
|
|
44
|
+
rules << Rule.new(name: name.freeze, checkable:)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def project_files
|
|
48
|
+
Files::FluentApi.project_files(@project_root)
|
|
49
|
+
end
|
|
50
|
+
alias files project_files
|
|
51
|
+
|
|
52
|
+
def project_layers
|
|
53
|
+
Layers::FluentApi.project_layers(@project_root)
|
|
54
|
+
end
|
|
55
|
+
alias layers project_layers
|
|
56
|
+
|
|
57
|
+
def architecture_preset(name, options = nil)
|
|
58
|
+
options = Layers::FluentApi::PresetOptions.resolve(options)
|
|
59
|
+
options = options.with(project_locator: options.project_locator || @project_root)
|
|
60
|
+
Layers::FluentApi::ArchitecturePreset.build(name, options)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def project_slices
|
|
64
|
+
Slices::FluentApi.project_slices(@project_root)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def metrics
|
|
68
|
+
Metrics::FluentApi.metrics(@project_root)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def validate_name(name, block)
|
|
74
|
+
unless name.is_a?(String) && !name.strip.empty? && block
|
|
75
|
+
raise ArgumentError, 'rule requires a non-empty name and a block returning a Checkable'
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
name = name.strip
|
|
79
|
+
if rules.any? { |item| item.name == name }
|
|
80
|
+
raise ArgumentError, "Duplicate rule name: #{name}"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
name
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def validate_checkable(checkable, name)
|
|
87
|
+
return if checkable.is_a?(Common::FluentApi::Checkable)
|
|
88
|
+
|
|
89
|
+
raise ArgumentError, "Rule '#{name}' must return a completed Checkable rule"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'optparse'
|
|
4
|
+
require_relative '../common/extraction/source_exclusions'
|
|
5
|
+
require_relative 'baseline_options'
|
|
6
|
+
|
|
7
|
+
module ArchUnit
|
|
8
|
+
module CLI
|
|
9
|
+
# Internal command parsing; does not discover or execute project code.
|
|
10
|
+
# @api private
|
|
11
|
+
class Options
|
|
12
|
+
include BaselineOptions
|
|
13
|
+
|
|
14
|
+
attr_reader :command, :project_root, :config_path, :format, :dependency_analysis, :parser,
|
|
15
|
+
:rails_imports, :load_paths, :source_exclusions
|
|
16
|
+
|
|
17
|
+
def initialize(arguments, cwd:)
|
|
18
|
+
@arguments = arguments.dup
|
|
19
|
+
@cwd = cwd
|
|
20
|
+
@command = @arguments.first&.start_with?('-') ? 'check' : (@arguments.shift || 'check')
|
|
21
|
+
@project_root = File.expand_path(cwd)
|
|
22
|
+
@format = json_requested? ? 'json' : 'text'
|
|
23
|
+
@dependency_analysis = 'imports'
|
|
24
|
+
@rails_imports = false
|
|
25
|
+
@load_paths = []
|
|
26
|
+
@source_exclusions = []
|
|
27
|
+
@parser = build_parser
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def parse!
|
|
31
|
+
parser.parse!(@arguments)
|
|
32
|
+
validate_arguments
|
|
33
|
+
return self if %w[help version].include?(command)
|
|
34
|
+
|
|
35
|
+
unless File.directory?(project_root)
|
|
36
|
+
raise ArgumentError, "Project directory does not exist: #{project_root}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
@config_path ||= File.join(project_root, '.archunit.rb')
|
|
40
|
+
@load_paths.freeze
|
|
41
|
+
@source_exclusions = Common::Extraction::SourceExclusions.normalize(@source_exclusions)
|
|
42
|
+
self
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def validate_arguments
|
|
48
|
+
validate_baseline_options
|
|
49
|
+
unless @arguments.empty?
|
|
50
|
+
raise OptionParser::InvalidArgument, "unexpected arguments: #{@arguments.join(' ')}"
|
|
51
|
+
end
|
|
52
|
+
return if %w[init check explain baseline help version].include?(command)
|
|
53
|
+
|
|
54
|
+
raise OptionParser::InvalidArgument, "unknown command: #{command}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def json_requested?
|
|
58
|
+
@arguments.include?('--format=json') ||
|
|
59
|
+
@arguments.each_cons(2).any? { |flag, value| flag == '--format' && value == 'json' }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def build_parser
|
|
63
|
+
OptionParser.new do |options|
|
|
64
|
+
options.banner = 'Usage: archunit [init|check|explain|baseline] [options]'
|
|
65
|
+
project_options(options)
|
|
66
|
+
load_path_option(options)
|
|
67
|
+
source_exclusion_option(options)
|
|
68
|
+
baseline_options(options)
|
|
69
|
+
output_options(options)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def project_options(options)
|
|
74
|
+
options.on('--project DIRECTORY', 'Project root (default: working directory)') do |value|
|
|
75
|
+
@project_root = File.expand_path(value, @cwd)
|
|
76
|
+
end
|
|
77
|
+
options.on('--config FILE',
|
|
78
|
+
'Trusted Ruby config (default: PROJECT/.archunit.rb)') do |value|
|
|
79
|
+
@config_path = File.expand_path(value, @cwd)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def load_path_option(options)
|
|
84
|
+
options.on('--load-path DIRECTORY',
|
|
85
|
+
'Additional project-local source root (repeatable)') do |value|
|
|
86
|
+
@load_paths << value
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def output_options(options)
|
|
91
|
+
options.on('--format FORMAT', %w[text json], 'Output format: text or json') do |value|
|
|
92
|
+
@format = value
|
|
93
|
+
end
|
|
94
|
+
analysis_options(options)
|
|
95
|
+
options.on('-h', '--help', 'Show help without loading config') { @command = 'help' }
|
|
96
|
+
options.on('-v', '--version', 'Show the installed version') { @command = 'version' }
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def source_exclusion_option(options)
|
|
100
|
+
options.on('--exclude-source GLOB',
|
|
101
|
+
'Additional source exclusion (repeatable; no target edits)') do |value|
|
|
102
|
+
@source_exclusions << value
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def analysis_options(options)
|
|
107
|
+
options.on('--dependency-analysis MODE', %w[imports imports_and_constants],
|
|
108
|
+
'Static analysis mode (default: imports)') do |value|
|
|
109
|
+
@dependency_analysis = value
|
|
110
|
+
end
|
|
111
|
+
options.on('--[no-]rails-imports', 'Opt into static require_dependency imports') do |value|
|
|
112
|
+
@rails_imports = value
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require_relative '../version'
|
|
5
|
+
require_relative '../testing/violation_factory'
|
|
6
|
+
require_relative '../error/source_parse_error'
|
|
7
|
+
require_relative 'analysis_report'
|
|
8
|
+
require_relative 'analysis_text'
|
|
9
|
+
require_relative 'baseline_text'
|
|
10
|
+
|
|
11
|
+
module ArchUnit
|
|
12
|
+
module CLI
|
|
13
|
+
# Internal presentation adapter reusing the shared violation vocabulary.
|
|
14
|
+
# @api private
|
|
15
|
+
class Report
|
|
16
|
+
PARSE_GUIDANCE = [
|
|
17
|
+
'Fix included Ruby syntax errors. If a file is a generator/ERB template rather than Ruby,',
|
|
18
|
+
'exclude only that known non-source input with --exclude-source GLOB or',
|
|
19
|
+
'CheckOptions.new(source_exclusions: [...]); selectors do not narrow source discovery.'
|
|
20
|
+
].freeze
|
|
21
|
+
private_constant :PARSE_GUIDANCE
|
|
22
|
+
|
|
23
|
+
attr_reader :payload
|
|
24
|
+
|
|
25
|
+
def initialize(options)
|
|
26
|
+
@payload = {
|
|
27
|
+
schema_version: 1, archunit_version: ArchUnit::VERSION, command: options.command,
|
|
28
|
+
project_root: options.project_root, config_path: options.config_path,
|
|
29
|
+
dependency_analysis: options.dependency_analysis,
|
|
30
|
+
rails_imports: options.rails_imports, load_paths: options.load_paths,
|
|
31
|
+
source_exclusions: options.source_exclusions,
|
|
32
|
+
status: 'error', rules: [], analysis: AnalysisReport.new.to_h
|
|
33
|
+
}
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def add_rule(rule, violations: nil, analysis: nil)
|
|
37
|
+
payload[:rules] << {
|
|
38
|
+
name: rule.name, rationale: rule.checkable.rationale, status: rule_status(violations),
|
|
39
|
+
analysis: analysis || { status: 'unavailable', scope_ids: [] },
|
|
40
|
+
violations: (violations || []).map { |violation| violation_payload(violation) }
|
|
41
|
+
}
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def finish(status)
|
|
45
|
+
payload[:status] = status
|
|
46
|
+
payload[:summary] = {
|
|
47
|
+
rule_count: payload[:rules].length,
|
|
48
|
+
violation_count: payload[:rules].sum { |rule| rule[:violations].length }
|
|
49
|
+
}
|
|
50
|
+
self
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def fail_with(error, rule_name: nil)
|
|
54
|
+
payload[:error] = { type: error.class.name, message: error.message, rule_name: }
|
|
55
|
+
if error.is_a?(SourceParseError)
|
|
56
|
+
payload[:error][:diagnostics] = error.diagnostics.map(&:to_h)
|
|
57
|
+
payload[:error][:guidance] = PARSE_GUIDANCE
|
|
58
|
+
end
|
|
59
|
+
finish('error')
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def write(out:, err:, format:)
|
|
63
|
+
return out.puts(JSON.pretty_generate(payload)) if format == 'json'
|
|
64
|
+
return write_error(err) if payload[:error]
|
|
65
|
+
|
|
66
|
+
write_text(out)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def write_error(err)
|
|
72
|
+
err.puts("ArchUnit error: #{payload[:error][:message]}")
|
|
73
|
+
payload[:error].fetch(:guidance, []).each { |line| err.puts(line) }
|
|
74
|
+
AnalysisText.write(payload[:analysis], err)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def rule_status(violations)
|
|
78
|
+
return 'not_checked' unless violations
|
|
79
|
+
|
|
80
|
+
violations.empty? ? 'passed' : 'failed'
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def violation_payload(violation)
|
|
84
|
+
formatted = Testing::ViolationFactory.from_violation(violation)
|
|
85
|
+
{ type: violation.class.name, message: formatted.message, details: formatted.details }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def write_text(out)
|
|
89
|
+
write_header(out)
|
|
90
|
+
write_notice(out)
|
|
91
|
+
payload[:rules].each { |rule| write_rule(rule, out) }
|
|
92
|
+
AnalysisText.write(payload[:analysis], out)
|
|
93
|
+
BaselineText.write(payload[:baseline], out)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def write_header(out)
|
|
97
|
+
out.puts("ArchUnit #{ArchUnit::VERSION} — #{payload[:status]} " \
|
|
98
|
+
"(#{payload[:dependency_analysis]})")
|
|
99
|
+
out.puts("Project: #{payload[:project_root]}\nConfiguration: #{payload[:config_path]}")
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def write_notice(out)
|
|
103
|
+
out.puts('Rails require_dependency imports: enabled') if payload[:rails_imports]
|
|
104
|
+
case payload[:status]
|
|
105
|
+
when 'configured'
|
|
106
|
+
out.puts('Rules are configured, not evaluated.')
|
|
107
|
+
when 'initialized'
|
|
108
|
+
out.puts('Review and customize the trusted Ruby configuration before checking.')
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def write_rule(rule, out)
|
|
113
|
+
out.puts("[#{rule[:status]}] #{rule[:name]}")
|
|
114
|
+
out.puts(" Because: #{rule[:rationale]}") if rule[:rationale]
|
|
115
|
+
rule[:violations].each do |violation|
|
|
116
|
+
out.puts(" #{violation[:message]}\n #{violation[:details]}")
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'options'
|
|
4
|
+
require_relative 'configuration'
|
|
5
|
+
require_relative 'report'
|
|
6
|
+
require_relative '../extraction/analysis_observer'
|
|
7
|
+
require_relative 'baseline_run'
|
|
8
|
+
|
|
9
|
+
module ArchUnit
|
|
10
|
+
module CLI
|
|
11
|
+
# Internal command execution; technical errors never become successful rule checks.
|
|
12
|
+
# @api private
|
|
13
|
+
class Runner
|
|
14
|
+
def initialize(arguments, out:, err:, cwd:)
|
|
15
|
+
@options = Options.new(arguments, cwd:)
|
|
16
|
+
@out = out
|
|
17
|
+
@err = err
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def run
|
|
21
|
+
@options.parse!
|
|
22
|
+
return information if %w[help version].include?(@options.command)
|
|
23
|
+
|
|
24
|
+
@report = Report.new(@options)
|
|
25
|
+
status = execute
|
|
26
|
+
@report.write(out: @out, err: @err, format: @options.format)
|
|
27
|
+
status
|
|
28
|
+
rescue StandardError, SyntaxError, LoadError, NotImplementedError, SystemExit => e
|
|
29
|
+
# An accidental exit in trusted configuration is not proof that rules passed.
|
|
30
|
+
report_error(e)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def report_error(error)
|
|
36
|
+
@report ||= Report.new(@options)
|
|
37
|
+
@report.fail_with(error, rule_name: @current_rule&.name)
|
|
38
|
+
@report.write(out: @out, err: @err, format: @options.format)
|
|
39
|
+
2
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def information
|
|
43
|
+
@out.puts(@options.command == 'help' ? @options.parser.to_s : ArchUnit::VERSION)
|
|
44
|
+
0
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def execute
|
|
48
|
+
return initialize_configuration if @options.command == 'init'
|
|
49
|
+
|
|
50
|
+
rules = Configuration.new(@options.project_root).load(@options.config_path)
|
|
51
|
+
return explain(rules) if @options.command == 'explain'
|
|
52
|
+
|
|
53
|
+
check(rules)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def initialize_configuration
|
|
57
|
+
ensure_missing_configuration
|
|
58
|
+
File.open(@options.config_path, File::WRONLY | File::CREAT | File::EXCL) do |file|
|
|
59
|
+
file.write(Configuration::STARTER)
|
|
60
|
+
end
|
|
61
|
+
@report.finish('initialized')
|
|
62
|
+
0
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def ensure_missing_configuration
|
|
66
|
+
# Windows exclusive creation may follow a dangling symlink; inspect the entry itself.
|
|
67
|
+
File.lstat(@options.config_path)
|
|
68
|
+
raise Errno::EEXIST, @options.config_path
|
|
69
|
+
rescue Errno::ENOENT
|
|
70
|
+
nil
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def explain(rules)
|
|
74
|
+
rules.each { |rule| @report.add_rule(rule) }
|
|
75
|
+
@report.finish('configured')
|
|
76
|
+
0
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def check(rules)
|
|
80
|
+
Extraction.clear_graph_cache
|
|
81
|
+
options = Common::FluentApi::CheckOptions.new(
|
|
82
|
+
dependency_analysis: @options.dependency_analysis.to_sym,
|
|
83
|
+
rails_imports: @options.rails_imports, load_paths: @options.load_paths,
|
|
84
|
+
source_exclusions: @options.source_exclusions
|
|
85
|
+
)
|
|
86
|
+
check_rules(rules, options)
|
|
87
|
+
BaselineRun.new(@options).finish(@report, @evaluations)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def check_rules(rules, options)
|
|
91
|
+
analysis = AnalysisReport.new
|
|
92
|
+
@evaluations = []
|
|
93
|
+
Extraction::AnalysisObserver.capture(analysis) do
|
|
94
|
+
rules.each { |rule| check_rule(rule, options, analysis) }
|
|
95
|
+
end
|
|
96
|
+
ensure
|
|
97
|
+
@report.payload[:analysis] = analysis.to_h
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def check_rule(rule, options, analysis)
|
|
101
|
+
@current_rule = rule
|
|
102
|
+
violations = analysis.for_rule(rule.name) { rule.checkable.check(options) }
|
|
103
|
+
@report.add_rule(rule, violations:, analysis: analysis.for_name(rule.name))
|
|
104
|
+
@evaluations << [rule.name, violations]
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
data/lib/archunit/cli.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'cli/runner'
|
|
4
|
+
|
|
5
|
+
module ArchUnit
|
|
6
|
+
# Command-line composition; load separately from the test-library entry point.
|
|
7
|
+
# Implementation classes are private to the executable, not the fluent public API.
|
|
8
|
+
module CLI
|
|
9
|
+
# Runs a command without terminating the embedding process.
|
|
10
|
+
# @return [Integer] 0 for success, 1 for violations, 2 for command/analysis errors
|
|
11
|
+
def self.run(arguments = ARGV, out: $stdout, err: $stderr, cwd: Dir.pwd)
|
|
12
|
+
Runner.new(arguments, out:, err:, cwd:).run
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'import_kind'
|
|
4
|
+
|
|
5
|
+
module ArchUnit
|
|
6
|
+
module Common
|
|
7
|
+
module Extraction
|
|
8
|
+
# Source-level support for a file dependency, without implying runtime execution.
|
|
9
|
+
DependencyEvidence = Data.define(
|
|
10
|
+
:source, :kind, :name, :line_number, :end_line, :target_line_number
|
|
11
|
+
) do
|
|
12
|
+
# Keywords mirror this immutable source span.
|
|
13
|
+
def initialize( # rubocop:disable Metrics/ParameterLists
|
|
14
|
+
source:, kind:, name:, line_number:, end_line: line_number, target_line_number: nil
|
|
15
|
+
)
|
|
16
|
+
validate_identity(source, kind, name)
|
|
17
|
+
validate_lines(line_number, end_line, target_line_number)
|
|
18
|
+
source = source.tr('\\', '/').freeze
|
|
19
|
+
name = name.dup.freeze
|
|
20
|
+
super
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def validate_identity(source, kind, name)
|
|
26
|
+
return if source.is_a?(String) && name.is_a?(String) && ImportKind.valid?(kind)
|
|
27
|
+
|
|
28
|
+
raise ArgumentError, 'evidence requires a source, name, and valid dependency kind'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def validate_lines(first, last, target)
|
|
32
|
+
valid = positive_line?(first) && positive_line?(last) && last >= first
|
|
33
|
+
return if valid && (target.nil? || positive_line?(target))
|
|
34
|
+
|
|
35
|
+
raise ArgumentError, 'evidence requires valid source line numbers'
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def positive_line?(value)
|
|
39
|
+
value.is_a?(Integer) && value.positive?
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'import_kind'
|
|
4
|
+
require_relative 'dependency_evidence'
|
|
4
5
|
|
|
5
6
|
module ArchUnit
|
|
6
7
|
module Common
|
|
7
8
|
module Extraction
|
|
8
|
-
Edge = Data.define(:source, :target, :external, :import_kinds) do
|
|
9
|
-
def initialize(source:, target:, external:, import_kinds: [])
|
|
9
|
+
Edge = Data.define(:source, :target, :external, :import_kinds, :evidence) do
|
|
10
|
+
def initialize(source:, target:, external:, import_kinds: [], evidence: [])
|
|
10
11
|
source = immutable_identifier(source, :source)
|
|
11
12
|
target = immutable_identifier(target, :target)
|
|
12
13
|
validate_external(external)
|
|
13
14
|
import_kinds = immutable_import_kinds(import_kinds)
|
|
15
|
+
evidence = Array(evidence).uniq.freeze
|
|
16
|
+
unless evidence.all?(DependencyEvidence)
|
|
17
|
+
raise ArgumentError, 'evidence must contain DependencyEvidence values'
|
|
18
|
+
end
|
|
14
19
|
|
|
15
20
|
super
|
|
16
21
|
end
|
|
@@ -9,8 +9,11 @@ module ArchUnit
|
|
|
9
9
|
REQUIRE_RELATIVE = :require_relative
|
|
10
10
|
AUTOLOAD = :autoload
|
|
11
11
|
LOAD = :load
|
|
12
|
+
REQUIRE_DEPENDENCY = :require_dependency
|
|
13
|
+
CONSTANT_REFERENCE = :constant_reference
|
|
12
14
|
|
|
13
|
-
ALL = [REQUIRE, REQUIRE_RELATIVE, AUTOLOAD, LOAD
|
|
15
|
+
ALL = [REQUIRE, REQUIRE_RELATIVE, AUTOLOAD, LOAD,
|
|
16
|
+
REQUIRE_DEPENDENCY, CONSTANT_REFERENCE].freeze
|
|
14
17
|
|
|
15
18
|
def self.valid?(value)
|
|
16
19
|
ALL.include?(value)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ArchUnit
|
|
4
|
+
module Common
|
|
5
|
+
module Extraction
|
|
6
|
+
# Immutable parser failure location; line and column are both one-based.
|
|
7
|
+
ParseDiagnostic = Data.define(:path, :line, :column, :message) do
|
|
8
|
+
def initialize(path:, line:, column:, message:)
|
|
9
|
+
unless [path, message].all? { |value| value.is_a?(String) && !value.empty? }
|
|
10
|
+
raise ArgumentError, 'parse diagnostic requires a non-empty path and message'
|
|
11
|
+
end
|
|
12
|
+
unless [line, column].all? { |value| value.is_a?(Integer) && value.positive? }
|
|
13
|
+
raise ArgumentError, 'parse diagnostic coordinates must be positive integers'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
path = path.tr('\\', '/').freeze
|
|
17
|
+
message = message.dup.freeze
|
|
18
|
+
super
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ArchUnit
|
|
4
|
+
module Common
|
|
5
|
+
module Extraction
|
|
6
|
+
# Immutable, additive caller-owned source globs; never reads target configuration.
|
|
7
|
+
module SourceExclusions
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def normalize(value)
|
|
11
|
+
unless value.is_a?(Array) && value.all?(String)
|
|
12
|
+
raise ArgumentError,
|
|
13
|
+
'source_exclusions must be an Array of project-relative glob Strings'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
value.map { |pattern| normalize_pattern(pattern) }.uniq.sort.freeze
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def normalize_pattern(pattern)
|
|
20
|
+
pattern = pattern.strip.tr('\\', '/').delete_prefix('./')
|
|
21
|
+
validate_pattern(pattern)
|
|
22
|
+
return pattern.freeze unless pattern.end_with?('/')
|
|
23
|
+
|
|
24
|
+
directory = pattern.delete_suffix('/')
|
|
25
|
+
prefix = directory.include?('/') ? '' : '**/'
|
|
26
|
+
"#{prefix}#{directory}/**".freeze
|
|
27
|
+
end
|
|
28
|
+
private_class_method :normalize_pattern
|
|
29
|
+
|
|
30
|
+
def validate_pattern(pattern)
|
|
31
|
+
invalid = pattern.empty? || pattern == '/' || pattern.start_with?('!', '//') ||
|
|
32
|
+
pattern.match?(/[\0\r\n]|\A[a-z]:/i) || pattern.split('/').include?('..')
|
|
33
|
+
return unless invalid
|
|
34
|
+
|
|
35
|
+
raise ArgumentError,
|
|
36
|
+
'source_exclusions require non-empty project-relative globs without negation, ' \
|
|
37
|
+
'NUL/newlines, drive paths or parent traversal'
|
|
38
|
+
end
|
|
39
|
+
private_class_method :validate_pattern
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|