Improve how the sessions generator adds bcrypt (#52346)

* doc: fix CHANGELOG message for sessions generator

* fix: Sessions generator adds bcrypt more robustly

Use Thor's `uncomment_lines` instead of `gsub_file`.

If the Gemfile does not contain bcrypt, either commented or
uncommented, then run `bundle add bcrypt`.

Make sure all bundler commands run within `Bundler.with_original_env`
to avoid bundler resolution problems while trying to resolve bundler
dependencies. Typically the failure mode here is seeing:

> Could not find bcrypt-3.1.20 in cached gems or installed locally (Bundler::GemNotFound)

while bootstrapping the process that will run bundle-install or bundle-add.
This commit is contained in:
Mike Dalessio 2024-07-17 02:14:31 -04:00 committed by GitHub
parent d35f52c5d1
commit f9e86f1bf5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 4 deletions

@ -2,7 +2,7 @@
# Generate with...
bin/rails sessions
bin/rails generate sessions
# Generated files
app/models/current.rb

@ -20,9 +20,12 @@ def configure_application
end
def enable_bcrypt
# FIXME: Make more resilient in case the default comment has been removed
gsub_file "Gemfile", /# gem "bcrypt"/, 'gem "bcrypt"'
execute_command :bundle, ""
if File.read("Gemfile").include?('gem "bcrypt"')
uncomment_lines "Gemfile", /gem "bcrypt"/
Bundler.with_original_env { execute_command :bundle, "" }
else
Bundler.with_original_env { execute_command :bundle, "add bcrypt" }
end
end
def add_migrations