Skip to content

Commit dd5b3e9

Browse files
authored
fix: use a safer gsub_file and update/remove file gsubs that were no longer doing anything (#533)
So it turns out that `gsub_file` does not actually check if it matched anything and so we have a few misc. changes silently not being applied due to changes in Rails 7.1. This addresses that by switching us to use `gsub_file!` which reads the file into memory before it's gsub'd and then compares the results to make sure it actually changed. I've opened rails/thor#874 to add this to Thor itself
1 parent a46e96d commit dd5b3e9

File tree

17 files changed

+129
-141
lines changed

17 files changed

+129
-141
lines changed

template.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,12 @@ def create_initial_migration
381381
run_with_clean_bundler_env "bin/rake db:migrate"
382382
end
383383

384+
def gsub_file!(path, flag, *args, &block)
385+
content = File.binread(path)
386+
387+
gsub_file(path, flag, *args, &block)
388+
389+
raise StandardError, "the contents of #{path} did not change!" if content == File.binread(path)
390+
end
391+
384392
apply_template!

variants/backend-base/app/template.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
empty_directory_with_keep_file "app/services"
1818

1919
# Configure the default mailer to use the our default from address
20-
gsub_file "app/mailers/application_mailer.rb",
21-
"default from: '[email protected]'",
22-
"default from: Rails.application.config.app.mail_from"
20+
gsub_file! "app/mailers/application_mailer.rb",
21+
/default from: ['"]from@example\.com['"]/,
22+
"default from: Rails.application.config.app.mail_from"

variants/backend-base/config/application.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
gsub_file "config/application.rb",
2-
"# config.time_zone = 'Central Time (US & Canada)'",
3-
"config.time_zone = 'Wellington'"
1+
gsub_file! "config/application.rb",
2+
/# config.time_zone = ['"]Central Time \(US & Canada\)['"]/,
3+
"config.time_zone = 'Wellington'"
44

55
insert_into_file "config/application.rb", after: /^require_relative ['"]boot['"]/ do
66
# the empty line at the beginning of this string is required

variants/backend-base/config/environments/development.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
RUBY
1616
end
1717

18-
gsub_file "config/environments/development.rb",
19-
"join('tmp', 'caching-dev.txt')",
20-
'join("tmp/caching-dev.txt")'
21-
22-
gsub_file "config/environments/development.rb",
23-
"config.action_controller.raise_on_missing_callback_actions = true",
24-
"# config.action_controller.raise_on_missing_callback_actions = true"
18+
gsub_file! "config/environments/development.rb",
19+
"config.action_controller.raise_on_missing_callback_actions = true",
20+
"# config.action_controller.raise_on_missing_callback_actions = true"

variants/backend-base/config/environments/production.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
RUBY
99
end
1010

11-
gsub_file "config/environments/production.rb",
12-
"# config.force_ssl = true",
13-
<<~RUBY
14-
##
15-
# `force_ssl` defaults to on. Set `force_ssl` to false if (and only if) RAILS_FORCE_SSL=false, otherwise set it to true.
16-
#
17-
config.force_ssl = ENV.fetch("RAILS_FORCE_SSL", "true").downcase != "false"
18-
RUBY
11+
gsub_file! "config/environments/production.rb",
12+
"config.force_ssl = true",
13+
<<~RUBY
14+
##
15+
# `force_ssl` defaults to on. Set `force_ssl` to false if (and only if) RAILS_FORCE_SSL=false, otherwise set it to true.
16+
#
17+
config.force_ssl = ENV.fetch("RAILS_FORCE_SSL", "true").downcase != "false"
18+
RUBY
1919

2020
insert_into_file "config/environments/production.rb",
2121
after: /# config\.action_mailer\.raise_deliv.*\n/ do
@@ -42,13 +42,13 @@
4242
RUBY
4343
end
4444

45-
gsub_file "config/environments/production.rb",
46-
"config.log_level = :info",
47-
'config.log_level = ENV.fetch("LOG_LEVEL", "info").to_sym'
45+
gsub_file! "config/environments/production.rb",
46+
'ENV.fetch("RAILS_LOG_LEVEL", "info")',
47+
'ENV.fetch("RAILS_LOG_LEVEL", ENV.fetch("LOG_LEVEL", "info"))'
4848

49-
gsub_file "config/environments/production.rb",
50-
"ActiveSupport::Logger.new(STDOUT)",
51-
"ActiveSupport::Logger.new($stdout)"
49+
gsub_file! "config/environments/production.rb",
50+
"ActiveSupport::Logger.new(STDOUT)",
51+
"ActiveSupport::Logger.new($stdout)"
5252

5353
insert_into_file "config/environments/production.rb",
5454
after: /.*config\.public_file_server\.enabled.*\n/ do

variants/backend-base/config/environments/test.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
gsub_file "config/environments/test.rb",
2-
"config.eager_load = false",
3-
"config.eager_load = defined?(SimpleCov).present?"
4-
51
insert_into_file \
62
"config/environments/test.rb",
73
after: /config\.action_mailer\.delivery_method = :test\n/ do
@@ -17,6 +13,6 @@
1713
RUBY
1814
end
1915

20-
gsub_file "config/environments/test.rb",
21-
"config.action_controller.raise_on_missing_callback_actions = true",
22-
"# config.action_controller.raise_on_missing_callback_actions = true"
16+
gsub_file! "config/environments/test.rb",
17+
"config.action_controller.raise_on_missing_callback_actions = true",
18+
"# config.action_controller.raise_on_missing_callback_actions = true"

variants/backend-base/config/template.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
copy_file "variants/backend-base/config/initializers/check_env.rb", "config/initializers/check_env.rb"
1818
copy_file "variants/backend-base/config/initializers/sentry.rb", "config/initializers/sentry.rb"
1919

20-
gsub_file "config/initializers/filter_parameter_logging.rb", /\[:password\]/ do
21-
"%w[password secret session cookie csrf]"
22-
end
20+
gsub_file! "config/initializers/filter_parameter_logging.rb",
21+
/ {2}:passw, :secret, /,
22+
" :passw, :secret, :session, :cookie, :csrf, "
2323

2424
apply "variants/backend-base/config/environments/development.rb"
2525
apply "variants/backend-base/config/environments/production.rb"
@@ -59,7 +59,7 @@
5959
EO_ROUTES
6060

6161
if File.exist? "config/storage.yml"
62-
gsub_file "config/storage.yml", /# service: S3/ do
62+
gsub_file! "config/storage.yml", /# service: S3/ do
6363
<<~YAML
6464
# service: S3
6565
# upload:

variants/deploy_with_ackama_ec2_capistrano/template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
178178
EO_RUBY
179179

180-
gsub_file("config/deploy.rb", old_generated_cap_config_snippet, new_ackama_cap_config_snippet)
180+
gsub_file!("config/deploy.rb", old_generated_cap_config_snippet, new_ackama_cap_config_snippet)
181181

182182
insert_into_file "Capfile", after: /install_plugin Capistrano::SCM::Git/ do
183183
<<~EO_RUBY

variants/deploy_with_capistrano/template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
166166
EO_RUBY
167167

168-
gsub_file("config/deploy.rb", old_generated_cap_config_snippet, new_ackama_cap_config_snippet)
168+
gsub_file!("config/deploy.rb", old_generated_cap_config_snippet, new_ackama_cap_config_snippet)
169169

170170
insert_into_file "Capfile", after: /install_plugin Capistrano::SCM::Git/ do
171171
<<~EO_RUBY

variants/devise/template.rb

Lines changed: 61 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,26 @@
1010
TERMINAL.puts_header "Generating User model with devise"
1111
run "bundle exec rails generate devise User"
1212

13-
gsub_file "app/models/user.rb",
14-
":validatable",
15-
":validatable, :lockable"
13+
gsub_file! "app/models/user.rb",
14+
":validatable",
15+
":validatable, :lockable"
1616

1717
devise_migration_filename = Dir.children("db/migrate").find { |filename| filename.end_with?("_devise_create_users.rb") }
1818
devise_migration_path = "db/migrate/#{devise_migration_filename}"
1919

2020
TERMINAL.puts_header "Tweaking auto-generated devise migration '#{devise_migration_path}'"
21-
gsub_file devise_migration_path,
22-
" # t.integer :failed_attempts",
23-
" t.integer :failed_attempts"
24-
gsub_file devise_migration_path,
25-
" # t.string :unlock_token",
26-
" t.string :unlock_token"
27-
gsub_file devise_migration_path,
28-
" # t.datetime :locked_at",
29-
" t.datetime :locked_at"
30-
gsub_file devise_migration_path,
31-
" # add_index :users, :unlock_token",
32-
" add_index :users, :unlock_token"
33-
gsub_file devise_migration_path,
34-
/ # add_index :users, :unlock_token.+/,
35-
" add_index :users, :unlock_token, unique: true"
21+
gsub_file! devise_migration_path,
22+
" # t.integer :failed_attempts",
23+
" t.integer :failed_attempts"
24+
gsub_file! devise_migration_path,
25+
" # t.string :unlock_token",
26+
" t.string :unlock_token"
27+
gsub_file! devise_migration_path,
28+
" # t.datetime :locked_at",
29+
" t.datetime :locked_at"
30+
gsub_file! devise_migration_path,
31+
" # add_index :users, :unlock_token",
32+
" add_index :users, :unlock_token"
3633

3734
TERMINAL.puts_header "Running db migration"
3835
run "bundle exec rails db:migrate"
@@ -45,51 +42,51 @@
4542
#
4643
TERMINAL.puts_header "Tweaking config/initializers/devise.rb"
4744

48-
gsub_file "config/initializers/devise.rb",
49-
" config.mailer_sender = '[email protected]'",
50-
" config.mailer_sender = Rails.application.config.app.mail_from"
45+
gsub_file! "config/initializers/devise.rb",
46+
" config.mailer_sender = '[email protected]'",
47+
" config.mailer_sender = Rails.application.config.app.mail_from"
5148

52-
gsub_file "config/initializers/devise.rb",
53-
" # config.scoped_views = false",
54-
" config.scoped_views = true"
49+
gsub_file! "config/initializers/devise.rb",
50+
" # config.scoped_views = false",
51+
" config.scoped_views = true"
5552

56-
gsub_file "config/initializers/devise.rb",
57-
" config.password_length = 6..128",
58-
" config.password_length = 16..128"
53+
gsub_file! "config/initializers/devise.rb",
54+
" config.password_length = 6..128",
55+
" config.password_length = 16..128"
5956

60-
gsub_file "config/initializers/devise.rb",
61-
" # config.paranoid = true",
62-
" config.paranoid = true"
57+
gsub_file! "config/initializers/devise.rb",
58+
" # config.paranoid = true",
59+
" config.paranoid = true"
6360

64-
gsub_file "config/initializers/devise.rb",
65-
/ # config.secret_key = '.+'/,
66-
" # config.secret_key = 'do_not_put_secrets_in_source_control_please'"
61+
gsub_file! "config/initializers/devise.rb",
62+
/ # config.secret_key = '.+'/,
63+
" # config.secret_key = 'do_not_put_secrets_in_source_control_please'"
6764

68-
gsub_file "config/initializers/devise.rb",
69-
/ # config.lock_strategy = .+/,
70-
" config.lock_strategy = :failed_attempts"
65+
gsub_file! "config/initializers/devise.rb",
66+
/ # config.lock_strategy = .+/,
67+
" config.lock_strategy = :failed_attempts"
7168

72-
gsub_file "config/initializers/devise.rb",
73-
/ # config.unlock_strategy = .+/,
74-
" config.unlock_strategy = :email"
69+
gsub_file! "config/initializers/devise.rb",
70+
/ # config.unlock_strategy = .+/,
71+
" config.unlock_strategy = :email"
7572

76-
gsub_file "config/initializers/devise.rb",
77-
" # config.parent_mailer = 'ActionMailer::Base'",
78-
" config.parent_mailer = 'ApplicationMailer'"
73+
gsub_file! "config/initializers/devise.rb",
74+
" # config.parent_mailer = 'ActionMailer::Base'",
75+
" config.parent_mailer = 'ApplicationMailer'"
7976

80-
gsub_file "config/initializers/devise.rb",
81-
/ # config.maximum_attempts = .+/,
82-
<<-EO_CHUNK
77+
gsub_file! "config/initializers/devise.rb",
78+
/ # config.maximum_attempts = .+/,
79+
<<-EO_CHUNK
8380
#
8481
# https://www.nzism.gcsb.govt.nz/ism-document/#1887 recommends 3 as a default. FYI to
8582
# be fully compliant with https://www.nzism.gcsb.govt.nz/ism-document/#1887 then only
8683
# Administrators should be able to unlock.
8784
config.maximum_attempts = 3
88-
EO_CHUNK
85+
EO_CHUNK
8986

90-
gsub_file "config/initializers/devise.rb",
91-
/ # config.last_attempt_warning = .+/,
92-
" config.last_attempt_warning = true"
87+
gsub_file! "config/initializers/devise.rb",
88+
/ # config.last_attempt_warning = .+/,
89+
" config.last_attempt_warning = true"
9390

9491
##
9592
# Add a block to config/routes.rb demonstrating how to create authenticated
@@ -140,13 +137,13 @@
140137

141138
copy_file "app/controllers/users/sessions_controller.rb"
142139

143-
gsub_file "config/routes.rb",
144-
"devise_for :users",
145-
<<~EO_DEVISE
146-
devise_for :users, controllers: {
147-
sessions: "users/sessions"
148-
}
149-
EO_DEVISE
140+
gsub_file! "config/routes.rb",
141+
"devise_for :users",
142+
<<~EO_DEVISE
143+
devise_for :users, controllers: {
144+
sessions: "users/sessions"
145+
}
146+
EO_DEVISE
150147

151148
insert_into_file "app/models/user.rb", before: /^end/ do
152149
<<~'RUBY'
@@ -202,14 +199,14 @@ def authenticatable_salt
202199
copy_file "spec/requests/session_cookie_expiry_spec.rb"
203200

204201
# tell pundit not to check that authorization was called on devise controllers
205-
gsub_file("app/controllers/application_controller.rb",
206-
"after_action :verify_authorized, except: :index",
207-
"after_action :verify_authorized, except: :index, unless: :devise_controller?"
208-
)
209-
gsub_file("app/controllers/application_controller.rb",
210-
"after_action :verify_policy_scoped, only: :index",
211-
"after_action :verify_policy_scoped, only: :index, unless: :devise_controller?"
212-
)
202+
gsub_file!("app/controllers/application_controller.rb",
203+
"after_action :verify_authorized, except: :index",
204+
"after_action :verify_authorized, except: :index, unless: :devise_controller?"
205+
)
206+
gsub_file!("app/controllers/application_controller.rb",
207+
"after_action :verify_policy_scoped, only: :index",
208+
"after_action :verify_policy_scoped, only: :index, unless: :devise_controller?"
209+
)
213210

214211
TERMINAL.puts_header "Running rubocop -A to fix formatting in files generated by devise"
215212
run "bundle exec rubocop -A -c ./.rubocop.yml"

variants/frontend-base-typescript/template.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def rename_js_file_to_ts(file)
3535
copy_file ".eslintrc.js", force: true
3636
copy_file "types.d.ts", force: true
3737

38-
gsub_file(
38+
gsub_file!(
3939
"app/frontend/packs/application.ts",
4040
"process.env.SENTRY_ENV || process.env.RAILS_ENV",
4141
"process.env.SENTRY_ENV ?? process.env.RAILS_ENV"
@@ -53,7 +53,7 @@ def rename_js_file_to_ts(file)
5353
return String(images(name));
5454
};
5555
EO_TS_ENABLE_IMAGES
56-
gsub_file("app/frontend/packs/application.ts", js_load_images_chunk, ts_load_images_chunk, force: true)
56+
gsub_file!("app/frontend/packs/application.ts", js_load_images_chunk, ts_load_images_chunk, force: true)
5757

5858
update_package_json do |package_json|
5959
package_json["scripts"]["typecheck"] = "tsc -p . --noEmit"

variants/frontend-base/sentry/template.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,3 @@
4646
//
4747
EO_JS
4848
end
49-
50-
gsub_file "config/initializers/content_security_policy.rb",
51-
/# policy.report_uri ".+"/,
52-
'policy.report_uri(ENV["SENTRY_CSP_HEADER_REPORT_ENDPOINT"]) if ENV["SENTRY_CSP_HEADER_REPORT_ENDPOINT"]'

0 commit comments

Comments
 (0)