Skip to content

Commit

Permalink
Allow suspenders to be used with a range of Ruby versions (#1129)
Browse files Browse the repository at this point in the history
Suspenders locks the user in a particular Ruby version (3.0.5 at the
time of writing), a restriction that can sometimes be annoying. This
PR enables Suspenders to be used with a range of safe Ruby versions,
initially from 3.0.5 to 3.2.2

Contrary to the Rails version, the Ruby version can be more flexible
if our gems are compatible. Major Rails versions, on the other hand,
tend to be a bit more disruptive and may require deeper changes to
Suspenders. [The last main breaking
change](https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/)
was on Ruby 3.0, but we're already past that point.

`.ruby-versions` was the source of truth for the canonical Ruby
version, but in this PR it represents the maximum version.
Unfortunately, it is still needed by CI so we can't delete it. Note
that even without such a file, rubybems validates the runtime Ruby
version through the gemspec.

Closes #1073
  • Loading branch information
thiagoa authored Aug 21, 2023
1 parent cc924a1 commit 761c413
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.5
3.2.2
13 changes: 13 additions & 0 deletions bin/suspenders
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ require 'pathname'
source_path = (Pathname.new(__FILE__).dirname + '../lib').expand_path
$LOAD_PATH << source_path

require "suspenders/version"

ruby_version_range = Suspenders::RUBY_VERSION_RANGE.map do |version|
Gem::Version.new(version)
end

current_ruby_version = Gem::Version.new(RUBY_VERSION)

unless current_ruby_version.between?(*ruby_version_range)
abort "Your version of Ruby #{current_ruby_version} is not supported. " \
"Versions from #{ruby_version_range.map(&:to_s).join(" to ")} are supported."
end

activate_rails_version = ->(rails_version) do
rails_bin_path = Gem.activate_bin_path("railties", "rails", rails_version)
rails_path = File.expand_path("../..", rails_bin_path)
Expand Down
2 changes: 1 addition & 1 deletion lib/suspenders/app_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def replace_gemfile
end

def ruby_version
create_file ".ruby-version", "#{Suspenders::RUBY_VERSION}\n"
create_file ".ruby-version", "#{RUBY_VERSION}\n"
end

def configure_i18n_for_missing_translations
Expand Down
9 changes: 5 additions & 4 deletions lib/suspenders/version.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module Suspenders
RAILS_VERSION = "~> 7.0.0".freeze
RUBY_VERSION = IO
.read("#{File.dirname(__FILE__)}/../../.ruby-version")
.strip
.freeze

minimum_ruby_version = "3.0.5"
maximum_ruby_version = Pathname(__dir__).join("../../.ruby-version").read.strip

RUBY_VERSION_RANGE = [minimum_ruby_version, maximum_ruby_version].freeze
VERSION = "20230113.0".freeze
end
6 changes: 3 additions & 3 deletions spec/features/new_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
it "uses custom Gemfile" do
gemfile_file = IO.read("#{project_path}/Gemfile")
expect(gemfile_file).to match(
/^ruby "#{Suspenders::RUBY_VERSION}"$/o
/^ruby "#{RUBY_VERSION}"$/o
)
expect(gemfile_file).to match(
/^gem "rails", "#{Suspenders::RAILS_VERSION}"$/o
Expand All @@ -34,8 +34,8 @@
end
end

it "creates .ruby-version from Suspenders .ruby-version" do
ruby_version_file = IO.read("#{project_path}/.ruby-version")
it "creates .ruby-version with the running ruby version" do
ruby_version_file = project_path.join(".ruby-version").read

expect(ruby_version_file).to eq "#{RUBY_VERSION}\n"
end
Expand Down
4 changes: 3 additions & 1 deletion suspenders.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ require "suspenders/version"
require "date"

Gem::Specification.new do |s|
s.required_ruby_version = ">= #{Suspenders::RUBY_VERSION}"
minimum_ruby_version, maximum_ruby_version = Suspenders::RUBY_VERSION_RANGE

s.required_ruby_version = [">= #{minimum_ruby_version}", "<= #{maximum_ruby_version}"]
s.required_rubygems_version = ">= 3.0.0"
s.authors = ["thoughtbot"]

Expand Down
2 changes: 1 addition & 1 deletion templates/Gemfile.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end

ruby "<%= Suspenders::RUBY_VERSION %>"
ruby <%= RUBY_VERSION.inspect %>
<% if options[:api] %>
gem "sprockets", "< 4"
Expand Down

0 comments on commit 761c413

Please sign in to comment.