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,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
|
|
@@ -9,9 +9,11 @@ module ArchUnit
|
|
|
9
9
|
REQUIRE_RELATIVE = :require_relative
|
|
10
10
|
AUTOLOAD = :autoload
|
|
11
11
|
LOAD = :load
|
|
12
|
+
REQUIRE_DEPENDENCY = :require_dependency
|
|
12
13
|
CONSTANT_REFERENCE = :constant_reference
|
|
13
14
|
|
|
14
|
-
ALL = [REQUIRE, REQUIRE_RELATIVE, AUTOLOAD, LOAD,
|
|
15
|
+
ALL = [REQUIRE, REQUIRE_RELATIVE, AUTOLOAD, LOAD,
|
|
16
|
+
REQUIRE_DEPENDENCY, CONSTANT_REFERENCE].freeze
|
|
15
17
|
|
|
16
18
|
def self.valid?(value)
|
|
17
19
|
ALL.include?(value)
|
|
@@ -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
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative '../logging/logging_options'
|
|
4
|
+
require_relative '../extraction/source_exclusions'
|
|
4
5
|
|
|
5
6
|
module ArchUnit
|
|
6
7
|
module Common
|
|
7
8
|
module FluentApi
|
|
8
9
|
# Immutable options shared by every terminal rule check.
|
|
9
10
|
CheckOptions = Data.define(
|
|
10
|
-
:allow_empty_tests, :logging, :clear_cache, :load_paths, :dependency_analysis
|
|
11
|
+
:allow_empty_tests, :logging, :clear_cache, :load_paths, :dependency_analysis,
|
|
12
|
+
:rails_imports, :source_exclusions
|
|
11
13
|
) do
|
|
12
14
|
# Normalizes nil or an existing options value for terminal execution.
|
|
13
15
|
# @param value [CheckOptions, nil]
|
|
@@ -24,14 +26,18 @@ module ArchUnit
|
|
|
24
26
|
# @param clear_cache [Boolean] rebuild the dependency graph for this check
|
|
25
27
|
# @param load_paths [Array<String, Pathname>] additional project-contained source roots
|
|
26
28
|
# @param dependency_analysis [Symbol] :imports or opt-in :imports_and_constants
|
|
27
|
-
|
|
29
|
+
# @param rails_imports [Boolean] opt into static Rails require_dependency syntax
|
|
30
|
+
# @param source_exclusions [Array<String>] additional project-relative source globs
|
|
31
|
+
def initialize( # rubocop:disable Metrics/ParameterLists
|
|
28
32
|
allow_empty_tests: false, logging: nil, clear_cache: false, load_paths: [],
|
|
29
|
-
dependency_analysis: :imports
|
|
33
|
+
dependency_analysis: :imports, rails_imports: false, source_exclusions: []
|
|
30
34
|
)
|
|
31
35
|
validate_boolean(allow_empty_tests, :allow_empty_tests)
|
|
32
36
|
validate_logging(logging)
|
|
33
37
|
validate_boolean(clear_cache, :clear_cache)
|
|
38
|
+
validate_boolean(rails_imports, :rails_imports)
|
|
34
39
|
load_paths = immutable_load_paths(load_paths)
|
|
40
|
+
source_exclusions = Common::Extraction::SourceExclusions.normalize(source_exclusions)
|
|
35
41
|
unless %i[imports imports_and_constants].include?(dependency_analysis)
|
|
36
42
|
raise ArgumentError, 'dependency_analysis must be :imports or :imports_and_constants'
|
|
37
43
|
end
|
|
@@ -53,6 +59,10 @@ module ArchUnit
|
|
|
53
59
|
dependency_analysis == :imports_and_constants
|
|
54
60
|
end
|
|
55
61
|
|
|
62
|
+
def rails_imports?
|
|
63
|
+
rails_imports
|
|
64
|
+
end
|
|
65
|
+
|
|
56
66
|
private
|
|
57
67
|
|
|
58
68
|
def validate_boolean(value, attribute)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ArchUnit
|
|
4
|
+
module Extraction
|
|
5
|
+
# Scoped, fiber-local diagnostics. Nothing is retained after capture ends.
|
|
6
|
+
# @api private
|
|
7
|
+
module AnalysisObserver
|
|
8
|
+
KEY = :archunit_analysis_observer
|
|
9
|
+
private_constant :KEY
|
|
10
|
+
|
|
11
|
+
def self.capture(observer)
|
|
12
|
+
previous = Thread.current[KEY]
|
|
13
|
+
Thread.current[KEY] = observer
|
|
14
|
+
yield
|
|
15
|
+
ensure
|
|
16
|
+
Thread.current[KEY] = previous
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.record(snapshot, cache_hit: false)
|
|
20
|
+
Thread.current[KEY]&.observe(snapshot, cache_hit:)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'extraction_profile'
|
|
4
|
+
|
|
5
|
+
module ArchUnit
|
|
6
|
+
module Extraction
|
|
7
|
+
# Independent graph counts, optionally forwarding work to a caller's timing profile.
|
|
8
|
+
# @api private
|
|
9
|
+
class AnalysisProfile
|
|
10
|
+
def initialize(delegate)
|
|
11
|
+
@delegate = delegate
|
|
12
|
+
@counts = ExtractionProfile.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def increment(counter, amount = 1)
|
|
16
|
+
@counts.increment(counter, amount)
|
|
17
|
+
@delegate&.increment(counter, amount)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def measure(stage, &)
|
|
21
|
+
ExtractionProfile.measure(@delegate, stage, &)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def counters
|
|
25
|
+
@counts.counters
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
private_constant :AnalysisProfile
|
|
29
|
+
end
|
|
30
|
+
end
|