11#!/usr/bin/env ruby
2+ # typed: strict
3+
24# This script fetches the latest patch versions for each supported Rails version
35# and updates them in the codebase.
46
@@ -17,37 +19,37 @@ def fetch_latest_rails_versions
1719 uri = URI ( "https://rubygems.org/api/v1/versions/rails.json" )
1820 response = Net ::HTTP . get ( uri )
1921 versions = JSON . parse ( response )
20-
22+
2123 # Filter out prerelease versions and map by version number
2224 stable_versions = versions . reject { |v | v [ "prerelease" ] } . map { |v | v [ "number" ] }
23-
25+
2426 # Find latest patch version for each supported major.minor
2527 latest_patches = { }
2628 SUPPORTED_MAJOR_MINOR . each do |major_minor |
2729 matching = stable_versions . select { |v | v . start_with? ( "#{ major_minor } ." ) }
2830 next if matching . empty?
29-
31+
3032 # Sort by version parts to find latest
31- latest = matching . sort_by { |v | v . split ( "." ) . map ( &:to_i ) } . last
33+ latest = matching . max_by { |v | v . split ( "." ) . map ( &:to_i ) }
3234 latest_patches [ major_minor ] = latest
3335 end
34-
36+
3537 puts "Latest patch versions:"
3638 latest_patches . each do |mm , version |
3739 puts " - Rails #{ mm } : #{ version } "
3840 end
39-
41+
4042 latest_patches
4143end
4244
4345def update_create_app_script ( versions )
4446 puts "\n Updating rails_test_app/create_app.rb..."
45-
47+
4648 content = File . read ( CREATE_APP_SCRIPT )
47-
49+
4850 # Look for the case statement with Rails version mapping
4951 case_pattern = /case\s +rails_version\s *\n (.*?)end/m
50-
52+
5153 if content . match ( case_pattern )
5254 # Replace the case statement with updated versions
5355 new_case = "case rails_version\n "
@@ -58,7 +60,7 @@ def update_create_app_script(versions)
5860 new_case += " else\n "
5961 new_case += " puts \" Warning: Using unrecognized Rails version \# {rails_version}\" \n "
6062 new_case += " end"
61-
63+
6264 updated_content = content . gsub ( case_pattern , new_case )
6365 File . write ( CREATE_APP_SCRIPT , updated_content )
6466 puts " Updated create_app.rb with latest versions"
@@ -70,30 +72,30 @@ def update_create_app_script(versions)
7072
7173def update_github_workflow ( versions )
7274 puts "\n Updating .github/workflows/test.yml..."
73-
75+
7476 content = File . read ( GITHUB_WORKFLOW )
7577 updated_content = content . dup
76-
78+
7779 # Update the pilot test Rails version
78- if updated_content . match ( /RAILS_VERSION:\s *'8\. 0[\. \d ]*'/ )
79- updated_content . gsub! ( /RAILS_VERSION:\s *'8\. 0[\. \d ]*'/ , "RAILS_VERSION: '#{ versions [ ' 8.0' ] } '" )
80- puts " Updated pilot test Rails version to #{ versions [ ' 8.0' ] } "
80+ if /RAILS_VERSION:\s *'8\. 0[\. \d ]*'/ . match? ( updated_content )
81+ updated_content . gsub! ( /RAILS_VERSION:\s *'8\. 0[\. \d ]*'/ , "RAILS_VERSION: '#{ versions [ " 8.0" ] } '" )
82+ puts " Updated pilot test Rails version to #{ versions [ " 8.0" ] } "
8183 else
8284 puts " Warning: Could not find RAILS_VERSION for pilot test in test.yml"
8385 end
84-
86+
8587 # Update the matrix versions
8688 versions . each do |major_minor , version |
8789 # Look for matrix entries with this major.minor version
8890 # Allow for different patch versions (e.g., .8, .8.7)
89- if updated_content . match ( /rails:\s *'#{ major_minor } [\. \d ]+'/ )
91+ if /rails:\s *'#{ major_minor } [\. \d ]+'/ . match? ( updated_content )
9092 updated_content . gsub! ( /rails:\s *'#{ major_minor } [\. \d ]+'/ , "rails: '#{ version } '" )
9193 puts " Updated matrix Rails #{ major_minor } to version #{ version } "
9294 else
9395 puts " Warning: Could not find matrix entry for Rails #{ major_minor } in test.yml"
9496 end
9597 end
96-
98+
9799 File . write ( GITHUB_WORKFLOW , updated_content )
98100 puts " Updated test.yml with latest versions"
99101end
@@ -104,4 +106,4 @@ def update_github_workflow(versions)
104106update_github_workflow ( versions )
105107
106108puts "\n Done! Rails versions have been updated to the latest patches."
107- puts "Remember to commit these changes if they look good."
109+ puts "Remember to commit these changes if they look good."
0 commit comments