thecore_generators 3.2.0 → 3.8.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.
@@ -77,6 +77,54 @@ module Thecore
77
77
  File.directory?(atom_dir) && !gemspec_path_for(atom_dir).nil?
78
78
  end
79
79
 
80
+ # Every valid ATOM directory under `<app_root>/vendor/submodules/`,
81
+ # deterministically (alphabetically) ordered. Used by
82
+ # Thecore::CheckPractices to scan the host app plus every ATOM in one
83
+ # pass when no `--atom=NAME` is given.
84
+ def all_atom_dirs(app_root)
85
+ submodules_dir = File.join(app_root.to_s, "vendor", "submodules")
86
+ return [] unless File.directory?(submodules_dir)
87
+
88
+ Dir.children(submodules_dir).sort.filter_map do |name|
89
+ atom_dir = File.join(submodules_dir, name)
90
+ atom_dir if valid_atom_dir?(atom_dir)
91
+ end
92
+ end
93
+
94
+ # Where does `app/models/<class_name>.rb` actually live? Used by
95
+ # Thecore::Generators::AssociationWiring to tell whether a
96
+ # `references` column's target model sits in the same app/ATOM as the
97
+ # generator invocation (destination_root, after AtomAware's own
98
+ # redirection) or a different one entirely — the cross-boundary case
99
+ # from docs/adr/0003-migration-driven-inverse-association-wiring.md in
100
+ # the thecore repo, where the generator still writes the concern but
101
+ # only logs the gemspec/Gemfile dependency a human needs to add.
102
+ #
103
+ # Searches the host app's own app/models first, then every valid ATOM
104
+ # under vendor/submodules/ (deterministically, alphabetically).
105
+ # Returns the absolute app/ATOM root the model was found under, or nil
106
+ # when it isn't found anywhere (e.g. the target model doesn't exist
107
+ # yet) — callers should treat "not found" as "assume same app/ATOM"
108
+ # rather than erroring, since this is a best-effort lookup, not a
109
+ # requirement.
110
+ def model_root_for(class_name:, app_root:)
111
+ file_name = "#{class_name.to_s.underscore}.rb"
112
+ app_root = app_root.to_s
113
+
114
+ return app_root if File.exist?(File.join(app_root, "app", "models", file_name))
115
+
116
+ submodules_dir = File.join(app_root, "vendor", "submodules")
117
+ return nil unless File.directory?(submodules_dir)
118
+
119
+ Dir.children(submodules_dir).sort.each do |name|
120
+ atom_dir = File.join(submodules_dir, name)
121
+ next unless valid_atom_dir?(atom_dir)
122
+ return atom_dir if File.exist?(File.join(atom_dir, "app", "models", file_name))
123
+ end
124
+
125
+ nil
126
+ end
127
+
80
128
  class << self
81
129
  private
82
130
 
@@ -0,0 +1,33 @@
1
+ require "optparse"
2
+ require "thecore_generators/check_practices"
3
+
4
+ namespace :thecore do
5
+ desc "Audit Thecore scaffolding conventions (Scaffold Files, Models, Actions). " \
6
+ "Usage: rails thecore:check_practices -- [--json] [--atom=NAME] [--fix]"
7
+ task check_practices: :environment do
8
+ # Rake's own option parser only understands its own flags (--trace, -T,
9
+ # ...) - anything meant for the task itself must follow a literal `--`
10
+ # separator, which Rake then leaves untouched at the front of ARGV (see
11
+ # https://ruby.github.io/rake/doc/rakefile_rdoc.html#label-Task+Arguments,
12
+ # the standard convention for passing CLI-style flags to a rake task).
13
+ extra_argv = ARGV.drop_while { |arg| arg != "--" }
14
+ extra_argv.shift
15
+
16
+ options = { json: false, atom: nil, fix: false }
17
+ OptionParser.new do |parser|
18
+ parser.on("--json", "Emit structured JSON instead of human-readable text") { options[:json] = true }
19
+ parser.on("--atom=NAME", "Scope the audit to a single ATOM under vendor/submodules/") { |value| options[:atom] = value }
20
+ parser.on("--fix", "Apply every fixable violation in one pass, no confirmation") { options[:fix] = true }
21
+ end.parse!(extra_argv)
22
+
23
+ begin
24
+ violations = Thecore::CheckPractices.run(app_root: Rails.root, atom_name: options[:atom], fix: options[:fix])
25
+ rescue Thor::Error => e
26
+ abort(e.message)
27
+ end
28
+
29
+ puts(options[:json] ? Thecore::CheckPractices::Reporter.json(violations) : Thecore::CheckPractices::Reporter.text(violations))
30
+
31
+ exit(1) unless violations.empty?
32
+ end
33
+ end
@@ -0,0 +1,220 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Thecore 3 Application Template — porting createApp.js's Gemfile/vendor-directory
4
+ # behavior (thecore_generators#17) plus devcontainer/CI/CLAUDE.md assets fetched from
5
+ # thecore's own samples/ (thecore_generators#18). See ADR 0005 in the thecore repo for
6
+ # the full design rationale behind both.
7
+ #
8
+ # Invoke via:
9
+ #
10
+ # rails new myapp --database=postgresql --asset-pipeline=sprockets \
11
+ # -m https://raw.githubusercontent.com/gabrieletassoni/thecore_generators/release/3/lib/templates/app_template.rb
12
+ #
13
+ # (or a local path to this file, e.g. while developing thecore_generators itself)
14
+
15
+ # --- Core Gemfile stack ------------------------------------------------------
16
+ # Always added, uncommented — every Thecore host app needs these (see thecore's own
17
+ # CLAUDE.md/GUIDE.md, "Standard Gem Stack").
18
+ gem "devise"
19
+ gem "cancancan"
20
+ gem "rails_admin"
21
+ # `rails_admin:install --asset=sprockets` (below) also unconditionally appends its own
22
+ # `gem 'sassc-rails'` line via its `configure_for_sprockets` step -- verified against
23
+ # the installed rails_admin 3.3.0 source, that step does nothing else. The resulting
24
+ # duplicate is harmless (empirically confirmed: `bundle lock` against two identical,
25
+ # unconstrained `gem` lines warns "lists the gem more than once" but resolves and
26
+ # exits 0) -- not worth a fragile post-install dedupe for a cosmetic Gemfile wart, and
27
+ # this declaration must stay regardless, since sassc-rails needs to be active even
28
+ # when the installer chain below is declined.
29
+ gem "sassc-rails"
30
+ # ADR 0001 (thecore repo) requires model_driven_api >= 3.9.0 / thecore_ui_rails_admin
31
+ # >= 3.8.0 for the DefaultModuleRegistry default-concern behavior this whole ecosystem
32
+ # now relies on -- matching the floor this gem's own Gemfile already pins to (see its
33
+ # comment), not an arbitrary choice.
34
+ gem "model_driven_api", "~> 3.9"
35
+ gem "thecore_ui_rails_admin", "~> 3.8"
36
+ gem "rails-erd", group: :development
37
+
38
+ # thecore_generators itself is dev-tooling only (it hooks `rails g model`/`migration`,
39
+ # see ADR 0002 in the thecore repo) — never a runtime dependency, so it goes in the
40
+ # :development group. A single-line `group:` option is equivalent to (and simpler
41
+ # than) createApp.js's manual `group :development do ... end` block insertion.
42
+ gem "thecore_generators", "~> 3.6", group: :development
43
+
44
+ # --- Optional ecosystem gems, discoverable but off by default ---------------
45
+ # Same "commented but documented" philosophy as the devcontainer's gh/glab CLI
46
+ # mounts (ADR 0005): each of these publishes to RubyGems.org (see thecore's own
47
+ # CLAUDE.md) but isn't needed by every app, so it starts commented out with a
48
+ # one-line description — uncomment only the ones this project actually needs.
49
+ append_to_file "Gemfile", <<~RUBY
50
+
51
+ # The rest of the generic Thecore ecosystem — uncomment only what you need.
52
+ # gem "thecore_auth_commons", "~> 3.0" # Role/permission/predicate models and authorization scaffolding
53
+ # gem "thecore_settings", "~> 3.0" # ThecoreSettings key/value configuration store
54
+ # gem "thecore_print_commons", "~> 3.0" # Shared PDF/print generation helpers
55
+ # gem "thecore_background_jobs", "~> 3.0" # Shared background job scheduling helpers
56
+ # gem "thecore_ui_commons", "~> 3.0" # Shared UI helpers/components for the admin frontend
57
+ # gem "thecore_tcp_debug", "~> 3.0" # TCP-level debugging/diagnostics support
58
+ # gem "thecore_download_documents", "~> 3.0" # Document download support
59
+ # gem "thecore_dataentry_commons", "~> 3.0" # Shared data-entry UI helpers
60
+ # gem "thecore_connectors" # Helpers for connecting to external systems/data sources
61
+ RUBY
62
+
63
+ # --- Developer-convenience placeholder directories --------------------------
64
+ # NOT template content (ADR 0005) — just known, git-trackable locations to clone
65
+ # auxiliary repos into during local development. Created empty; nothing is
66
+ # pre-wired, no submodule declarations, no Gemfile `path:` entries.
67
+ empty_directory "vendor/submodules"
68
+ create_file "vendor/submodules/.keep", <<~TEXT
69
+ Clone auxiliary Thecore ecosystem repos here during local development (e.g. an
70
+ ATOM you're developing against this app). Not consumed by any Gemfile entry
71
+ automatically — add a `path:` gem yourself if you want one of these clones
72
+ bundled from source instead of from RubyGems.org.
73
+ TEXT
74
+
75
+ empty_directory "vendor/external"
76
+ create_file "vendor/external/.keep", <<~TEXT
77
+ Clone read-only reference repos here during local development (framework docs,
78
+ sibling tooling repos you want open alongside this app). Not consumed by
79
+ anything automatically.
80
+ TEXT
81
+
82
+ # --- Devcontainer / CI / CLAUDE.md assets, from thecore's own samples -------
83
+ # Single source of truth (ADR 0005) — not duplicated inside this gem. The base
84
+ # location is resolved fresh inside `fetch_thecore_sample` on every call, through
85
+ # one overridable point: `ENV["THECORE_SAMPLES_SOURCE"]`, defaulting to the real
86
+ # raw GitHub URL for thecore's `samples/` directory on `master` (thecore's own
87
+ # default branch — double-checked directly against that repo, not assumed from
88
+ # this gem's own `release/3` naming convention, which doesn't apply there). An
89
+ # unset *or blank* env var falls back to the default (an explicit emptiness
90
+ # check, not `||` alone — `ENV["X"] || default` would treat `THECORE_SAMPLES_SOURCE=""`,
91
+ # a realistic shape for an optional env var in a compose file with no value set,
92
+ # as a real override and try to read a same-named file relative to whatever the
93
+ # current directory happens to be, instead of falling back). An http(s) value is
94
+ # fetched over the network via `get`; anything else is treated as a local
95
+ # directory and read directly — this gem's own offline test points it at a
96
+ # fixture instead.
97
+ #
98
+ # NOTE: this default URL only serves real content once `thecore`'s `master`
99
+ # carries the commits that added `samples/CLAUDE.md`/`samples/.gitlab-ci.yml`
100
+ # and brought `samples/devcontainer/` up to date (thecore#14/#15) — as of this
101
+ # gem's own 3.8.0 release those commits exist only in a local checkout, not
102
+ # pushed to `origin/master` yet. Until they're pushed, the default URL 404s for
103
+ # `.gitlab-ci.yml`/`CLAUDE.md`/the two devcontainer scripts and serves stale
104
+ # content for `devcontainer.json`/`docker-compose.yml`. This is an operational
105
+ # sequencing issue (push `thecore` before relying on the default in production),
106
+ # not a defect in this code — but it means `THECORE_SAMPLES_SOURCE` pointed at a
107
+ # local clone of `thecore` is the only way to exercise the real default content
108
+ # today.
109
+ #
110
+ # Every write below is unconditional (`force: true`, no interactive prompt).
111
+ # Of the six `.devcontainer/*` files, four (`devcontainer.json`,
112
+ # `docker-compose.yml`, `Dockerfile`, `create-db-user.sql`) are expected to
113
+ # already exist, written by the separate, prior "Setup Devcontainer" bootstrap
114
+ # step this template runs inside of (verified directly against that command's
115
+ # source, `thecore_code_extension/commands/setupDevContainer.js`) — replacing
116
+ # them is the whole point, not a conflict to ask about. The other two
117
+ # (`link-host-home.sh`/`check-plugins.sh`) are *not* written by that step at
118
+ # all — genuinely new files here, not overwrites — but `force: true` is harmless
119
+ # for a new file and keeps every write in this block uniform.
120
+ # `.gitlab-ci.yml`/`CLAUDE.md` don't exist yet in the documented flow either,
121
+ # but stay unconditional too so re-running this template later
122
+ # (`bin/rails app:template`) is a clean overwrite, not a prompt.
123
+ #
124
+ # A fetch failure (today: the 404s above; going forward: any network blip, or
125
+ # `thecore` reorganizing `samples/`) aborts the whole template with a clear,
126
+ # specific message instead of continuing into a silently half-scaffolded app
127
+ # (some `.devcontainer/*` files present, others not, no clear signal pointing
128
+ # back to the real cause) -- the same fail-fast philosophy thecore_generators#17
129
+ # already applies to `bundle_command`/`rails_command` failures in the installer
130
+ # chain below.
131
+ def fetch_thecore_sample(relative_path, destination)
132
+ source = ENV["THECORE_SAMPLES_SOURCE"]
133
+ source = "https://raw.githubusercontent.com/gabrieletassoni/thecore/master/samples" if source.nil? || source.empty?
134
+
135
+ if source.start_with?("http://", "https://")
136
+ get("#{source}/#{relative_path}", destination, force: true)
137
+ else
138
+ create_file(destination, File.read(File.join(source, relative_path)), force: true)
139
+ end
140
+ rescue StandardError => e
141
+ abort("Failed to fetch #{relative_path} from #{source} (#{e.class}: #{e.message}) " \
142
+ "-- aborting the app template. Set THECORE_SAMPLES_SOURCE to override the source.")
143
+ end
144
+
145
+ %w[devcontainer.json docker-compose.yml Dockerfile create-db-user.sql link-host-home.sh check-plugins.sh].each do |file|
146
+ fetch_thecore_sample("devcontainer/#{file}", ".devcontainer/#{file}")
147
+ end
148
+ # `get`/`create_file` only write content, never permissions — the executable bit
149
+ # has to be restored explicitly for the two scripts (`thecore/samples/devcontainer/`
150
+ # stores them executable, but that's lost the moment their bytes cross an HTTP
151
+ # fetch or a plain `File.read`).
152
+ chmod ".devcontainer/link-host-home.sh", 0o755
153
+ chmod ".devcontainer/check-plugins.sh", 0o755
154
+
155
+ fetch_thecore_sample(".gitlab-ci.yml", ".gitlab-ci.yml")
156
+ fetch_thecore_sample("CLAUDE.md", "CLAUDE.md")
157
+
158
+ # --- Standard installer chain ------------------------------------------------
159
+ # Needs the gems added above actually bundled and installable, i.e. real network
160
+ # access — genuinely optional (not a test-only escape hatch): a developer
161
+ # bootstrapping offline can say no here and run these steps by hand once they
162
+ # have connectivity. Wrapped in `after_bundle` (Rails' own template mechanism
163
+ # for "run this once the gems this template just added are actually bundled")
164
+ # so it runs after — never before — the gems above are installed.
165
+ if options[:asset_pipeline].to_s != "sprockets"
166
+ say_status :warning,
167
+ "this template assumes --asset-pipeline=sprockets (got #{options[:asset_pipeline].inspect}) " \
168
+ "-- rails_admin:install below is still configured for sprockets regardless, see the invocation " \
169
+ "documented in this gem's README.",
170
+ :yellow
171
+ end
172
+
173
+ run_setup_now = yes?(
174
+ "Run `bundle install` and the standard installer generators (devise, rails_admin, " \
175
+ "active_storage, action_text, action_mailbox, cancan, erd) now? (y/n)"
176
+ )
177
+
178
+ after_bundle do
179
+ next unless run_setup_now
180
+
181
+ # `bundle_command`/`rails_command` never abort on failure by themselves (verified
182
+ # against railties' own source: `bundle_command` is a bare `system` call with no
183
+ # result check at all; `rails_command` only aborts when `abort_on_failure: true` is
184
+ # passed explicitly -- `generate` sets that internally, `rails_command` does not).
185
+ # createApp.js's own equivalent was one `&&`-joined shell command that failed the
186
+ # whole chain atomically on any step; matched here explicitly rather than silently
187
+ # limping on into later steps against a broken bundle/app.
188
+ abort("bundle install failed -- aborting the app template's installer chain") unless bundle_command("install")
189
+
190
+ generate "devise:install"
191
+
192
+ # The `_namespace` positional argument ("app") is RailsAdmin::InstallGenerator's own
193
+ # mount-path argument (not a placeholder/app-name) -- passing a non-blank value here
194
+ # is what makes it skip its interactive "Where do you want to mount rails_admin?"
195
+ # prompt (verified against its source), which nothing in this non-interactive chain
196
+ # could ever answer. The resulting namespace is irrelevant: `thecore_ui_rails_admin`
197
+ # already mounts `RailsAdmin::Engine` itself, in its own engine routes (see that
198
+ # gem's `config/routes.rb`) -- a second, active mount in *this* app's routes.rb would
199
+ # be a genuine duplicate route, not just cosmetic. `route(...)` below writes a
200
+ # commented placeholder line containing the exact substring
201
+ # (`"mount RailsAdmin::Engine"`) the installer checks for
202
+ # (`routes.rb.include?('mount RailsAdmin::Engine')`) *before* calling it, so it skips
203
+ # inserting an active one at all -- more robust than adding one and regex-stripping
204
+ # it back out afterward (a future RailsAdmin release reformatting its generated line
205
+ # would silently break a regex-based removal with no error raised anywhere).
206
+ route "# mount RailsAdmin::Engine -- already mounted by thecore_ui_rails_admin's own engine routes, see that gem's config/routes.rb"
207
+ generate "rails_admin:install", "app", "--asset=sprockets"
208
+
209
+ rails_command "active_storage:install", abort_on_failure: true
210
+ rails_command "action_text:install", abort_on_failure: true
211
+ # action_text:install (above) may add its own `image_processing` Gemfile dependency
212
+ # (verified against its source) -- this bundle is the one that actually picks that
213
+ # up; devise:install/rails_admin:install above added nothing new to bundle beyond
214
+ # what the first `bundle_command("install")` already covered, so no bundle call sits
215
+ # between them.
216
+ abort("bundle install failed -- aborting the app template's installer chain") unless bundle_command("install")
217
+ rails_command "action_mailbox:install", abort_on_failure: true
218
+ generate "cancan:ability"
219
+ generate "erd:install"
220
+ end