2929end
3030
3131def install_rails_if_missing ( version )
32- return if version == "main"
32+ if version == "main"
33+ begin
34+ versions = Gem ::Specification . find_all_by_name ( "rails" ) . map ( &:version )
35+ rescue => e
36+ warn "Warning: gem spec check failed (#{ e } ). Falling back to shell check."
37+ versions = [ ]
38+ if system ( "gem list -i rails" )
39+ versions = [ Gem ::Version . new ( "0.0.0" ) ]
40+ end
41+ end
42+
43+ latest = versions . max
44+ return if latest && latest >= Gem ::Version . new ( "8.0.0" )
45+
46+ puts "Rails not found or too old. Installing latest release..."
47+ system ( "gem install rails --no-document" ) || abort ( "Failed to install Rails" )
48+ return
49+ end
50+
3351 # Robust detection using RubyGems API
3452 begin
3553 if Gem ::Specification . find_all_by_name ( "rails" , version ) . any?
@@ -53,6 +71,15 @@ def install_rails_if_missing(version)
5371
5472install_rails_if_missing ( rails_version )
5573
74+ def rails_supports_skip_kamal? ( clean_env )
75+ stdout , stderr , status = Open3 . capture3 ( clean_env , "rails" , "new" , "--help" )
76+ return false unless status . success?
77+
78+ stdout . include? ( "--skip-kamal" ) || stderr . include? ( "--skip-kamal" )
79+ rescue Errno ::ENOENT
80+ false
81+ end
82+
5683# Fix for Rails 7.0 compatibility with concurrent-ruby
5784# (Have to run this before bundler/setup)
5885# See: https://github.com/rails/rails/pull/54264
@@ -82,6 +109,7 @@ def install_rails_if_missing(version)
82109$VERBOSE = nil
83110require "fileutils"
84111require "erb"
112+ require "open3"
85113
86114# Make the rails_version available as an instance variable for the ERB templates
87115@rails_version = rails_version
@@ -162,7 +190,8 @@ def copy_template(file, target_path = nil)
162190 "--skip-sprockets" , "--skip-javascript" , "--skip-hotwire" ,
163191 "--skip-jbuilder" , "--skip-asset-pipeline" , "--skip-bootsnap" ,
164192 "--api" , "-T" ]
165- rails_cmd << "--skip-kamal" if @rails_major_minor . start_with? ( "8." )
193+ skip_kamal = @rails_major_minor . start_with? ( "8." ) && rails_supports_skip_kamal? ( clean_env )
194+ rails_cmd << "--skip-kamal" if skip_kamal
166195 require "tmpdir"
167196 Dir . mktmpdir ( "logstruct_rails_new_" ) do |tmpdir |
168197 Dir . chdir ( tmpdir ) do
0 commit comments