rolemodel-rails 2.7.1 → 2.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.
- checksums.yaml +4 -4
- data/README.md +11 -3
- data/exe/rmr +6 -0
- data/lib/generators/rolemodel/README.md +1 -0
- data/lib/generators/rolemodel/all_generator.rb +1 -0
- data/lib/generators/rolemodel/core_setup/README.md +1 -0
- data/lib/generators/rolemodel/core_setup/core_setup_generator.rb +1 -0
- data/lib/generators/rolemodel/dotenv/README.md +110 -0
- data/lib/generators/rolemodel/dotenv/USAGE +15 -0
- data/lib/generators/rolemodel/dotenv/dotenv_generator.rb +43 -0
- data/lib/generators/rolemodel/dotenv/templates/lib/env_value.rb +66 -0
- data/lib/generators/rolemodel/dotenv/templates/spec/lib/env_value_spec.rb +178 -0
- data/lib/generators/rolemodel/editors/editors_generator.rb +10 -13
- data/lib/generators/rolemodel/good_job/good_job_generator.rb +5 -1
- data/lib/generators/rolemodel/good_job/templates/config/initializers/good_job.rb +1 -1
- data/lib/generators/rolemodel/heroku/heroku_generator.rb +4 -9
- data/lib/generators/rolemodel/heroku/templates/deploy-app/SKILL.md +1 -1
- data/lib/generators/rolemodel/linters/eslint/eslint_generator.rb +0 -1
- data/lib/generators/rolemodel/mailers/mailers_generator.rb +27 -47
- data/lib/generators/rolemodel/mailers/templates/app/mailers/staging_mailer_interceptor.rb +2 -3
- data/lib/generators/rolemodel/readme/templates/README.md.erb +3 -0
- data/lib/generators/rolemodel/saas/devise/devise_generator.rb +3 -23
- data/lib/generators/rolemodel/sentry/sentry_generator.rb +1 -0
- data/lib/generators/rolemodel/sentry/templates/config/initializers/sentry.rb +1 -1
- data/lib/generators/rolemodel/source_map/README.md +1 -1
- data/lib/generators/rolemodel/source_map/source_map_generator.rb +8 -4
- data/lib/generators/rolemodel/source_map/templates/lib/middleware/rolemodel/source_map.rb +1 -1
- data/lib/generators/rolemodel/testing/jasmine_playwright/jasmine_playwright_generator.rb +0 -1
- data/lib/generators/rolemodel/testing/vitest/vitest_generator.rb +0 -1
- data/lib/rolemodel/file_content_helper.rb +191 -0
- data/lib/rolemodel/generator_base.rb +41 -2
- data/lib/rolemodel/new_app/template.rb +7 -0
- data/lib/rolemodel/new_app.rb +22 -0
- data/lib/rolemodel/version.rb +1 -1
- metadata +12 -4
- data/lib/generators/rolemodel/editors/vscode_helpers.rb +0 -22
- data/lib/rolemodel/replace_content_helper.rb +0 -29
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2eae463b96705a0478a97b06daf586e4022499a8d53cca46ba6c91ab2cf1d41c
|
|
4
|
+
data.tar.gz: 174fcab3a6f7b8a4e29434a0ee123a9b853fb3ef7006e9cefc1199721083bbe6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ab6a232a25594bf86544265511deb2174f148ed29bfd945f9f561ac19cbeed3c26503206045b5f3dfc3065b83ad049d9ea821e9558e8a7238fa512cbc722ca88
|
|
7
|
+
data.tar.gz: a12296267725fd071c2db0fe5a57ebc4fa623f18a55b19c109d0399ad7be19df00c4f5c412da46f93ccac8bac50d7f8eb0a468766c9caf66a417ccbcb6a97226
|
data/README.md
CHANGED
|
@@ -11,14 +11,21 @@ Attempts to solve the pain of:
|
|
|
11
11
|
* Our BestPractice learns don't often get ported into other projects as it isn't straightforward to do so
|
|
12
12
|
* There is an emerging pattern of libraries using generators (e.g. webpacker) to migrate a setup between library versions
|
|
13
13
|
|
|
14
|
-
##
|
|
14
|
+
## New Apps
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
Start a new app with `rmr`, which wraps `rails new`:
|
|
17
17
|
|
|
18
18
|
```shell
|
|
19
|
-
|
|
19
|
+
gem install rolemodel-rails
|
|
20
|
+
rmr new <app-name>
|
|
20
21
|
```
|
|
21
22
|
|
|
23
|
+
`rmr` passes `--database=postgresql --javascript=webpack --skip-kamal --skip-solid --skip-test` and a template that adds this gem and runs `rolemodel:core_setup`, which asks for an icon library (default Material). Flags in your `~/.railsrc` override those defaults, and flags on the command line override both.
|
|
24
|
+
|
|
25
|
+
## Existing Apps
|
|
26
|
+
|
|
27
|
+
The rolemodel-rails gem can be added to an existing Rails project.
|
|
28
|
+
|
|
22
29
|
The Devise generator requires your database to exist before running.
|
|
23
30
|
|
|
24
31
|
```shell
|
|
@@ -109,6 +116,7 @@ bin/rails g
|
|
|
109
116
|
* [Tailored Select](./lib/generators/rolemodel/tailored_select)
|
|
110
117
|
* [Tom Select](./lib/generators/rolemodel/tom_select)
|
|
111
118
|
* [Lograge](./lib/generators/rolemodel/lograge)
|
|
119
|
+
* [Dotenv](./lib/generators/rolemodel/dotenv)
|
|
112
120
|
|
|
113
121
|
## Agent Skills
|
|
114
122
|
|
data/exe/rmr
ADDED
|
@@ -19,6 +19,7 @@ app you can push straight to Heroku right after generation.
|
|
|
19
19
|
|
|
20
20
|
## What you get
|
|
21
21
|
|
|
22
|
+
- [Dotenv](../dotenv) — local settings in `.env.local`, and `EnvValue` to read them
|
|
22
23
|
- [Slim](../slim) — Slim templates
|
|
23
24
|
- [Webpack](../webpack) — Webpack v5 for JS and CSS
|
|
24
25
|
- [Optics](../optics) — Optics design system and icons
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Dotenv Generator
|
|
2
|
+
|
|
3
|
+
[USAGE](./USAGE)
|
|
4
|
+
|
|
5
|
+
Sets up local settings in a `.env.local` file, and adds `EnvValue` to read settings from ENV.
|
|
6
|
+
|
|
7
|
+
Run it with:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
bin/rails generate rolemodel:dotenv
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## What you get
|
|
14
|
+
|
|
15
|
+
- The `dotenv` gem in the `development` and `test` groups.
|
|
16
|
+
- `/.env*` in `.gitignore`, so Git never commits your local values. New Rails apps already ignore it.
|
|
17
|
+
- `lib/env_value.rb`, required from `config/application.rb`.
|
|
18
|
+
- `spec/lib/env_value_spec.rb`, when the app has a `spec` folder.
|
|
19
|
+
|
|
20
|
+
## Put a local value in `.env.local`
|
|
21
|
+
|
|
22
|
+
Create `.env.local` in the project root. Add one `KEY=value` line for each setting:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
SENDGRID_API_KEY=xxxxxxxxx
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Restart the server after you change the file.
|
|
29
|
+
|
|
30
|
+
To copy values from a Heroku app, run `heroku config -s -a <app>`. It prints `KEY=value` lines. Paste the lines you need into `.env.local`.
|
|
31
|
+
|
|
32
|
+
The dotenv gem loads `.env.local` in development. Tests don't read `.env.local`, so a test run never depends on your machine. Put a value that tests need in `.env.test`, or give it a `local:` or `default:` value (see [Defaults for each environment](#defaults-for-each-environment)). Production reads its values from the host, for example Heroku config vars.
|
|
33
|
+
|
|
34
|
+
## Read a setting with `EnvValue`
|
|
35
|
+
|
|
36
|
+
`EnvValue` reads a value from ENV. A blank value counts as unset. When the value is unset, `EnvValue` uses a default.
|
|
37
|
+
|
|
38
|
+
Read each setting once in `config/application.rb`, and keep it as a `config.x` value:
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
config.x.sendgrid.api_key = EnvValue.string('SENDGRID_API_KEY')
|
|
42
|
+
config.x.report_emails = EnvValue.array('REPORT_EMAILS', default: %w[team@example.com])
|
|
43
|
+
config.x.serve_static_files = EnvValue.boolean('RAILS_SERVE_STATIC_FILES')
|
|
44
|
+
config.x.default_url = EnvValue.url('DEFAULT_URL', default: 'http://localhost:3000')
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Then read it in app code:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
Rails.configuration.x.sendgrid.api_key
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
| Method | Returns | When unset, with no default | When the value is not valid |
|
|
54
|
+
|---|---|---|---|
|
|
55
|
+
| `string` | the text | `nil` | — |
|
|
56
|
+
| `string!` | the text | raises | raises when blank |
|
|
57
|
+
| `array` | the comma-separated items, trimmed | `[]` | — |
|
|
58
|
+
| `boolean` | `true` for `true`, `t`, `1`, `yes`, `y`, `on`, `enabled`. `false` for `false`, `f`, `0`, `no`, `n`, `off`, `disabled`. Case does not matter. | `false` | raises on any other text, naming the key |
|
|
59
|
+
| `url` | the URL | `nil` | raises when it has no scheme or host |
|
|
60
|
+
|
|
61
|
+
Read settings in `config/application.rb`, so a raise stops the app when it starts. Then you see a bad value at once.
|
|
62
|
+
|
|
63
|
+
### Defaults for each environment
|
|
64
|
+
|
|
65
|
+
A default can be different in each environment. `EnvValue` uses the first of these that applies:
|
|
66
|
+
|
|
67
|
+
1. A key named for the current environment, such as `production:` or `staging:`. A key set to `nil` counts, and gives `nil`.
|
|
68
|
+
2. `local:`, in development and test only.
|
|
69
|
+
3. A block.
|
|
70
|
+
4. `default:`.
|
|
71
|
+
|
|
72
|
+
```ruby
|
|
73
|
+
# Every environment except production falls back to dev-key. Staging gets dev-key too.
|
|
74
|
+
# Production gets nil unless ENCRYPTION_KEY is set.
|
|
75
|
+
EnvValue.string('ENCRYPTION_KEY', default: 'dev-key', production: nil)
|
|
76
|
+
|
|
77
|
+
# Only development and test fall back to dev-client-id. Staging and production get nil.
|
|
78
|
+
EnvValue.string('API_CLIENT_ID', local: 'dev-client-id')
|
|
79
|
+
|
|
80
|
+
# The block runs only when the key is unset.
|
|
81
|
+
EnvValue.url('DEFAULT_URL') { "https://#{EnvValue.string('HEROKU_APP_NAME')}.herokuapp.com" }
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Move from `env.rb`
|
|
85
|
+
|
|
86
|
+
Some RoleModel apps keep local settings in `env.rb` and `.env_overrides.rb`. Those are Ruby files, so only Ruby can read them. A `.env` file holds plain `KEY=value` lines that Ruby, Node, Docker Compose, foreman and 1Password can all read.
|
|
87
|
+
|
|
88
|
+
If the app has `env.rb` or `.env_overrides.rb`:
|
|
89
|
+
|
|
90
|
+
1. Run the generator.
|
|
91
|
+
2. Move each `ENV['KEY'] ||= 'value'` line from `env.rb` and `.env_overrides.rb` to `.env.local` as `KEY=value`.
|
|
92
|
+
3. If `env.rb` set a value that production uses, add it as a default in `config/application.rb` with `EnvValue`.
|
|
93
|
+
4. Remove the line that requires `env.rb` from `config/application.rb`.
|
|
94
|
+
5. Delete `env.rb` and `.env_overrides.rb`.
|
|
95
|
+
|
|
96
|
+
## Generators that add settings
|
|
97
|
+
|
|
98
|
+
These generators add their settings to `config/application.rb` with `EnvValue`. If the app has no `lib/env_value.rb`, they run this generator first.
|
|
99
|
+
|
|
100
|
+
| Generator | ENV key | Setting |
|
|
101
|
+
|---|---|---|
|
|
102
|
+
| `rolemodel:heroku` | `LOG_LEVEL` | `config.x.log_level` (default `INFO`) |
|
|
103
|
+
| `rolemodel:sentry` | `SENTRY_DSN` | `config.x.sentry.dsn` |
|
|
104
|
+
| `rolemodel:mailers` | `WHITELISTED_EMAILS` | `config.x.whitelisted_emails` |
|
|
105
|
+
| | `DEFAULT_URL` | `config.x.default_url`: the app's full URL, such as `https://app.example.com`, used for links and assets in emails. Set it in production. Without it, a Heroku review app uses `https://<HEROKU_APP_NAME>.herokuapp.com`, and development uses `http://localhost:3000`. It replaces `PRODUCTION_HOST` and `REVIEW_APP`. The generator comments out the `default_url_options` line in development.rb and production.rb, so set the URL here, not there. |
|
|
106
|
+
| | `HEROKU_APP_NAME` | `config.x.heroku_app_name` |
|
|
107
|
+
| | `SENDGRID_API_KEY` | `config.x.sendgrid.api_key` |
|
|
108
|
+
| `rolemodel:saas:devise` | `DEFAULT_URL` | `config.x.default_url`, the same setting as `rolemodel:mailers` |
|
|
109
|
+
| `rolemodel:source_map` | `SOURCE_MAPS_ALLOWED_USERS_EMAILS` | `config.x.source_maps.allowed_user_emails` |
|
|
110
|
+
| `rolemodel:good_job` | `GOOD_JOB_QUEUES` | `config.x.good_job.queues` (default `pdf:1;-pdf`) |
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Description:
|
|
2
|
+
Loads local settings from a .env.local file with the dotenv gem,
|
|
3
|
+
and adds EnvValue to read typed settings from ENV.
|
|
4
|
+
|
|
5
|
+
Example:
|
|
6
|
+
rails generate rolemodel:dotenv
|
|
7
|
+
|
|
8
|
+
This will create:
|
|
9
|
+
lib/env_value.rb
|
|
10
|
+
spec/lib/env_value_spec.rb (when the app has a spec folder)
|
|
11
|
+
|
|
12
|
+
This will change:
|
|
13
|
+
Gemfile (adds dotenv to the development and test groups)
|
|
14
|
+
config/application.rb (requires lib/env_value)
|
|
15
|
+
.gitignore (ignores /.env*)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rolemodel
|
|
4
|
+
class DotenvGenerator < GeneratorBase
|
|
5
|
+
source_root File.expand_path('templates', __dir__)
|
|
6
|
+
|
|
7
|
+
def install_dotenv
|
|
8
|
+
say 'Installing dotenv', :green
|
|
9
|
+
|
|
10
|
+
bundle_command('add dotenv --group "development, test"')
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# New Rails apps already ignore /.env*. Older apps may not.
|
|
14
|
+
def ignore_env_files
|
|
15
|
+
gitignore = File.join(destination_root, '.gitignore')
|
|
16
|
+
content = File.exist?(gitignore) ? File.read(gitignore) : ''
|
|
17
|
+
return if content.include?('/.env*')
|
|
18
|
+
|
|
19
|
+
create_file '.gitignore', content + <<~IGNORE, force: true
|
|
20
|
+
|
|
21
|
+
# Ignore all environment files.
|
|
22
|
+
/.env*
|
|
23
|
+
IGNORE
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def add_env_value
|
|
27
|
+
say 'Adding EnvValue to read settings from ENV', :green
|
|
28
|
+
|
|
29
|
+
copy_file 'lib/env_value.rb'
|
|
30
|
+
inject_into_file(
|
|
31
|
+
'config/application.rb',
|
|
32
|
+
"require_relative '../lib/env_value'\n",
|
|
33
|
+
before: /^# Require the gems listed in Gemfile/
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def add_env_value_spec
|
|
38
|
+
return unless Dir.exist?(File.join(destination_root, 'spec'))
|
|
39
|
+
|
|
40
|
+
copy_file 'spec/lib/env_value_spec.rb'
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Reads typed settings from ENV. A blank value counts as unset. When it is unset, the
|
|
4
|
+
# default comes from the first of these that applies: a key named for the current
|
|
5
|
+
# environment, `local:` in development and test, the block, then `default:`.
|
|
6
|
+
#
|
|
7
|
+
# EnvValue.string('DEFAULT_URL') { 'http://localhost:3000' }
|
|
8
|
+
# EnvValue.string('API_CLIENT_ID', local: 'dev-client-id')
|
|
9
|
+
# EnvValue.string!('FROM') # raises KeyError when unset, ArgumentError when blank
|
|
10
|
+
# EnvValue.url('DEFAULT_URL') # raises ArgumentError without a scheme and host
|
|
11
|
+
# EnvValue.string('ENCRYPTION_KEY', default: 'dev-key', production: nil)
|
|
12
|
+
# EnvValue.array('REPORT_EMAILS', default: %w[someone@example.com])
|
|
13
|
+
# EnvValue.boolean('FEATURE_ON', default: true)
|
|
14
|
+
module EnvValue
|
|
15
|
+
ENVIRONMENTS = %i[development test staging production local default].freeze
|
|
16
|
+
TRUE_WORDS = %w[true t 1 yes y on enabled].freeze
|
|
17
|
+
FALSE_WORDS = %w[false f 0 no n off disabled].freeze
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
def string(key, **defaults, &)
|
|
22
|
+
read(key) || fallback(defaults, &)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def string!(key)
|
|
26
|
+
ENV.fetch(key).presence || raise(ArgumentError, "#{key} is blank")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def url(key, **defaults, &)
|
|
30
|
+
value = string(key, **defaults, &)
|
|
31
|
+
return value if value.nil? || URI.parse(value).then { it.scheme && it.host }
|
|
32
|
+
|
|
33
|
+
raise ArgumentError, "#{key} must be a full URL, like https://example.com, not #{value.inspect}"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def array(key, **defaults, &)
|
|
37
|
+
value = read(key)
|
|
38
|
+
value ? value.split(',').map(&:strip).compact_blank : fallback(defaults, &) || []
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def boolean(key, **defaults, &)
|
|
42
|
+
value = read(key)&.downcase
|
|
43
|
+
return fallback(defaults, &) || false unless value
|
|
44
|
+
return true if TRUE_WORDS.include?(value)
|
|
45
|
+
return false if FALSE_WORDS.include?(value)
|
|
46
|
+
|
|
47
|
+
raise ArgumentError, "#{key} must be one of #{(TRUE_WORDS + FALSE_WORDS).join(', ')}, not #{value.inspect}"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def read(key)
|
|
51
|
+
ENV.fetch(key, nil).presence
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def fallback(defaults)
|
|
55
|
+
unknown = defaults.keys - ENVIRONMENTS
|
|
56
|
+
raise ArgumentError, "unknown environment: #{unknown.join(', ')}" if unknown.any?
|
|
57
|
+
|
|
58
|
+
environment = Rails.env.to_sym
|
|
59
|
+
if defaults.key?(environment) then defaults[environment]
|
|
60
|
+
elsif Rails.env.local? && defaults.key?(:local) then defaults[:local]
|
|
61
|
+
elsif block_given? then yield
|
|
62
|
+
else defaults[:default]
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
private_class_method :read, :fallback
|
|
66
|
+
end
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rails_helper'
|
|
4
|
+
|
|
5
|
+
RSpec.describe EnvValue do
|
|
6
|
+
let(:key) { 'ENV_VALUE_SPEC' }
|
|
7
|
+
|
|
8
|
+
around do |example|
|
|
9
|
+
original = ENV.fetch(key, nil)
|
|
10
|
+
example.run
|
|
11
|
+
ensure
|
|
12
|
+
ENV[key] = original
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def in_environment(name)
|
|
16
|
+
allow(Rails).to receive(:env).and_return(ActiveSupport::EnvironmentInquirer.new(name))
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe '.string' do
|
|
20
|
+
it 'reads the value' do
|
|
21
|
+
ENV[key] = 'https://example.test'
|
|
22
|
+
|
|
23
|
+
expect(described_class.string(key, default: 'fallback')).to eq 'https://example.test'
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it 'falls back to the default when the key is missing or blank' do
|
|
27
|
+
ENV[key] = nil
|
|
28
|
+
expect(described_class.string(key, default: 'fallback')).to eq 'fallback'
|
|
29
|
+
|
|
30
|
+
ENV[key] = ' '
|
|
31
|
+
expect(described_class.string(key, default: 'fallback')).to eq 'fallback'
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it 'computes the default from a block only when it is needed' do
|
|
35
|
+
ENV[key] = 'set'
|
|
36
|
+
expect(described_class.string(key) { raise 'should not run' }).to eq 'set'
|
|
37
|
+
|
|
38
|
+
ENV[key] = nil
|
|
39
|
+
expect(described_class.string(key) { 'computed' }).to eq 'computed'
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it 'is nil without a value or a default' do
|
|
43
|
+
ENV[key] = nil
|
|
44
|
+
|
|
45
|
+
expect(described_class.string(key)).to be_nil
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe '.string!' do
|
|
50
|
+
it 'reads the value' do
|
|
51
|
+
ENV[key] = '2026-09-01'
|
|
52
|
+
|
|
53
|
+
expect(described_class.string!(key)).to eq '2026-09-01'
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it 'raises KeyError when the key is missing, as ENV.fetch does' do
|
|
57
|
+
ENV[key] = nil
|
|
58
|
+
|
|
59
|
+
expect { described_class.string!(key) }.to raise_error(KeyError, /ENV_VALUE_SPEC/)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it 'raises ArgumentError, naming the key, when the value is blank' do
|
|
63
|
+
ENV[key] = ' '
|
|
64
|
+
|
|
65
|
+
expect { described_class.string!(key) }.to raise_error(ArgumentError, 'ENV_VALUE_SPEC is blank')
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
describe '.url' do
|
|
70
|
+
it 'reads a full URL' do
|
|
71
|
+
ENV[key] = 'https://example.test'
|
|
72
|
+
|
|
73
|
+
expect(described_class.url(key)).to eq 'https://example.test'
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it 'checks a default the same way' do
|
|
77
|
+
ENV[key] = nil
|
|
78
|
+
|
|
79
|
+
expect(described_class.url(key) { 'http://localhost:3000' }).to eq 'http://localhost:3000'
|
|
80
|
+
expect { described_class.url(key, default: 'localhost:3000') }.to raise_error(ArgumentError)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it 'rejects a value without a scheme, naming the key' do
|
|
84
|
+
ENV[key] = 'example.test'
|
|
85
|
+
|
|
86
|
+
expect { described_class.url(key) }.to raise_error(ArgumentError, /ENV_VALUE_SPEC must be a full URL/)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it 'is nil without a value or a default' do
|
|
90
|
+
ENV[key] = nil
|
|
91
|
+
|
|
92
|
+
expect(described_class.url(key)).to be_nil
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
describe 'defaults per environment' do
|
|
97
|
+
before { ENV[key] = nil }
|
|
98
|
+
|
|
99
|
+
it 'uses the default named for the current environment' do
|
|
100
|
+
in_environment('staging')
|
|
101
|
+
|
|
102
|
+
expect(described_class.string(key, default: 'any', staging: 'review')).to eq 'review'
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it 'uses the local default in development and test' do
|
|
106
|
+
in_environment('development')
|
|
107
|
+
expect(described_class.string(key, local: 'asdf')).to eq 'asdf'
|
|
108
|
+
|
|
109
|
+
in_environment('production')
|
|
110
|
+
expect(described_class.string(key, local: 'asdf')).to be_nil
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it 'prefers the named environment over local, and local over the plain default' do
|
|
114
|
+
in_environment('test')
|
|
115
|
+
|
|
116
|
+
expect(described_class.string(key, default: 'any', local: 'local', test: 'test')).to eq 'test'
|
|
117
|
+
expect(described_class.string(key, default: 'any', local: 'local')).to eq 'local'
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
it 'lets an environment opt out of the default with nil' do
|
|
121
|
+
in_environment('production')
|
|
122
|
+
|
|
123
|
+
expect(described_class.string(key, default: 'dev-key', production: nil)).to be_nil
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
it 'still prefers a set value over any default' do
|
|
127
|
+
in_environment('development')
|
|
128
|
+
ENV[key] = 'set'
|
|
129
|
+
|
|
130
|
+
expect(described_class.string(key, local: 'asdf')).to eq 'set'
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
it 'rejects an environment it does not know, to catch typos' do
|
|
134
|
+
expect { described_class.string(key, prodution: nil) }.to raise_error(ArgumentError, /prodution/)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
describe '.array' do
|
|
139
|
+
it 'splits a comma-separated value and trims each item' do
|
|
140
|
+
ENV[key] = 'a@example.com, b@example.com,,c@example.com '
|
|
141
|
+
|
|
142
|
+
expect(described_class.array(key)).to eq %w[a@example.com b@example.com c@example.com]
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
it 'falls back to the default when the key is missing' do
|
|
146
|
+
ENV[key] = nil
|
|
147
|
+
|
|
148
|
+
expect(described_class.array(key, default: %w[fallback])).to eq %w[fallback]
|
|
149
|
+
expect(described_class.array(key)).to eq []
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
describe '.boolean' do
|
|
154
|
+
it 'reads the usual words for true and false, in any case' do
|
|
155
|
+
{ 'true' => true, 'TRUE' => true, 't' => true, '1' => true, 'yes' => true, 'Y' => true, 'on' => true,
|
|
156
|
+
'enabled' => true,
|
|
157
|
+
'false' => false, 'f' => false, '0' => false, 'no' => false, 'n' => false, 'Off' => false,
|
|
158
|
+
'disabled' => false, }.each do |value, expected|
|
|
159
|
+
ENV[key] = value
|
|
160
|
+
|
|
161
|
+
expect(described_class.boolean(key)).to be(expected), "expected #{value.inspect} to be #{expected}"
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
it 'rejects any other value rather than guessing, naming the key' do
|
|
166
|
+
ENV[key] = 'nope'
|
|
167
|
+
|
|
168
|
+
expect { described_class.boolean(key) }.to raise_error(ArgumentError, /ENV_VALUE_SPEC/)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
it 'falls back to the default when the key is missing' do
|
|
172
|
+
ENV[key] = nil
|
|
173
|
+
|
|
174
|
+
expect(described_class.boolean(key, default: true)).to be true
|
|
175
|
+
expect(described_class.boolean(key)).to be false
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative 'vscode_helpers'
|
|
4
|
-
|
|
5
3
|
module Rolemodel
|
|
6
4
|
# Add standard editorconfig and any extensions to enable it
|
|
7
5
|
class EditorsGenerator < GeneratorBase
|
|
8
|
-
include Rolemodel::VSCodeHelpers
|
|
9
|
-
|
|
10
6
|
# This is bringing in the root from this gem, so we only modify
|
|
11
7
|
# one copy of .editorconfig for both this repo and any consumers.
|
|
12
8
|
source_root File.expand_path('../../../../', __dir__)
|
|
@@ -15,15 +11,16 @@ module Rolemodel
|
|
|
15
11
|
# Bring over the .editorconfig file for whitespace, etc.
|
|
16
12
|
copy_file '.editorconfig'
|
|
17
13
|
|
|
18
|
-
#
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
#
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
14
|
+
# Recommend the EditorConfig extension in VSCode. This creates .vscode/extensions.json
|
|
15
|
+
# when it is missing. Presumably in the future, we might add ESLint, Rubocop, etc.
|
|
16
|
+
# extensions, or config files for other RM-supported editors.
|
|
17
|
+
# VS Code extension IDs ignore case, so an existing 'editorconfig.editorconfig' counts.
|
|
18
|
+
modify_json_file('.vscode/extensions.json') do |config|
|
|
19
|
+
recommendations = Array(config['recommendations'])
|
|
20
|
+
unless recommendations.any? { it.casecmp?('EditorConfig.EditorConfig') }
|
|
21
|
+
recommendations << 'EditorConfig.EditorConfig'
|
|
22
|
+
end
|
|
23
|
+
config['recommendations'] = recommendations
|
|
27
24
|
end
|
|
28
25
|
end
|
|
29
26
|
end
|
|
@@ -22,7 +22,10 @@ module Rolemodel
|
|
|
22
22
|
|
|
23
23
|
RUBY
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
# production.rb runs after application.rb, so an adapter it sets, such as Solid Queue's,
|
|
26
|
+
# would win. Comment out only a line that sets one; new apps made with --skip-solid
|
|
27
|
+
# already have it as a comment.
|
|
28
|
+
comment_lines 'config/environments/production.rb', /config\.active_job\.queue_adapter\s*=/
|
|
26
29
|
end
|
|
27
30
|
|
|
28
31
|
def configure_good_job_routes
|
|
@@ -55,6 +58,7 @@ module Rolemodel
|
|
|
55
58
|
def copy_initializers
|
|
56
59
|
say 'Add GoodJob initializers'
|
|
57
60
|
|
|
61
|
+
add_setting "config.x.good_job.queues = EnvValue.string('GOOD_JOB_QUEUES', default: 'pdf:1;-pdf')"
|
|
58
62
|
directory 'config/initializers'
|
|
59
63
|
end
|
|
60
64
|
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
Rails.application.configure do
|
|
12
12
|
config.good_job = {
|
|
13
13
|
smaller_number_is_higher_priority: true, # new default in V4 and for all of ActiveJob
|
|
14
|
-
queues:
|
|
14
|
+
queues: config.x.good_job.queues, # see note above
|
|
15
15
|
execution_mode: Rails.env.test? ? :inline : :external # see note above
|
|
16
16
|
# enable_cron: true,
|
|
17
17
|
# cron: {
|
|
@@ -27,21 +27,16 @@ module Rolemodel
|
|
|
27
27
|
inject_into_file 'Gemfile', "\nruby file: '.ruby-version'\n", after: /^source .*$/
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
def force_ssl
|
|
31
|
-
say 'Require SSL for production environment.', :green
|
|
32
|
-
|
|
33
|
-
uncomment_lines('config/environments/production.rb', 'config.force_ssl = true')
|
|
34
|
-
end
|
|
35
|
-
|
|
36
30
|
def enable_log_level_configurability
|
|
37
31
|
say 'Enable log-level adjustment via "LOG_LEVEL" environment variable', :green
|
|
38
32
|
|
|
33
|
+
add_setting "config.x.log_level = EnvValue.string('LOG_LEVEL', default: 'INFO')"
|
|
34
|
+
|
|
39
35
|
gsub_file(
|
|
40
36
|
'config/environments/production.rb',
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
/config\.log_level = (ENV\.fetch\("RAILS_LOG_LEVEL", "info"\)|:info)/,
|
|
38
|
+
'config.log_level = config.x.log_level'
|
|
43
39
|
)
|
|
44
|
-
gsub_file('config/environments/production.rb', 'config.log_level = :info', "config.log_level = ENV.fetch('LOG_LEVEL', 'INFO')")
|
|
45
40
|
end
|
|
46
41
|
|
|
47
42
|
def create_assets_rake_tasks # rubocop:disable Metrics/MethodLength
|
|
@@ -41,7 +41,7 @@ exception). Stop and tell the user how to fix any that fail:
|
|
|
41
41
|
fall back to another mechanism.
|
|
42
42
|
5. Read `Procfile` — note whether a `worker` process exists.
|
|
43
43
|
6. Read `config/initializers/sentry.rb` — determine how the DSN is consumed:
|
|
44
|
-
- reads `ENV['SENTRY_DSN']` → **env-var mode** (RoleModel standard)
|
|
44
|
+
- reads `Rails.configuration.x.sentry.dsn` or `ENV['SENTRY_DSN']` → **env-var mode** (RoleModel standard)
|
|
45
45
|
- reads `Rails.application.credentials...` → **credentials mode**
|
|
46
46
|
7. Deploy-readiness (recent `rolemodel-rails` bakes these in; older apps need the manual
|
|
47
47
|
fixes noted in each phase). Confirm the generated app already has:
|
|
@@ -17,60 +17,40 @@ module Rolemodel
|
|
|
17
17
|
after: /^\s*require\('postcss-preset-env'\)\({(.|\n)*?}\)/
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
# Default mailing host suggested by Devise installation instructions
|
|
27
|
-
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
|
|
28
|
-
RUBY
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
inject_into_file 'config/environments/development.rb',
|
|
33
|
-
after: "config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }\n" do
|
|
34
|
-
optimize_indentation <<~'RUBY', 2
|
|
35
|
-
|
|
36
|
-
# Ensure premailer_rails can point to webpack compiled resources
|
|
37
|
-
# https://github.com/fphilipe/premailer-rails/issues/232#issuecomment-839819705
|
|
38
|
-
config.action_mailer.asset_host = 'http://localhost:3000'
|
|
39
|
-
RUBY
|
|
40
|
-
end
|
|
20
|
+
# Email links and assets use DEFAULT_URL in every environment. The asset host lets
|
|
21
|
+
# premailer_rails find webpack compiled resources:
|
|
22
|
+
# https://github.com/fphilipe/premailer-rails/issues/232#issuecomment-839819705
|
|
23
|
+
def use_default_url
|
|
24
|
+
add_default_url
|
|
41
25
|
end
|
|
42
26
|
|
|
43
27
|
def add_production_mailer_defaults
|
|
28
|
+
add_setting "config.x.whitelisted_emails = EnvValue.array('WHITELISTED_EMAILS')"
|
|
29
|
+
add_setting "config.x.heroku_app_name = EnvValue.string('HEROKU_APP_NAME')"
|
|
30
|
+
add_setting "config.x.sendgrid.api_key = EnvValue.string('SENDGRID_API_KEY')"
|
|
31
|
+
|
|
32
|
+
copy_file 'app/mailers/staging_mailer_interceptor.rb'
|
|
44
33
|
prepend_to_file 'config/environments/production.rb',
|
|
45
34
|
"require Rails.root.join('app/mailers/staging_mailer_interceptor')\n"
|
|
46
35
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
host = ENV['PRODUCTION_HOST']
|
|
55
|
-
|
|
56
|
-
# Ensure premailer_rails can point to webpack compiled resources
|
|
57
|
-
# https://github.com/fphilipe/premailer-rails/issues/232#issuecomment-839819705
|
|
58
|
-
config.action_mailer.asset_host = host
|
|
36
|
+
append_to_environment 'production', <<~'RUBY'
|
|
37
|
+
# Prevent live user emails from being sent out in staging
|
|
38
|
+
if config.x.whitelisted_emails.any?
|
|
39
|
+
ActionMailer::Base.register_interceptor(StagingMailerInterceptor)
|
|
40
|
+
end
|
|
59
41
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
RUBY
|
|
73
|
-
end
|
|
42
|
+
config.action_mailer.delivery_method = :smtp
|
|
43
|
+
config.action_mailer.perform_deliveries = true
|
|
44
|
+
config.action_mailer.smtp_settings = {
|
|
45
|
+
user_name: 'apikey',
|
|
46
|
+
password: config.x.sendgrid.api_key,
|
|
47
|
+
domain: URI(config.x.default_url).host,
|
|
48
|
+
address: 'smtp.sendgrid.net',
|
|
49
|
+
port: 587,
|
|
50
|
+
authentication: :plain,
|
|
51
|
+
enable_starttls_auto: true
|
|
52
|
+
}
|
|
53
|
+
RUBY
|
|
74
54
|
end
|
|
75
55
|
|
|
76
56
|
def add_mailer_css
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
class StagingMailerInterceptor
|
|
2
2
|
def self.delivering_email(message)
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
subject_name = ENV['HEROKU_APP_NAME'].presence || 'STAGING'
|
|
3
|
+
message.to = message.to & Rails.configuration.x.whitelisted_emails
|
|
4
|
+
subject_name = Rails.configuration.x.heroku_app_name || 'STAGING'
|
|
6
5
|
message.subject = "#{subject_name} - #{message.subject}"
|
|
7
6
|
message.perform_deliveries = false if message.to.blank?
|
|
8
7
|
end
|
|
@@ -79,28 +79,8 @@ module Rolemodel
|
|
|
79
79
|
end
|
|
80
80
|
|
|
81
81
|
def modify_existing_files
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
after: "config.action_mailer.perform_caching = false\n" do
|
|
85
|
-
optimize_indentation <<~'RUBY', 2
|
|
86
|
-
|
|
87
|
-
# Default mailing host suggested by Devise installation instructions
|
|
88
|
-
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
|
|
89
|
-
RUBY
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
inject_into_file 'config/environments/production.rb', after: "config.action_mailer.perform_caching = false\n" do
|
|
94
|
-
optimize_indentation <<~'RUBY', 2
|
|
95
|
-
|
|
96
|
-
# Tell Devise about your host, so it can send password reset emails
|
|
97
|
-
if ENV['REVIEW_APP'] == 'true' && ENV['HEROKU_APP_NAME'].present?
|
|
98
|
-
config.action_mailer.default_url_options = { host: "https://#{ENV['HEROKU_APP_NAME']}.herokuapp.com" }
|
|
99
|
-
else
|
|
100
|
-
config.action_mailer.default_url_options = { host: ENV['PRODUCTION_HOST'] }
|
|
101
|
-
end
|
|
102
|
-
RUBY
|
|
103
|
-
end
|
|
82
|
+
# Devise links in emails, such as password resets, use DEFAULT_URL.
|
|
83
|
+
add_default_url
|
|
104
84
|
|
|
105
85
|
# Update Devise email_regexp to something more useful
|
|
106
86
|
gsub_file 'config/initializers/devise.rb', '/\A[^@\s]+@[^@\s]+\z/',
|
|
@@ -111,7 +91,7 @@ module Rolemodel
|
|
|
111
91
|
'config.validate_on_invite = true'
|
|
112
92
|
end
|
|
113
93
|
|
|
114
|
-
|
|
94
|
+
inject_into_class 'app/models/user.rb', 'User', at: :end do
|
|
115
95
|
optimize_indentation <<~'RUBY', 2
|
|
116
96
|
|
|
117
97
|
enum :role, { user: 'user', admin: 'admin' }
|
|
@@ -10,4 +10,4 @@ Adds Rack middleware for serving js source maps only to allowed users in a produ
|
|
|
10
10
|
## Usage
|
|
11
11
|
|
|
12
12
|
* Ensure that app uses Warden::Manager middleware
|
|
13
|
-
* Set
|
|
13
|
+
* Set `SOURCE_MAPS_ALLOWED_USERS_EMAILS` to a comma-separated list of emails. The middleware reads it from `Rails.configuration.x.source_maps.allowed_user_emails`. To allow all super admins instead, change `permit_user?` in the middleware.
|
|
@@ -5,13 +5,17 @@ module Rolemodel
|
|
|
5
5
|
source_root File.expand_path('templates', __dir__)
|
|
6
6
|
|
|
7
7
|
def inject_config_for_production
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
prepend_to_file(
|
|
9
|
+
'config/environments/production.rb',
|
|
10
|
+
"require_relative Rails.root.join('lib/middleware/rolemodel/source_map.rb')\n"
|
|
11
|
+
)
|
|
12
|
+
append_to_environment 'production', <<~'RUBY'
|
|
13
|
+
config.middleware.insert_after Warden::Manager, Middleware::Rolemodel::SourceMap
|
|
14
|
+
RUBY
|
|
12
15
|
end
|
|
13
16
|
|
|
14
17
|
def copy_middleware_files
|
|
18
|
+
add_setting "config.x.source_maps.allowed_user_emails = EnvValue.array('SOURCE_MAPS_ALLOWED_USERS_EMAILS')"
|
|
15
19
|
copy_file 'lib/middleware/rolemodel/source_map.rb'
|
|
16
20
|
end
|
|
17
21
|
|
|
@@ -8,7 +8,7 @@ module Middleware
|
|
|
8
8
|
default_headers = { 'Set-Cookie' => 'Same-Site=None', 'Cache-Control' => 'max-age=0;no-cache' }
|
|
9
9
|
custom_headers = default_headers.merge(options.delete(:headers) || {})
|
|
10
10
|
@app = app
|
|
11
|
-
@allowed_users_emails =
|
|
11
|
+
@allowed_users_emails = Rails.configuration.x.source_maps.allowed_user_emails
|
|
12
12
|
@file_server = Rack::Files.new(root, custom_headers)
|
|
13
13
|
end
|
|
14
14
|
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
module Rolemodel
|
|
4
4
|
module Testing
|
|
5
5
|
class JasminePlaywrightGenerator < GeneratorBase
|
|
6
|
-
include ReplaceContentHelper
|
|
7
6
|
source_root File.expand_path('templates', __dir__)
|
|
8
7
|
class_option :github_package_token, type: :string, default: ENV['GITHUB_PACKAGES_TOKEN'], desc: 'GitHub Packages token with access to @rolemodel packages'
|
|
9
8
|
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
require 'prism'
|
|
2
|
+
|
|
3
|
+
module Rolemodel
|
|
4
|
+
module FileContentHelper
|
|
5
|
+
# Finds a block for inject_into_block. `pattern` matches a Prism node by its structure, not its text.
|
|
6
|
+
# `example` is code the pattern matches. Its first line describes the matcher in error messages,
|
|
7
|
+
# and a spec checks that the pattern matches it. Build one with for_class, for_module or for_call.
|
|
8
|
+
BlockMatcher = Data.define(:example, :pattern) do
|
|
9
|
+
# A matcher for a class, by its name ('User', 'Admin::User'), its superclass
|
|
10
|
+
# ('Rails::Application'), or both. A leading :: on either name does not matter.
|
|
11
|
+
def self.for_class(name: nil, superclass: nil)
|
|
12
|
+
parent = constant_argument(superclass)
|
|
13
|
+
definition(Prism::ClassNode, 'class', name, suffix: (" < #{parent}" if parent)) do |node|
|
|
14
|
+
parent.nil? || constant_name(node.superclass) == parent
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# A matcher for a module, by its name ('Admin', 'Admin::Reports'). A leading :: does not matter.
|
|
19
|
+
def self.for_module(name)
|
|
20
|
+
definition(Prism::ModuleNode, 'module', name)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# A matcher for a method call with a do ... end block, given as the names in its chain, such as
|
|
24
|
+
# for_call(:Rails, :application, :configure). The first name may be a constant, such as :Rails or
|
|
25
|
+
# 'Admin::Settings', with or without a leading ::. Without one, the chain must start with a call
|
|
26
|
+
# that has no receiver, as in for_call(:configure). It does not compare arguments.
|
|
27
|
+
def self.for_call(first, *rest)
|
|
28
|
+
names = [constant_argument(first), *rest.map(&:to_s)]
|
|
29
|
+
new(
|
|
30
|
+
example: "#{names.join('.')} do\nend\n",
|
|
31
|
+
pattern: lambda do |node|
|
|
32
|
+
node.is_a?(Prism::CallNode) &&
|
|
33
|
+
node.block.is_a?(Prism::BlockNode) &&
|
|
34
|
+
same_chain?(node, names)
|
|
35
|
+
end
|
|
36
|
+
)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# A matcher for a class or module definition: a node of `type` whose name is `name`, when given,
|
|
40
|
+
# and that passes the block, when given.
|
|
41
|
+
def self.definition(type, keyword, name, suffix: nil, &extra)
|
|
42
|
+
name = constant_argument(name)
|
|
43
|
+
new(
|
|
44
|
+
example: "#{keyword} #{name || 'Example'}#{suffix}\nend\n",
|
|
45
|
+
pattern: lambda do |node|
|
|
46
|
+
node.is_a?(type) &&
|
|
47
|
+
(name.nil? || constant_name(node.constant_path) == name) &&
|
|
48
|
+
(extra.nil? || extra.call(node))
|
|
49
|
+
end
|
|
50
|
+
)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# A constant name given to a factory, as a String without a leading ::, or nil when not given.
|
|
54
|
+
def self.constant_argument(name)
|
|
55
|
+
name&.to_s&.delete_prefix('::')
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# A constant's full name without a leading ::, or nil when the node is not a plain constant,
|
|
59
|
+
# such as `Struct.new(:x)` or `foo::Bar`.
|
|
60
|
+
def self.constant_name(node)
|
|
61
|
+
node.full_name.delete_prefix('::') if node.respond_to?(:full_name)
|
|
62
|
+
rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Whether `node` and its receivers are the chain `names`, read from the end: each call's name
|
|
67
|
+
# in turn, then either a constant with the first name or no receiver at all.
|
|
68
|
+
def self.same_chain?(node, names)
|
|
69
|
+
return node.nil? if names.empty?
|
|
70
|
+
|
|
71
|
+
*rest, last = names
|
|
72
|
+
if node.is_a?(Prism::CallNode)
|
|
73
|
+
node.name.to_s == last && same_chain?(node.receiver, rest)
|
|
74
|
+
else
|
|
75
|
+
rest.empty? && constant_name(node) == last
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
private_class_method :definition, :constant_argument, :constant_name, :same_chain?
|
|
79
|
+
|
|
80
|
+
def call(node) = pattern.call(node)
|
|
81
|
+
def to_s = example.lines.first.strip
|
|
82
|
+
|
|
83
|
+
# The first node in `source` that the pattern matches, or nil.
|
|
84
|
+
def find_in(source) = Prism.parse(source).value.breadth_first_search { call(it) }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
def replace_content(relative_path)
|
|
90
|
+
source = File.expand_path(relative_path.to_s, destination_root)
|
|
91
|
+
content = File.exist?(source) ? File.open(source) { it.binmode.read } : ''
|
|
92
|
+
|
|
93
|
+
remove_file relative_path
|
|
94
|
+
create_file relative_path, yield(content)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def modify_json_file(relative_path)
|
|
98
|
+
replace_content(relative_path) do |content|
|
|
99
|
+
# Editor config files such as .vscode/*.json allow comments and trailing commas.
|
|
100
|
+
# Writing the file back drops the comments.
|
|
101
|
+
hash = JSON.parse(
|
|
102
|
+
content.presence || '{}',
|
|
103
|
+
allow_comments: true,
|
|
104
|
+
allow_trailing_comma: true
|
|
105
|
+
)
|
|
106
|
+
yield(hash)
|
|
107
|
+
JSON.pretty_generate(hash) + "\n"
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def add_package_json_script(command_name, command)
|
|
112
|
+
modify_json_file('package.json') do |hash|
|
|
113
|
+
hash['scripts'] ||= {}
|
|
114
|
+
hash['scripts'][command_name] = command
|
|
115
|
+
hash
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Adds code as the last lines of the `Rails.application.configure` block in an environment file,
|
|
120
|
+
# indented 2 spaces, so it overrides the Rails defaults above it.
|
|
121
|
+
# Rails' own `environment(code, env:)` adds it at the top instead.
|
|
122
|
+
def append_to_environment(env, code)
|
|
123
|
+
inject_into_block(
|
|
124
|
+
"config/environments/#{env}.rb",
|
|
125
|
+
BlockMatcher.for_call(:Rails, :application, :configure),
|
|
126
|
+
optimize_indentation(code, 2),
|
|
127
|
+
at: :end
|
|
128
|
+
)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Overrides Thor's inject_into_class, with the same arguments and options. See inject_into_block.
|
|
132
|
+
def inject_into_class(path, klass, *args, &block)
|
|
133
|
+
inject_into_block(path, BlockMatcher.for_class(name: klass), *args, &block)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Overrides Thor's inject_into_module, with the same arguments and options. See inject_into_block.
|
|
137
|
+
def inject_into_module(path, module_name, *args, &block)
|
|
138
|
+
inject_into_block(path, BlockMatcher.for_module(module_name), *args, &block)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Adds data inside the first block in a file that `matcher`, a BlockMatcher, accepts. A block is any
|
|
142
|
+
# code that closes with `end`: a class, module, method, or a do ... end block.
|
|
143
|
+
#
|
|
144
|
+
# Works like Thor's inject_into_class: data as a string or a block, the :verbose and :force options,
|
|
145
|
+
# and no change to the data's indentation. It adds one option: at: :start (the default) adds the data
|
|
146
|
+
# right after the block's opening line, and at: :end adds it just before the block's `end`.
|
|
147
|
+
#
|
|
148
|
+
# Prism finds the block, so spacing, comments and code around it do not change where the data goes,
|
|
149
|
+
# and it raises when the block is missing, where Thor prints "unchanged" and goes on. Thor's
|
|
150
|
+
# insert_into_file still writes the data, so it skips data the file already has, honors --pretend,
|
|
151
|
+
# and `rails destroy` can remove it.
|
|
152
|
+
def inject_into_block(path, matcher, *args, &block)
|
|
153
|
+
config = args.extract_options!
|
|
154
|
+
at = config.delete(:at) || :start
|
|
155
|
+
unless %i[start end].include?(at)
|
|
156
|
+
raise ArgumentError, "at: must be :start or :end, not #{at.inspect}"
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
content = File.read(File.join(destination_root, path))
|
|
160
|
+
opening_line, closing = block_lines(matcher.find_in(content))
|
|
161
|
+
unless closing
|
|
162
|
+
raise Thor::Error, "Could not find `#{matcher}` with `end` on its own line in #{path}"
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
lines_before = content.lines.first(at == :start ? opening_line : closing.start_line - 1).join
|
|
166
|
+
config[:after] = /\A#{Regexp.escape(lines_before)}/
|
|
167
|
+
insert_into_file(path, *(args << config), &block)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# For a block, the last line of its opening (such as `class User < ApplicationRecord`, or
|
|
171
|
+
# `configure do |config|` when the opening wraps), and the location of the `end` that closes it.
|
|
172
|
+
# Returns nil unless the `end` keyword starts its own line. A `{ }` block has no `end`.
|
|
173
|
+
def block_lines(node)
|
|
174
|
+
opening, closing = case node
|
|
175
|
+
in Prism::ClassNode
|
|
176
|
+
[node.superclass || node.constant_path, node.end_keyword_loc]
|
|
177
|
+
in Prism::ModuleNode
|
|
178
|
+
[node.constant_path, node.end_keyword_loc]
|
|
179
|
+
in Prism::DefNode
|
|
180
|
+
[node.rparen_loc || node.name_loc, node.end_keyword_loc]
|
|
181
|
+
in Prism::CallNode[block: Prism::BlockNode => block]
|
|
182
|
+
[block.parameters || block.opening_loc, block.closing_loc]
|
|
183
|
+
else return
|
|
184
|
+
end
|
|
185
|
+
return unless closing.slice == 'end' && closing.start_line_slice.strip.empty?
|
|
186
|
+
|
|
187
|
+
opening = opening.location if opening.respond_to?(:location)
|
|
188
|
+
[opening.end_line, closing]
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
require 'rails/generators'
|
|
4
4
|
require 'rails/generators/bundle_helper'
|
|
5
|
-
require_relative '
|
|
5
|
+
require_relative 'file_content_helper'
|
|
6
6
|
require_relative 'skills_helper'
|
|
7
7
|
require 'rolemodel/yarn'
|
|
8
8
|
|
|
9
9
|
module Rolemodel
|
|
10
10
|
class GeneratorBase < ::Rails::Generators::Base
|
|
11
|
-
include ::Rails::Generators::BundleHelper,
|
|
11
|
+
include ::Rails::Generators::BundleHelper, FileContentHelper, SkillsHelper
|
|
12
12
|
|
|
13
13
|
private
|
|
14
14
|
|
|
@@ -38,6 +38,45 @@ module Rolemodel
|
|
|
38
38
|
invoke 'rolemodel:yarn:setup', [], options.slice(:pretend)
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
# Adds a setting that reads ENV through EnvValue as the last line of `class Application`
|
|
42
|
+
# in config/application.rb, for example
|
|
43
|
+
# add_setting("config.x.sentry.dsn = EnvValue.string('SENTRY_DSN')").
|
|
44
|
+
# Runs rolemodel:dotenv first when the app has no EnvValue yet.
|
|
45
|
+
def add_setting(line)
|
|
46
|
+
ensure_env_value
|
|
47
|
+
inject_into_class 'config/application.rb', 'Application', optimize_indentation(line, 4), at: :end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Sets every URL built outside a request, such as links and assets in emails, from DEFAULT_URL.
|
|
51
|
+
# Heroku apps with no DEFAULT_URL, such as review apps, use their herokuapp.com address,
|
|
52
|
+
# and local apps use http://localhost:3000. Safe to call from several generators.
|
|
53
|
+
def add_default_url
|
|
54
|
+
# Rails splits a full URL given as host: into its protocol, host and port.
|
|
55
|
+
add_setting <<~'RUBY'
|
|
56
|
+
# Links in emails, and URLs built outside a request, use DEFAULT_URL.
|
|
57
|
+
config.x.default_url = EnvValue.url('DEFAULT_URL') do
|
|
58
|
+
heroku_app = EnvValue.string('HEROKU_APP_NAME')
|
|
59
|
+
heroku_app ? "https://#{heroku_app}.herokuapp.com" : 'http://localhost:3000'
|
|
60
|
+
end
|
|
61
|
+
config.action_mailer.default_url_options = { host: config.x.default_url }
|
|
62
|
+
config.action_mailer.asset_host = config.x.default_url
|
|
63
|
+
Rails.application.routes.default_url_options = config.action_mailer.default_url_options
|
|
64
|
+
RUBY
|
|
65
|
+
|
|
66
|
+
# New Rails apps set their own default_url_options in development.rb and production.rb, and
|
|
67
|
+
# those files run after application.rb, so they would win over DEFAULT_URL. Comment them out.
|
|
68
|
+
# test.rb keeps its own, so tests still build URLs for example.com.
|
|
69
|
+
%w[development production].each do |env|
|
|
70
|
+
comment_lines "config/environments/#{env}.rb", /config\.action_mailer\.default_url_options\s*=/
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def ensure_env_value
|
|
75
|
+
return if File.exist?(File.join(destination_root, 'lib/env_value.rb'))
|
|
76
|
+
|
|
77
|
+
invoke 'rolemodel:dotenv', [], options.slice(:pretend)
|
|
78
|
+
end
|
|
79
|
+
|
|
41
80
|
def application_stylesheet_path
|
|
42
81
|
Dir.glob('app/assets/stylesheets/application.*').first
|
|
43
82
|
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rails/command'
|
|
4
|
+
require 'rails/commands/application/application_command'
|
|
5
|
+
|
|
6
|
+
module Rolemodel
|
|
7
|
+
module NewApp
|
|
8
|
+
DEFAULTS = %W[
|
|
9
|
+
--database=postgresql
|
|
10
|
+
--javascript=webpack
|
|
11
|
+
--skip-kamal
|
|
12
|
+
--skip-solid
|
|
13
|
+
--skip-test
|
|
14
|
+
--template=#{File.expand_path('new_app/template.rb', __dir__)}
|
|
15
|
+
].freeze
|
|
16
|
+
|
|
17
|
+
def self.argv(argv)
|
|
18
|
+
scrubbed = Rails::Generators::ARGVScrubber.new(argv).prepare!
|
|
19
|
+
scrubbed.take(1) + DEFAULTS + scrubbed.drop(1)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
data/lib/rolemodel/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rolemodel-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- RoleModel Software Inc
|
|
@@ -181,23 +181,29 @@ description: A collection of executable best practices for common aspects of Rai
|
|
|
181
181
|
application development.
|
|
182
182
|
email:
|
|
183
183
|
- it-support@rolemodelsoftware.com
|
|
184
|
-
executables:
|
|
184
|
+
executables:
|
|
185
|
+
- rmr
|
|
185
186
|
extensions: []
|
|
186
187
|
extra_rdoc_files: []
|
|
187
188
|
files:
|
|
188
189
|
- LICENSE.txt
|
|
189
190
|
- README.md
|
|
190
191
|
- Rakefile
|
|
192
|
+
- exe/rmr
|
|
191
193
|
- lib/generators/rolemodel/README.md
|
|
192
194
|
- lib/generators/rolemodel/USAGE
|
|
193
195
|
- lib/generators/rolemodel/all_generator.rb
|
|
194
196
|
- lib/generators/rolemodel/core_setup/README.md
|
|
195
197
|
- lib/generators/rolemodel/core_setup/USAGE
|
|
196
198
|
- lib/generators/rolemodel/core_setup/core_setup_generator.rb
|
|
199
|
+
- lib/generators/rolemodel/dotenv/README.md
|
|
200
|
+
- lib/generators/rolemodel/dotenv/USAGE
|
|
201
|
+
- lib/generators/rolemodel/dotenv/dotenv_generator.rb
|
|
202
|
+
- lib/generators/rolemodel/dotenv/templates/lib/env_value.rb
|
|
203
|
+
- lib/generators/rolemodel/dotenv/templates/spec/lib/env_value_spec.rb
|
|
197
204
|
- lib/generators/rolemodel/editors/README.md
|
|
198
205
|
- lib/generators/rolemodel/editors/USAGE
|
|
199
206
|
- lib/generators/rolemodel/editors/editors_generator.rb
|
|
200
|
-
- lib/generators/rolemodel/editors/vscode_helpers.rb
|
|
201
207
|
- lib/generators/rolemodel/github/README.md
|
|
202
208
|
- lib/generators/rolemodel/github/USAGE
|
|
203
209
|
- lib/generators/rolemodel/github/github_generator.rb
|
|
@@ -449,7 +455,10 @@ files:
|
|
|
449
455
|
- lib/generators/rolemodel/webpack/webpack_generator.rb
|
|
450
456
|
- lib/rolemodel-rails.rb
|
|
451
457
|
- lib/rolemodel/engine.rb
|
|
458
|
+
- lib/rolemodel/file_content_helper.rb
|
|
452
459
|
- lib/rolemodel/generator_base.rb
|
|
460
|
+
- lib/rolemodel/new_app.rb
|
|
461
|
+
- lib/rolemodel/new_app/template.rb
|
|
453
462
|
- lib/rolemodel/optics.rb
|
|
454
463
|
- lib/rolemodel/optics/custom_icon_builder.rb
|
|
455
464
|
- lib/rolemodel/optics/feather_icon_builder.rb
|
|
@@ -458,7 +467,6 @@ files:
|
|
|
458
467
|
- lib/rolemodel/optics/material_icon_builder.rb
|
|
459
468
|
- lib/rolemodel/optics/phosphor_icon_builder.rb
|
|
460
469
|
- lib/rolemodel/optics/tabler_icon_builder.rb
|
|
461
|
-
- lib/rolemodel/replace_content_helper.rb
|
|
462
470
|
- lib/rolemodel/resource_for/controller_extension.rb
|
|
463
471
|
- lib/rolemodel/skills_helper.rb
|
|
464
472
|
- lib/rolemodel/utility.rb
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Rolemodel
|
|
4
|
-
module VSCodeHelpers
|
|
5
|
-
private
|
|
6
|
-
|
|
7
|
-
# Create an extensions file for VSCode and provide an empty
|
|
8
|
-
# recommendations array if it doesn't exist.
|
|
9
|
-
def ensure_extensions_config
|
|
10
|
-
return if File.exist?('.vscode/extensions.json')
|
|
11
|
-
|
|
12
|
-
create_file('.vscode/extensions.json') do |file|
|
|
13
|
-
<<~JSON
|
|
14
|
-
{
|
|
15
|
-
"recommendations": [
|
|
16
|
-
]
|
|
17
|
-
}
|
|
18
|
-
JSON
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
module Rolemodel
|
|
2
|
-
module ReplaceContentHelper
|
|
3
|
-
private
|
|
4
|
-
|
|
5
|
-
def replace_content(relative_path, &block)
|
|
6
|
-
source = File.expand_path(relative_path.to_s, destination_root)
|
|
7
|
-
content = File.exist?(source) ? File.open(source) { it.binmode.read } : ''
|
|
8
|
-
|
|
9
|
-
remove_file relative_path
|
|
10
|
-
create_file relative_path, yield(content)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def modify_json_file(relative_path, &block)
|
|
14
|
-
replace_content(relative_path) do |content|
|
|
15
|
-
hash = JSON.parse(content.presence || '{}')
|
|
16
|
-
yield(hash)
|
|
17
|
-
JSON.pretty_generate(hash) + "\n"
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def add_package_json_script(command_name, command)
|
|
22
|
-
modify_json_file('package.json') do |hash|
|
|
23
|
-
hash['scripts'] ||= {}
|
|
24
|
-
hash['scripts'][command_name] = command
|
|
25
|
-
hash
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|