Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ jobs:
name: RAG fixture on Ruby 4.0
runs-on: ubuntu-latest
timeout-minutes: 10
env:
ARCHUNIT_RUBY_PATH: ../ArchUnitRuby

steps:
- name: Check out ArchUnitRuby
Expand All @@ -299,8 +301,7 @@ jobs:
bundler-cache: true
working-directory: ArchUnitRuby-TestRepo-RAG
env:
# The fixture locks an older version of the adjacent, checked-out path gem.
# Refresh that entry for this revision while retaining its locked dependencies.
# Switch the fixture's locked release to the adjacent revision in this disposable job.
BUNDLE_FROZEN: "false"

- name: Verify the fixture loads this checkout
Expand Down
1 change: 1 addition & 0 deletions .yardopts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
--no-private
--no-stats
--asset docs-assets/custom.css:css/archunit.css
--asset docs-assets/custom.js:js/archunit.js
lib/**/*.rb
-
README.md
Expand Down
4 changes: 4 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ observed files matching multiple layers with UserError; empty layers use the nor
EmptyTestViolation defense unless explicitly permitted. CheckOptions controls analysis, exclusions,
cache and logging as usual; source parse errors remain fatal. `.because` and framework matchers
work normally. Rails conventions do not boot Rails or automatically turn on expanded/helper modes.
For an autoloaded Rails app, pass
`CheckOptions.new(dependency_analysis: :imports_and_constants)` to `check` or `assert_passes`
(CLI: `--dependency-analysis imports_and_constants`). Otherwise, a check can pass without seeing
references between mapped layers that have no imports. See [choosing a mode](README.md#analysis).
These presets are not complete architecture/cycle/protocol verification; compose more rules or
build arbitrary policies with `project_layers`.

Expand Down
23 changes: 21 additions & 2 deletions GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,17 @@ puts ArchUnit.project_slices.defined_by('app/(**)/').to_plantuml(options)
values = ArchUnit.metrics.distance.instability.measure(options)
~~~

The default remains `dependency_analysis: :imports`. Expanded mode indexes declarations from the
same included source files, then resolves references to definition files. Files remain graph nodes.
The default remains `dependency_analysis: :imports` to preserve the meaning of existing rules and
keep cold checks focused on explicit imports. Start there for gems and apps that declare their
dependencies with `require` or `require_relative`. Start with `:imports_and_constants` for
Rails/Zeitwerk or other autoloaded applications: a green imports-only check may simply mean the
dependency was not represented in the import graph. `rails_imports: true` is a separate setting for
legacy `require_dependency` calls, not a substitute for constant analysis.

Expanded mode indexes declarations from the same included source files, then resolves references
to definition files. This adds cold-check time and memory, especially in larger projects; later
checks with the same extraction options can reuse the cached graph. Measure on your own code and
verify a known violating reference before enabling a rule in CI. Files remain graph nodes.
It supports lexical nesting, compact namespaces, root-qualified names, and explicit superclass,
`include`, `prepend`, and `extend` references. Nested `module A; module B` includes `A` in lexical
lookup; top-level `module A::B` does not. A nearer namespace shadows a root namespace even when its
Expand Down Expand Up @@ -698,6 +707,16 @@ patterns can be overridden with `PresetOptions.folders` (String or Array of sour
Unknown/duplicate keys and empty patterns are errors. For arbitrary policies use `project_layers`.
The Rails preset is opinionated and expects a services layer; it is not full Rails/DDD verification
and does not automatically enable `rails_imports` or constant analysis.
For a Zeitwerk app, pass expanded analysis at check time:

~~~ruby
rule = ArchUnit.architecture_preset(:rails)
options = ArchUnit::CheckOptions.new(dependency_analysis: :imports_and_constants)
ArchUnit.assert_passes(rule, options)
~~~

For a CLI-configured preset use `bundle exec archunit check --dependency-analysis imports_and_constants`.
Neither option boots Rails or reproduces its runtime autoloading.

In trusted CLI configuration use `rule('Core independent') { architecture_preset(:hexagonal) }`;
the helper defaults to the CLI's selected project root. Public `ArchUnit.architecture_preset` uses
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,17 @@ report queries, and complete examples.

## Choosing the analysis mode

The default `:imports` mode recognizes supported static `require`, `require_relative`,
`autoload`, and `load` calls. It does not infer the runtime behavior of Ruby.
The default `:imports` mode checks supported static `require`, `require_relative`,
`autoload`, and `load` calls. It keeps existing rules compatible and avoids the cost and
uncertainty of inferring dependencies from constant names.

For autoloaded code, explicitly add the supported static constant-reference model:
| Your project | Start with | Why |
| --- | --- | --- |
| Rails/Zeitwerk or another autoloaded app | `:imports_and_constants` | Cross-layer constant uses often have no explicit import. |
| Gem or app with explicit imports | `:imports` | Checks the declared dependencies without building a constant index. |

The Rails preset defines folder boundaries; it does **not** switch analysis modes. In imports-only
mode, a green check can miss an autoloaded dependency. Opt in when checking autoloaded code:

<!-- example:expanded-options -->
~~~ruby
Expand All @@ -196,8 +203,10 @@ options = ArchUnit::CheckOptions.new(dependency_analysis: :imports_and_constants
violations = rule.check(options)
~~~

Expanded analysis maps known project constants to definition files and retains evidence.
It is still bounded: ambiguous, inherited, or dynamic lookup can remain unresolved.
Expanded analysis builds a project-wide constant index and resolves references on a cold check.
Expect extra time and memory, especially on large projects; repeated checks can reuse the cached
graph. A green result still is not proof of runtime behavior: ambiguous, inherited, and dynamic
lookups can remain unresolved. Verify your rule against a known violation before relying on it in CI.
[Resolution details](GUIDE.md#analysis) · [Options and caching](API.md#check-options)

<a id="development-cli"></a>
Expand Down
15 changes: 13 additions & 2 deletions docs-assets/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ a:hover,

a:focus-visible,
button:focus-visible,
input:focus-visible {
input:focus-visible,
summary:focus-visible,
pre:focus-visible {
outline: 3px solid var(--au-gold);
outline-offset: 3px;
}
Expand All @@ -70,6 +72,7 @@ input:focus-visible {
radial-gradient(circle at 78% -10%, rgb(228 73 63 / 14%), transparent 34rem),
var(--au-bg);
color: var(--au-text);
min-width: 0;
padding: 0 3rem 4rem;
}

Expand Down Expand Up @@ -391,6 +394,7 @@ pre,
font-family: var(--au-mono) !important;
line-height: 1.65;
margin: 1.5rem 0;
max-width: 100%;
overflow-x: auto;
padding: 1.35rem !important;
}
Expand Down Expand Up @@ -551,6 +555,10 @@ h2 small a:hover,
text-transform: uppercase;
}

#toc > summary {
cursor: pointer;
}

#toc li {
line-height: 1.5;
}
Expand Down Expand Up @@ -678,6 +686,10 @@ pre.code .rubyid_include {
position: static;
width: 100%;
}

#content {
overflow-wrap: anywhere;
}
}

@media (max-width: 620px) {
Expand All @@ -702,7 +714,6 @@ pre.code .rubyid_include {
pre {
border-radius: 8px !important;
font-size: 0.78rem;
margin-inline: -0.5rem;
padding: 1rem !important;
}
}
Expand Down
31 changes: 31 additions & 0 deletions docs-assets/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(() => {
// YARD also loads pages in a hidden iframe before swapping their content into #main.
if (window !== window.top) return;
const main = document.getElementById("main");
if (!main) return;

const narrow = window.matchMedia("(max-width: 920px)");
function enhanceContents() {
const toc = main.querySelector("div#toc");
const list = toc?.querySelector("ol.top");
if (!list) return;

const disclosure = document.createElement("details");
disclosure.id = toc.id;
disclosure.open = !narrow.matches;

const summary = document.createElement("summary");
summary.className = "title";
summary.textContent = "Table of Contents";
disclosure.append(summary, list);
toc.replaceWith(disclosure);
}

// Keep working when YARD replaces #main's contents during reference navigation.
new MutationObserver(enhanceContents).observe(main, { childList: true, subtree: true });
enhanceContents();
narrow.addEventListener("change", () => {
const toc = main.querySelector("details#toc");
if (toc) toc.open = !narrow.matches;
});
})();
21 changes: 19 additions & 2 deletions scripts/check_docs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
ROOT = Pathname.new(__dir__).join('..').expand_path
OUTPUT = ROOT.join('docs')
REQUIRED_FILES = %w[
index.html file.API.html file.GUIDE.html class_list.html css/archunit.css assets/logo-rounded.png
assets/social-preview.png .nojekyll robots.txt sitemap.xml
index.html file.API.html file.GUIDE.html _index.html class_list.html css/archunit.css
js/archunit.js assets/logo-rounded.png assets/social-preview.png .nojekyll robots.txt sitemap.xml
].freeze

def internal_target(page, href)
Expand All @@ -20,6 +20,15 @@ def internal_target(page, href)
page.dirname.join(URI::DEFAULT_PARSER.unescape(relative)).cleanpath
end

def check_reference_navigation(page, content)
return unless content.include?('class="archunit-topbar"')

href = content[%r{<a href="([^"]+)">Reference</a>}, 1]
return if href && internal_target(page, href) == OUTPUT.join('_index.html')

raise "Reference must link to the full API index from #{page.relative_path_from(OUTPUT)}"
end

missing = REQUIRED_FILES.reject { |relative| OUTPUT.join(relative).exist? }
raise "Documentation build is missing: #{missing.join(', ')}" unless missing.empty?

Expand All @@ -29,6 +38,8 @@ def internal_target(page, href)
unless content.include?('data-archunit-theme')
raise "Custom theme is missing from #{page.relative_path_from(OUTPUT)}"
end

check_reference_navigation(page, content)
if page.basename.to_s.match?(/\A(?:class|method|file)_list\.html\z/) &&
!content.include?('<body class="archunit-list">')
raise "Navigation theme hook is missing from #{page.relative_path_from(OUTPUT)}"
Expand All @@ -40,6 +51,12 @@ def internal_target(page, href)
end
end

reference = OUTPUT.join('_index.html').binread.force_encoding(Encoding::UTF_8)
unless reference.include?('class="archunit-topbar"') &&
reference.include?('href="ArchUnit.html"') && reference.include?('href="ArchUnit/CLI.html"')
raise 'The full API index must keep site navigation and link to ArchUnit and CLI'
end

raise "Broken documentation links:\n#{broken.join("\n")}" unless broken.empty?

fragments = pages.flat_map { |page| ArchUnitDocumentationLinks.rendered_errors(page) }
Expand Down
15 changes: 10 additions & 5 deletions scripts/prepare_docs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
ROOT = Pathname.new(__dir__).join('..').expand_path
OUTPUT = ROOT.join('docs')
STYLESHEET = OUTPUT.join('css', 'archunit.css')
THEME_SCRIPT = OUTPUT.join('js', 'archunit.js')
LOGO = OUTPUT.join('assets', 'logo-rounded.png')
SOCIAL_PREVIEW = OUTPUT.join('assets', 'social-preview.png')
SITE_URL = 'https://dragonsec-ai.github.io/ArchUnitRuby/'
Expand All @@ -32,7 +33,6 @@
</div>
HTML
PAGE_HEAD = <<~HTML
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#0b0e14">
<meta name="description" content="ArchUnitRuby architecture testing documentation and API reference">
<meta property="og:title" content="ArchUnitRuby — Architecture testing for Ruby">
Expand All @@ -43,7 +43,9 @@
<meta name="twitter:card" content="summary_large_image">
<link rel="icon" type="image/png" href="%<logo>s">
<link rel="stylesheet" href="%<stylesheet>s" data-archunit-theme>
<script defer src="%<theme_script>s"></script>
HTML
VIEWPORT = %(<meta name="viewport" content="width=device-width, initial-scale=1">\n)

def relative_href(target, page)
target.relative_path_from(page.dirname).to_s.tr('\\', '/')
Expand All @@ -53,15 +55,17 @@ def navigation(page)
guide = relative_href(OUTPUT.join('index.html'), page)
api_guide = relative_href(OUTPUT.join('file.API.html'), page)
user_guide = relative_href(OUTPUT.join('file.GUIDE.html'), page)
reference = relative_href(OUTPUT.join('class_list.html'), page)
reference = relative_href(OUTPUT.join('_index.html'), page)
logo = relative_href(LOGO, page)
format(NAVIGATION, guide:, user_guide:, api_guide:, reference:, logo:)
end

def page_head(page)
def page_head(page, content)
stylesheet = relative_href(STYLESHEET, page)
theme_script = relative_href(THEME_SCRIPT, page)
logo = relative_href(LOGO, page)
format(PAGE_HEAD, site_url: SITE_URL, logo:, stylesheet:)
viewport = content.include?('name="viewport"') ? '' : VIEWPORT
viewport + format(PAGE_HEAD, site_url: SITE_URL, logo:, stylesheet:, theme_script:)
end

def rewrite_source_links(content, page)
Expand All @@ -85,7 +89,7 @@ def prepare_page(path)
if page.basename.to_s.match?(/\A(?:class|method|file)_list\.html\z/)
content.sub!('<body>', '<body class="archunit-list">')
end
content.sub!('</head>', "#{page_head(page)}</head>")
content.sub!('</head>', "#{page_head(page, content)}</head>")
content.sub!(/(<div id="main"[^>]*>)/, "\\1#{navigation(page)}")
page.binwrite(content)
end
Expand All @@ -95,6 +99,7 @@ def prepare_page(path)
FileUtils.cp(ROOT.join('assets', 'social-preview.png'), SOCIAL_PREVIEW)

raise 'YARD did not generate the documentation stylesheet' unless STYLESHEET.file?
raise 'YARD did not copy the documentation theme script' unless THEME_SCRIPT.file?
raise 'YARD did not copy the documentation logo' unless LOGO.file?

pages = Dir[OUTPUT.join('**', '*.html')]
Expand Down