Skip to content

Commit a1c6e27

Browse files
skipkayhilsinsoku
andcommitted
Use ruby file: ".ruby-version" for new apps
Previously, new apps would have a Ruby version set in both the Gemfile and the .ruby-version file. This duplication makes it more difficult to quickly change an application's ruby version as users must remember to update multiple files. This commit updates the app generator's Gemfile to read the Ruby version from the .ruby-version file. Since this feature was introduced in the latest version of Bundler, it will only be enabled if a supported version of Bundler is used. Alternatively, another solution mentioned on the original PR adding .ruby-version was that the .ruby-version file could be removed once rvm/rbenv support reading the Ruby version from the Gemfile. This has a downside that many other tools like chruby do not have plans to support reading a Ruby version from the Gemfile, and so users of those tools would have a worse experience if the .ruby-version file is removed. Co-authored-by: Takumi Shotoku <[email protected]>
1 parent 3163bb7 commit a1c6e27

File tree

6 files changed

+27
-5
lines changed

6 files changed

+27
-5
lines changed

railties/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Use `ruby file: ".ruby-version"` in generated Gemfile if supported.
2+
3+
*Hartley McGuire*
4+
15
* `bin/rails test` will no longer load files named `*_test.rb` if they are located in the `fixtures` folder.
26

37
*Edouard Chin*

railties/lib/rails/generators/app_base.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,20 @@ def to_s
436436
end
437437
end
438438

439+
def gem_ruby_entry
440+
if Gem::Version.new(Bundler::VERSION) >= Gem::Version.new("2.4.20") # add file: option to #ruby
441+
'ruby file: ".ruby-version"'
442+
else
443+
"ruby \"#{gem_ruby_version}\""
444+
end
445+
end
446+
439447
def gem_ruby_version
440-
Gem::Version.new(Gem::VERSION) >= Gem::Version.new("3.3.13") ? Gem.ruby_version : RUBY_VERSION
448+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new("3.3.13") # patch level removed from Gem.ruby_version
449+
Gem.ruby_version
450+
else
451+
RUBY_VERSION
452+
end
441453
end
442454

443455
def rails_prerelease?

railties/lib/rails/generators/rails/app/templates/Dockerfile.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ RUN curl -fsSL https://bun.sh/install | bash -s -- "bun-v${BUN_VERSION}"
4242

4343
<% end -%>
4444
# Install application gems
45-
COPY Gemfile Gemfile.lock ./
45+
COPY .ruby-version Gemfile Gemfile.lock ./
4646
RUN bundle install && \
4747
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git<% if depend_on_bootsnap? -%> && \
4848
bundle exec bootsnap precompile --gemfile<% end %>

railties/lib/rails/generators/rails/app/templates/Gemfile.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
source "https://rubygems.org"
22

3-
ruby <%= "\"#{gem_ruby_version}\"" -%>
3+
<%= gem_ruby_entry %>
44

55
<% gemfile_entries.each do |gemfile_entry| %>
66
<%= gemfile_entry %>

railties/test/generators/app_generator_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,13 @@ def test_inclusion_of_ruby_version
10061006
run_generator
10071007

10081008
assert_file "Gemfile" do |content|
1009-
assert_match(/ruby "#{Gem::Version.new(Gem::VERSION) >= Gem::Version.new("3.3.13") ? Gem.ruby_version : RUBY_VERSION}"/, content)
1009+
if Gem::Version.new(Bundler::VERSION) >= Gem::Version.new("2.4.20") # add file: option to #ruby
1010+
assert_match('ruby file: ".ruby-version"', content)
1011+
elsif Gem::Version.new(Gem::VERSION) >= Gem::Version.new("3.3.13") # patch level removed from Gem.ruby_version
1012+
assert_match("ruby \"#{Gem.ruby_version}\"", content)
1013+
else
1014+
assert_match("ruby \"#{RUBY_VERSION}\"", content)
1015+
end
10101016
end
10111017
assert_file "Dockerfile" do |content|
10121018
assert_match(/ARG RUBY_VERSION=#{Gem::Version.new(Gem::VERSION) >= Gem::Version.new("3.3.13") ? Gem.ruby_version : RUBY_VERSION}/, content)

railties/test/generators/generators_test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def evaluate_template_docker(file)
100100
private
101101
def gemfile_locals
102102
{
103-
gem_ruby_version: RUBY_VERSION,
103+
gem_ruby_entry: "ruby \"#{RUBY_VERSION}\"",
104104
rails_prerelease: false,
105105
skip_active_storage: true,
106106
depend_on_bootsnap: false,

0 commit comments

Comments
 (0)