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
data/README.md CHANGED
@@ -1,375 +1,310 @@
1
1
  # ArchUnitRuby
2
2
 
3
- Architecture testing for Ruby. Part of **ArchUnitEverything**: one recognizable testing library
4
- for each programming language.
5
-
6
- [![CI](https://github.com/LukasNiessen/ArchUnitRuby/actions/workflows/ci.yml/badge.svg)](https://github.com/LukasNiessen/ArchUnitRuby/actions/workflows/ci.yml)
7
- [![Documentation](https://img.shields.io/badge/docs-GitHub%20Pages-e4493f)](https://lukasniessen.github.io/ArchUnitRuby/)
8
- [![Gem version](https://img.shields.io/gem/v/archunit.svg)](https://rubygems.org/gems/archunit)
9
- [![Gem downloads](https://img.shields.io/gem/dt/archunit.svg)](https://rubygems.org/gems/archunit)
10
- [![Ruby 3.3+](https://img.shields.io/badge/Ruby-3.3%2B-CC342D?logo=ruby&logoColor=white)](https://www.ruby-lang.org/)
11
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
12
- [![GitHub stars](https://img.shields.io/github/stars/LukasNiessen/ArchUnitRuby.svg)](https://github.com/LukasNiessen/ArchUnitRuby)
13
-
14
- ArchUnitRuby turns a Ruby codebase into a dependency graph and lets you test that graph with rules
15
- that read like English:
16
-
17
- ```ruby
18
- ArchUnit.project_files
19
- .in_folder('app/api/**')
20
- .should_not.depend_on_files
21
- .in_folder('app/database/**')
22
- ```
23
-
24
- It is a working executable prototype with file, layer, slice, graph-reporting, and metric APIs. It
25
- is tested on Ruby 3.3, 3.4, and 4.0 on Linux and Ruby 4.0 on Windows. Version 0.0.1 is available as
26
- [`archunit`](https://rubygems.org/gems/archunit) on RubyGems.
27
-
28
- Siblings: [ArchUnitTS](https://github.com/LukasNiessen/ArchUnitTS) and
29
- [ArchUnitPython](https://github.com/LukasNiessen/ArchUnitPython).
3
+ <p align="center">
4
+ <img src="assets/logo-rounded.png" width="160" alt="ArchUnitRuby">
5
+ </p>
6
+
7
+ <p align="center">
8
+ <a href="https://github.com/dragonsec-ai/ArchUnitRuby/actions/workflows/ci.yml"><img alt="Build and tests" src="https://img.shields.io/github/actions/workflow/status/dragonsec-ai/ArchUnitRuby/ci.yml?branch=main&amp;label=build%20%26%20tests"></a>
9
+ <a href="https://rubygems.org/gems/archunit"><img alt="RubyGems version" src="https://img.shields.io/gem/v/archunit.svg"></a>
10
+ <a href="https://clickgems.clickhouse.com/dashboard/archunit"><img alt="Gem downloads" src="https://img.shields.io/gem/dt/archunit.svg"></a>
11
+ <a href="LICENSE"><img alt="MIT license" src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
12
+ <img alt="Ruby 3.3 or newer" src="https://img.shields.io/badge/Ruby-3.3%2B-CC342D?logo=ruby&amp;logoColor=white">
13
+ </p>
14
+
15
+ **Architecture rules for Ruby, enforced in your tests.**
16
+
17
+ Keep dependency boundaries explicit, catch cycles, enforce conventions, measure code quality,
18
+ and generate architecture diagrams—with readable rules in RSpec, Minitest, or plain Ruby.
19
+
20
+ ~~~ruby
21
+ ArchUnit.project_files.in_folder('app/api/**')
22
+ .should_not.depend_on_files.in_folder('app/database/**')
23
+ .because('Keep persistence behind the service boundary')
24
+ ~~~
25
+
26
+ ArchUnitRuby analyzes source with Prism without booting your application. It is the Ruby member
27
+ of the [ArchUnitEverything family](#community), inspired by—but not affiliated with—ArchUnit.
28
+
29
+ [Quickstart](#quickstart) · [Capabilities](#capabilities) · [User guide](GUIDE.md) ·
30
+ [API contract](API.md) · [Documentation site](https://dragonsec-ai.github.io/ArchUnitRuby/) ·
31
+ [Example project](#example-project) · [Limitations](#limitations)
32
+
33
+ <a id="availability"></a>
34
+
35
+ ## Release status
36
+
37
+ | Version | Capabilities |
38
+ | --- | --- |
39
+ | `1.0` | Fluent file/layer/slice rules, cycles and naming, metrics, graph/diagram exports, RSpec/Minitest integrations, opt-in constant-file analysis |
40
+ | `1.1` | Everything in 1.0, plus CLI, analysis diagnostics, accepted-debt baselines, architecture presets, Rails helper imports, caller-owned source exclusions, and further extraction fixes |
41
+
42
+ This guide and its installation example target the **1.1 series**. Check
43
+ [RubyGems](https://rubygems.org/gems/archunit) for published versions; the documentation site
44
+ follows `main` and can be ahead of the registry during release preparation.
45
+ Installing 1.0.0 does not provide the CLI or other 1.1 additions. See the
46
+ [changelog](CHANGELOG.md) and [1.x compatibility contract](API.md#compatibility) before upgrading.
30
47
 
31
- ## Documentation
48
+ <a id="quickstart"></a>
32
49
 
33
- The [documentation site](https://lukasniessen.github.io/ArchUnitRuby/) combines this guide with a
34
- searchable, source-generated API reference for every public module, class, and method. The same
35
- site is rebuilt in CI and deployed from `main`, so the published reference follows the repository.
50
+ ## Quickstart
36
51
 
37
- ## Install
52
+ ### 1. Install
38
53
 
39
- ArchUnitRuby requires Ruby 3.3 or newer. Add it to your test dependencies:
54
+ Ruby 3.3+ is required. Add ArchUnitRuby to your test dependencies:
40
55
 
41
- ```ruby
56
+ ~~~ruby
42
57
  # Gemfile
43
58
  group :test do
44
- gem 'archunit', '~> 0.0.1'
59
+ gem 'archunit', '~> 1.1.0'
60
+ gem 'rspec' # Omit this line if RSpec is already in your bundle.
45
61
  end
46
- ```
62
+ ~~~
47
63
 
48
- Then install it:
49
-
50
- ```bash
64
+ ~~~sh
51
65
  bundle install
52
- ```
66
+ ~~~
67
+
68
+ RSpec and Minitest are optional integrations, not runtime dependencies of the gem.
69
+ Already using Minitest? Keep your existing setup and use `assert_passes(rule)`.
53
70
 
54
- Or install it directly with `gem install archunit`.
71
+ ### 2. Write your first architecture test
55
72
 
56
- RSpec and Minitest integrations are optional; ArchUnitRuby does not install either test framework
57
- for you.
73
+ For this example, use two ordinary source files:
58
74
 
59
- ## Your first rule
75
+ ~~~text
76
+ app/
77
+ api/request.rb # class Request; end
78
+ database/store.rb # class Store; end
79
+ spec/
80
+ architecture_spec.rb
81
+ ~~~
60
82
 
61
- Create `spec/architecture_spec.rb`:
83
+ Create `spec/architecture_spec.rb`. Change the folder patterns to match your project.
62
84
 
63
- ```ruby
85
+ <!-- example:quickstart-spec -->
86
+ ~~~ruby
64
87
  require 'archunit'
65
88
 
66
- RSpec.describe 'architecture' do
89
+ RSpec.describe 'Architecture' do
67
90
  it 'keeps the API away from the database' do
68
91
  rule = ArchUnit.project_files.in_folder('app/api/**')
69
92
  .should_not.depend_on_files.in_folder('app/database/**')
93
+ .because('Keep persistence behind the service boundary')
70
94
 
71
95
  expect(rule).to pass
72
96
  end
73
97
  end
74
- ```
75
-
76
- Run it like any other specification:
98
+ ~~~
77
99
 
78
- ```bash
100
+ ~~~sh
79
101
  bundle exec rspec spec/architecture_spec.rb
80
- ```
81
-
82
- The project locator is optional. With no argument, ArchUnitRuby searches from the current directory
83
- for a `Gemfile` or gemspec. Pass a directory or either marker file when analyzing another project:
84
-
85
- ```ruby
86
- ArchUnit.project_files('/workspace/my_app')
87
- ArchUnit.project_files('/workspace/my_app/Gemfile')
88
- ```
89
-
90
- ## The fluent grammar
91
-
92
- Every rule is built left to right from the same small grammar:
93
-
94
- | Stage | Purpose | Examples |
95
- | --- | --- | --- |
96
- | Entry | Choose the architectural vocabulary | `project_files`, `project_layers`, `project_slices`, `project_graph`, `metrics` |
97
- | Scope | Select subjects; repeated scopes use AND | `in_folder`, `in_path`, `with_name`, `for_classes_matching` |
98
- | Mood | Choose the expected direction | `should`, `should_not` |
99
- | Predicate | State the policy | `have_no_cycles`, `depend_on_files`, `adhere_to_diagram` |
100
- | Object | Select the target of a relational rule | `in_folder`, `matching`, a layer or slice name |
101
- | Terminal | Execute or render | `check`, `to_json`, `export_as_html`, `measure` |
102
-
103
- Building a rule is lazy and does not scan the filesystem. `check`, `measure`, snapshot/report
104
- terminals, and export terminals perform the work. Builders are immutable, so a scope can be reused:
105
-
106
- ```ruby
107
- services = ArchUnit.project_files.in_folder('app/services/**')
102
+ ~~~
108
103
 
109
- cycle_rule = services.should.have_no_cycles
110
- database_rule = services.should_not.depend_on_files.in_folder('app/database/**')
111
- ```
104
+ No special CI service is required: run architecture specifications with your normal tests.
112
105
 
113
- String patterns are anchored globs. `*` stays inside one path segment, `**` crosses directories,
114
- and `?` matches one non-separator character. Most selectors also accept regular expressions;
115
- `defined_by_regex` is the explicit regex form for slices, while `in_file` takes one exact path.
116
- Paths are project-relative and normalized to `/` separators.
106
+ ### 3. See it catch a dependency
117
107
 
118
- A scope matching zero files returns `EmptyTestViolation`; it does not silently pass. Opt out only
119
- when an empty result is genuinely valid:
108
+ Add this line to `app/api/request.rb` and rerun the test:
120
109
 
121
- ```ruby
122
- rule.check(ArchUnit::CheckOptions.new(allow_empty_tests: true))
123
- ```
110
+ ~~~ruby
111
+ require_relative '../database/store'
112
+ ~~~
124
113
 
125
- ## Files
114
+ The test fails with concrete file evidence:
126
115
 
127
- File rules cover cycles, naming, location, internal dependencies, external modules, and custom
128
- source predicates:
116
+ ~~~text
117
+ File dependency violation
118
+ File 'app/api/request.rb' depends on forbidden file 'app/database/store.rb'.
119
+ ~~~
129
120
 
130
- ```ruby
131
- rules = [
132
- ArchUnit.project_files.in_path('lib/**/*.rb').should.have_no_cycles,
133
- ArchUnit.project_files.in_folder('app/services/**')
134
- .should.have_name('*_service.rb'),
135
- ArchUnit.project_files.in_folder('app/domain/**')
136
- .should_not.depend_on_external_modules.matching('faraday')
137
- ]
121
+ Remove that dependency in this small fixture to return to green. In an application, refactor
122
+ the interaction through your intended service boundary instead of merely suppressing the test.
123
+ In a long-lived process, clear the graph cache after source edits; separate RSpec runs start fresh.
138
124
 
139
- rules.each { |rule| ArchUnit.assert_passes(rule) }
140
- ```
125
+ [Complete workflows](GUIDE.md) · [Watch the recorded demo](https://github.com/user-attachments/assets/9b7305a0-deee-454d-b99e-f8fac63d06ab)
141
126
 
142
- A custom predicate receives an immutable `FileInfo` with `path`, `name`, `extension`, `directory`,
143
- complete `content`, and non-blank `lines_of_code`:
127
+ <a id="capabilities"></a>
144
128
 
145
- ```ruby
146
- rule = ArchUnit.project_files.in_folder('app/services/**')
147
- .should.adhere_to(
148
- ->(file) { file.lines_of_code < 300 },
149
- 'services must stay below 300 non-blank lines'
150
- )
151
- ```
129
+ ## What you can enforce and explore
152
130
 
153
- ## Layers
154
-
155
- Named layers express an allowlist or blocklist over groups of files:
156
-
157
- ```ruby
158
- rule = ArchUnit.project_layers
159
- .layer('api').defined_by('app/api/**/*.rb')
160
- .layer('services').defined_by('app/services/**/*.rb')
161
- .layer('database').defined_by('app/database/**/*.rb')
162
- .where_layer('api').may_only_depend_on_layers('services')
163
- .where_layer('services').may_only_depend_on_layers('database')
164
- .where_layer('database').may_only_depend_on_layers
165
-
166
- expect(rule).to pass
167
- ```
168
-
169
- Dependencies within one layer are always allowed. Edges with an unassigned endpoint are ignored.
170
- Calling `may_only_depend_on_layers` without targets seals a layer; `may_not_depend_on_layers`
171
- requires at least one forbidden target.
172
-
173
- ## Slices and PlantUML
174
-
175
- Slices group files by one captured path segment and preserve every concrete dependency as evidence:
176
-
177
- ```ruby
178
- slices = ArchUnit.project_slices.defined_by('lib/my_app/(**)/')
179
- rule = slices.should_not.contain_dependency('api', 'database')
180
-
181
- expect(rule).to pass
182
- ```
183
-
184
- `(**)` is the slice capture. `defined_by_regex` uses the first regular-expression capture instead.
185
-
186
- A checked-in PlantUML component diagram can also be the architecture contract:
187
-
188
- ```ruby
189
- rule = slices.should
190
- .ignoring_external_slices
191
- .adhere_to_diagram_in_file('docs/architecture.puml')
192
-
193
- expect(rule).to pass
194
- ```
195
-
196
- The supported subset recognizes components, directed dependencies, comments, and `@startuml` /
197
- `@enduml`. Use `to_plantuml` or `export_as_plantuml(path)` to generate a diagram from the real graph.
198
-
199
- ## Dependency graph reports
200
-
201
- Graph reporting builds one immutable snapshot and renders it consistently as DOT, Mermaid, D2, CSV,
202
- JSON, or self-contained HTML:
203
-
204
- ```ruby
205
- report = ArchUnit.project_graph
206
- .include_external_dependencies
207
- .focus_on('app/services/**', 2)
208
- .collapse_to_folder_depth(2)
209
- .titled('Service dependencies')
210
-
211
- puts report.summary.node_count
212
- report.export_as_html('reports/services.html')
213
- ```
131
+ | Capability | Public entry point | Details |
132
+ | --- | --- | --- |
133
+ | File boundaries, external modules, naming, locations, custom predicates | `project_files` | [File rules](API.md#file-rules) |
134
+ | Directed dependency cycles | `project_files.should.have_no_cycles` | [Rule recipes](GUIDE.md#recipes) |
135
+ | Named-layer allowlists and blocklists | `project_layers` | [Layer rules](API.md#layer-rules) |
136
+ | Component slices and PlantUML conformance | `project_slices` | [Slices and diagrams](API.md#slices) |
137
+ | Mermaid, DOT, D2, JSON, CSV, and offline HTML reports | `project_graph` | [Graph reports](API.md#graph-reports) |
138
+ | Counts, cohesion, coupling, distance, and custom thresholds | `metrics` | [Metrics](API.md#metrics) |
139
+ | Layered, hexagonal, and Rails presets | `architecture_preset` — **1.1+** | [Preset contract](API.md#presets) |
140
+
141
+ Builders are immutable and lazy: assembling a rule does not scan the project. Terminal calls
142
+ such as `check`, `measure`, `snapshot`, and exports perform the work.
143
+ `check` returns structured violations; framework integrations turn them into test failures.
144
+
145
+ | Integration | Assertion |
146
+ | --- | --- |
147
+ | RSpec | `expect(rule).to pass` |
148
+ | Minitest | `assert_passes(rule)` |
149
+ | Plain Ruby | `ArchUnit.assert_passes(rule)` |
214
150
 
215
- Queries include `focus_on`, `reachable_from`, and `dependents_of`. Collapse by folder depth or a
216
- regular-expression replacement. Every format has an in-memory `to_<format>` and an
217
- `export_as_<format>(path)` terminal.
151
+ ### More than dependency boundaries
218
152
 
219
- ## Metrics
153
+ A cycle rule:
220
154
 
221
- Metric scopes select files and Ruby classes before measurement or assertion:
155
+ <!-- example:cycle-rule -->
156
+ ~~~ruby
157
+ rule = ArchUnit.project_files.in_path('app/**/*.rb').should.have_no_cycles
158
+ ArchUnit.assert_passes(rule)
159
+ ~~~
222
160
 
223
- ```ruby
224
- services = ArchUnit.metrics
225
- .in_path('app/services/**/*.rb')
226
- .for_classes_matching('*Service')
161
+ An architecture report:
227
162
 
228
- size_rule = services.count.method_count.should_be_below_or_equal(20)
229
- cohesion_rule = services.lcom.lcom4.should_be(1)
230
- distance_rule = services.distance.instability.should_be_below(0.8)
163
+ <!-- example:graph-report -->
164
+ ~~~ruby
165
+ report = ArchUnit.project_graph.collapse_to_folder_depth(2).titled('Application dependencies')
166
+ report.export_as_html('reports/architecture.html')
167
+ puts report.to_mermaid
168
+ ~~~
231
169
 
232
- [size_rule, cohesion_rule, distance_rule].each { |rule| ArchUnit.assert_passes(rule) }
233
- ```
170
+ A metric threshold:
234
171
 
235
- Count metrics cover class methods and fields plus file lines, statements, imports, classes, and
236
- top-level functions. Cohesion includes LCOM96a, LCOM96b, LCOM1-5, and LCOM*. Dependency-derived
237
- metrics include abstractness, instability, main-sequence distance, coupling factor, and normalized
238
- distance. Zone guards detect the conventional zones of pain and uselessness.
172
+ <!-- example:metric-rule -->
173
+ ~~~ruby
174
+ rule = ArchUnit.metrics.in_path('app/services/**/*.rb')
175
+ .count.method_count.should_be_below_or_equal(20)
176
+ ArchUnit.assert_passes(rule)
177
+ ~~~
239
178
 
240
- Use `measure` for immutable numeric results, `custom_metric` for a calculation over `ClassInfo`, and
241
- `export_as_html` for an offline metrics report:
179
+ See the [user guide](GUIDE.md#recipes) for layer/slice policies, custom predicates, logging,
180
+ report queries, and complete examples.
242
181
 
243
- ```ruby
244
- services.count.export_as_html('reports/service-counts')
245
- ```
182
+ <a id="analysis"></a>
246
183
 
247
- The threshold vocabulary is intentionally limited to `should_be_below`, `should_be_above`,
248
- `should_be`, `should_be_below_or_equal`, `should_be_above_or_equal`, and `should_satisfy`.
184
+ ## Choosing the analysis mode
249
185
 
250
- ## Pattern exclusions
186
+ The default `:imports` mode recognizes supported static `require`, `require_relative`,
187
+ `autoload`, and `load` calls. It does not infer the runtime behavior of Ruby.
251
188
 
252
- Every selector accepts `except:` in the same call. A plain pattern or array uses the parent
253
- selector's context, including filenames for path and folder selectors:
189
+ For autoloaded code, explicitly add the supported static constant-reference model:
254
190
 
255
- ```ruby
256
- scope = ArchUnit.project_files.in_path(
257
- 'app/**/*.rb',
258
- except: ['app/generated/**', 'schema.rb']
259
- )
260
- ```
191
+ <!-- example:expanded-options -->
192
+ ~~~ruby
193
+ rule = ArchUnit.project_files.in_folder('app/api/**')
194
+ .should_not.depend_on_files.in_folder('app/database/**')
195
+ options = ArchUnit::CheckOptions.new(dependency_analysis: :imports_and_constants)
196
+ violations = rule.check(options)
197
+ ~~~
261
198
 
262
- Use explicit targets when needed. Supported keys are `in_path`, `in_folder`, `with_name`, and
263
- `for_classes_matching`:
199
+ Expanded analysis maps known project constants to definition files and retains evidence.
200
+ It is still bounded: ambiguous, inherited, or dynamic lookup can remain unresolved.
201
+ [Resolution details](GUIDE.md#analysis) · [Options and caching](API.md#check-options)
264
202
 
265
- ```ruby
266
- scope = ArchUnit.metrics.in_path(
267
- 'app/**/*.rb',
268
- except: { in_folder: 'app/generated', with_name: '*_spec.rb' }
269
- )
270
- ```
203
+ <a id="development-cli"></a>
271
204
 
272
- ## Results and test frameworks
205
+ ## CLI and gradual adoption — 1.1+
273
206
 
274
- `check` returns an array of structured violations. Architecture disagreement is data, not an
275
- exception:
207
+ These commands require ArchUnitRuby 1.1 or newer. They are not available in 1.0.0.
276
208
 
277
- ```ruby
278
- violations = rule.check
279
- violations.each { |violation| puts violation.class }
280
- ```
209
+ ~~~sh
210
+ bundle exec archunit init
211
+ # Review and customize the generated .archunit.rb before running it.
212
+ bundle exec archunit check
213
+ bundle exec archunit check --format json --dependency-analysis imports_and_constants
214
+ bundle exec archunit explain
215
+ ~~~
281
216
 
282
- Translate that result into a test failure at the boundary that suits your suite:
217
+ The CLI composes the same rules as your tests. It reports source/uncertainty diagnostics without
218
+ turning unknowns into a semantic-coverage percentage. `explain` lists configuration; it does not
219
+ prove that rules passed.
283
220
 
284
- ```ruby
285
- expect(rule).to pass # RSpec
286
- assert_passes(rule) # Minitest test case
287
- ArchUnit.assert_passes(rule) # Framework-neutral
288
- ```
221
+ For gradual adoption, explicitly record reviewed violations:
289
222
 
290
- `ArchUnit.format_violations` and `ResultFactory` provide stable human-readable output. All
291
- violations retain the concrete dependency, file, layer, slice, or metric evidence that caused them.
223
+ ~~~sh
224
+ bundle exec archunit baseline --output .archunit-baseline.json
225
+ bundle exec archunit check --baseline .archunit-baseline.json
226
+ ~~~
292
227
 
293
- ## Per-check logging
228
+ Every rule still runs. New violations and stale entries for fixed debt fail the check.
229
+ An `accepted` result is not a clean `passed` result. Review baseline changes; do not regenerate
230
+ them blindly in CI. Presets compose existing layer rules and retain empty-layer and overlap guards.
294
231
 
295
- Logging is off by default and belongs to one check; there is no process-global configuration:
232
+ **Trust boundary:** `.archunit.rb` is executable Ruby, including during `explain`. For an untrusted
233
+ repository, use your own trusted `--config` file. The analyzer does not automatically execute
234
+ target source, Gemfiles/gemspecs, tests, or Rails boot; your own Bundler/configuration code still
235
+ has normal Ruby behavior.
296
236
 
297
- ```ruby
298
- logging = ArchUnit::LoggingOptions.new(
299
- level: :debug,
300
- output_directory: 'tmp/archunit-logs',
301
- append: false
302
- )
237
+ [CLI contract](API.md#cli) · [Baseline contract](API.md#baselines) · [Presets](GUIDE.md#presets)
303
238
 
304
- violations = rule.check(ArchUnit::CheckOptions.new(logging: logging))
305
- ```
239
+ <a id="limitations"></a>
306
240
 
307
- Levels are `debug`, `info`, `warn`, and `error`. The fixed events cover check start/end, progress,
308
- violations, and metric evidence. `io:` defaults to `$stderr`, accepts any writable stream, and may be
309
- `nil`. File output creates missing directories and writes timestamped `archunit-*.log` files.
241
+ ## Important boundaries
310
242
 
311
- ## What ArchUnitRuby extracts
243
+ - Empty subject selections fail by default; they are usually a typo, not a clean architecture.
244
+ - Malformed included Ruby raises `SourceParseError`. Exclude identified generator templates
245
+ explicitly; a rule selector does not narrow whole-project source discovery.
246
+ - Runtime-derived imports, ambiguous constants, inherited lookup, and metaprogramming can stay
247
+ unresolved. Passing a policy describes the resolved static model—not complete runtime behavior.
248
+ - Whole-project exclusions and rule selectors serve different purposes. Source symlinks are skipped;
249
+ cached graphs require explicit clearing after edits in a long-lived process.
250
+ - Full method-call/signature protocols and semantic component selectors are **not shipped**.
251
+ The [semantic evaluation](https://github.com/dragonsec-ai/ArchUnitRuby/blob/main/design/semantic-boundaries.md)
252
+ is a decision record, not feature parity. Dense cycle enumeration can still be expensive.
312
253
 
313
- ArchUnitRuby uses Prism and statically recognizes:
254
+ [Full analysis contract](GUIDE.md#analysis) · [Errors and troubleshooting](API.md#errors)
314
255
 
315
- | Ruby source form | Import kind |
316
- | --- | --- |
317
- | `require 'json'` | `:require` |
318
- | `require_relative '../models/order'` | `:require_relative` |
319
- | `autoload :Order, 'models/order'` | `:autoload` |
320
- | `load 'config/setup.rb'` | `:load` |
256
+ <a id="example-project"></a>
321
257
 
322
- Project dependencies use normalized, project-relative paths. Standard-library and gem dependencies
323
- retain the module name written in source. Inline or immediately preceding ignore directives can
324
- suppress known compatibility imports:
258
+ ## Example project and validation
325
259
 
326
- ```ruby
327
- require 'legacy/client' # archunit: ignore legacy/client
260
+ The [Ruby RAG example](https://github.com/TristanKruse/ArchUnitRuby-TestRepo-RAG) is a small layered
261
+ application with intentional architecture violations. Use it for a hands-on walkthrough;
262
+ its tests also run against the current checkout in our CI.
328
263
 
329
- # archunit: ignore experimental/plugin
330
- require 'experimental/plugin'
331
- ```
264
+ The library dogfoods its own architecture. CI checks Ruby 3.3/3.4/4.0 on Linux and Ruby 4.0 on
265
+ Windows, gates runtime-library coverage at 98% line / 90% branch, validates docs and package
266
+ installation, and runs security checks. Coverage measures executed test paths, not Ruby semantic
267
+ completeness or a guarantee that no bugs remain.
332
268
 
333
- Dynamic imports such as `require dependency_name` or `require "plugins/#{name}"` are omitted rather
334
- than guessed because resolving them would require executing application code.
269
+ Four pinned production-library canaries—Rack, Rake, RSpec Core, and RestClient—exercise passing
270
+ and deliberately failing policies. A broader 50-repository source audit and the separate
271
+ [Linux performance study](https://github.com/dragonsec-ai/ArchUnitRuby/blob/main/benchmark/study/README.md)
272
+ provide bounded evidence, not upstream architecture/security judgments or a universal speed claim.
335
273
 
336
- ## Executable examples
274
+ <a id="documentation"></a>
337
275
 
338
- The [ArchUnitRuby RAG test repository](https://github.com/TristanKruse/ArchUnitRuby-TestRepo-RAG)
339
- is a small layered retrieval-augmented-generation application with two deliberate architecture
340
- violations. Its test suite exercises the public APIs above against real Ruby source on Linux and
341
- Windows.
276
+ ## Documentation and development
342
277
 
343
- ArchUnitRuby also dogfoods itself in `spec/architecture_spec.rb`: `common` is isolated, domain
344
- modules cannot depend on one another, implementation files cannot depend on the public surface, and
345
- the complete library graph must remain cycle-free.
278
+ - [User guide](GUIDE.md): recipes, project discovery, exclusions, Ruby analysis, CLI, and migration.
279
+ - [API contract](API.md): exact behavior, options, report formats, and errors.
280
+ - [Online documentation](https://dragonsec-ai.github.io/ArchUnitRuby/): getting started, guides,
281
+ and searchable source-generated reference; follows `main`.
282
+ - [Changelog](CHANGELOG.md), [contributing](CONTRIBUTING.md), [security](SECURITY.md),
283
+ [support](SUPPORT.md), and [release process](https://github.com/dragonsec-ai/ArchUnitRuby/blob/main/.github/RELEASING.md).
346
284
 
347
- ## Development
285
+ To work on the library:
348
286
 
349
- ```bash
350
- git clone https://github.com/LukasNiessen/ArchUnitRuby.git
287
+ ~~~sh
288
+ git clone https://github.com/dragonsec-ai/ArchUnitRuby.git
351
289
  cd ArchUnitRuby
352
290
  bundle install
353
291
  bundle exec rake
354
292
  bundle exec rake docs
355
- gem build archunit.gemspec --strict
356
- ```
293
+ ~~~
357
294
 
358
- `bundle exec rake` runs the randomized RSpec suite and RuboCop. CI additionally enforces 98% line
359
- and 90% branch coverage, runs the dogfooding rules explicitly, builds the documentation, loads the
360
- public API with Ruby warnings, tests optional RSpec/Minitest integrations, builds and installs the
361
- gem artifact, runs the external RAG fixture, and checks Ruby 3.3, 3.4, and 4.0 across Ubuntu and
362
- Windows.
295
+ <a id="community"></a>
363
296
 
364
- The implementation conventions and intended dependency directions live in [`AGENTS.md`](AGENTS.md).
297
+ ## Community and license
365
298
 
366
- ## Current limitations
299
+ Maintained by [Tristan Kruse](https://github.com/TristanKruse) and
300
+ [Lukas Niessen](https://github.com/LukasNiessen), with our
301
+ [contributors](https://github.com/dragonsec-ai/ArchUnitRuby/graphs/contributors).
367
302
 
368
- - Ruby constants are not modeled as a separate graph. Files are the primary dependency vocabulary.
369
- - Dynamic `require`, `autoload`, and `load` arguments cannot be resolved statically.
370
- - PlantUML support is a deliberately small component-diagram subset, not a complete UML parser.
371
- - The API is still pre-release and may change before the first stable gem version.
372
-
373
- ## License
303
+ Part of the ArchUnitEverything family alongside
304
+ [ArchUnitTS](https://github.com/LukasNiessen/ArchUnitTS) and
305
+ [ArchUnitPython](https://github.com/LukasNiessen/ArchUnitPython).
306
+ Questions and feature requests belong in [GitHub Issues](https://github.com/dragonsec-ai/ArchUnitRuby/issues);
307
+ report vulnerabilities through [SECURITY.md](SECURITY.md).
374
308
 
375
- [MIT](LICENSE)
309
+ [MIT license](LICENSE). [Download analytics](https://clickgems.clickhouse.com/dashboard/archunit)
310
+ count package fetches, not unique users.