@@ -16,49 +16,64 @@ require_relative "task_helpers"
1616namespace :shakapacker_examples do # rubocop:disable Metrics/BlockLength
1717 include ReactOnRails ::TaskHelpers
1818
19- # Updates package.json to use minimum supported versions for compatibility testing
20- # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
19+ # Updates package.json and Gemfile to use minimum supported versions for compatibility testing
20+ # rubocop:disable Metrics/AbcSize,Metrics/ CyclomaticComplexity,Metrics/PerceivedComplexity
2121 def apply_minimum_versions ( dir )
22+ # Update package.json
2223 package_json_path = File . join ( dir , "package.json" )
23- return unless File . exist? ( package_json_path )
24+ if File . exist? ( package_json_path )
25+ begin
26+ package_json = JSON . parse ( File . read ( package_json_path ) )
27+ rescue JSON ::ParserError => e
28+ puts " ERROR: Failed to parse package.json in #{ dir } : #{ e . message } "
29+ raise
30+ end
2431
25- begin
26- package_json = JSON . parse ( File . read ( package_json_path ) )
27- rescue JSON ::ParserError => e
28- puts " ERROR: Failed to parse package.json in #{ dir } : #{ e . message } "
29- raise
30- end
32+ deps = package_json [ "dependencies" ]
33+ dev_deps = package_json [ "devDependencies" ]
3134
32- deps = package_json [ "dependencies" ]
33- dev_deps = package_json [ "devDependencies" ]
35+ # Update React versions to minimum supported
36+ if deps
37+ deps [ "react" ] = ExampleType ::MINIMUM_REACT_VERSION
38+ deps [ "react-dom" ] = ExampleType ::MINIMUM_REACT_VERSION
39+ # Shakapacker 8.2.0 requires webpack-assets-manifest ^5.x
40+ deps [ "webpack-assets-manifest" ] = "^5.0.6" if deps . key? ( "webpack-assets-manifest" )
41+ end
3442
35- # Update React versions to minimum supported
36- if deps
37- deps [ "react" ] = ExampleType ::MINIMUM_REACT_VERSION
38- deps [ "react-dom" ] = ExampleType ::MINIMUM_REACT_VERSION
39- # Shakapacker 8.2.0 requires webpack-assets-manifest ^5.x
40- deps [ "webpack-assets-manifest" ] = "^5.0.6" if deps . key? ( "webpack-assets-manifest" )
41- end
43+ # Shakapacker 8.2.0 requires webpack-assets-manifest ^5.x (check devDependencies too)
44+ dev_deps [ "webpack-assets-manifest" ] = "^5.0.6" if dev_deps &.key? ( "webpack-assets-manifest" )
4245
43- # Shakapacker 8.2.0 requires webpack-assets-manifest ^5.x (check devDependencies too)
44- dev_deps [ "webpack-assets-manifest" ] = "^5.0.6" if dev_deps &.key? ( "webpack-assets-manifest" )
46+ # Update Shakapacker to minimum supported version in package.json
47+ if dev_deps &.key? ( "shakapacker" )
48+ dev_deps [ "shakapacker" ] = ExampleType ::MINIMUM_SHAKAPACKER_VERSION
49+ elsif deps &.key? ( "shakapacker" )
50+ deps [ "shakapacker" ] = ExampleType ::MINIMUM_SHAKAPACKER_VERSION
51+ end
52+
53+ File . write ( package_json_path , "#{ JSON . pretty_generate ( package_json ) } \n " )
54+ end
4555
46- # Update Shakapacker to minimum supported version
47- if dev_deps &.key? ( "shakapacker" )
48- dev_deps [ "shakapacker" ] = ExampleType ::MINIMUM_SHAKAPACKER_VERSION
49- elsif deps &.key? ( "shakapacker" )
50- deps [ "shakapacker" ] = ExampleType ::MINIMUM_SHAKAPACKER_VERSION
56+ # Update Gemfile to pin shakapacker to minimum version
57+ # (must match the npm package version exactly)
58+ gemfile_path = File . join ( dir , "Gemfile" )
59+ if File . exist? ( gemfile_path )
60+ gemfile_content = File . read ( gemfile_path )
61+ # Replace any shakapacker gem line with exact version pin
62+ gemfile_content = gemfile_content . gsub (
63+ /gem ['"]shakapacker['"].*$/ ,
64+ "gem 'shakapacker', '#{ ExampleType ::MINIMUM_SHAKAPACKER_VERSION } '"
65+ )
66+ File . write ( gemfile_path , gemfile_content )
5167 end
5268
53- File . write ( package_json_path , "#{ JSON . pretty_generate ( package_json ) } \n " )
5469 puts " Updated package.json with minimum versions:"
5570 puts " React: #{ ExampleType ::MINIMUM_REACT_VERSION } "
5671 puts " Shakapacker: #{ ExampleType ::MINIMUM_SHAKAPACKER_VERSION } "
5772 end
58- # rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
73+ # rubocop:enable Metrics/AbcSize,Metrics/ CyclomaticComplexity,Metrics/PerceivedComplexity
5974
6075 # Define tasks for each example type
61- ExampleType . all [ :shakapacker_examples ] . each do |example_type |
76+ ExampleType . all [ :shakapacker_examples ] . each do |example_type | # rubocop:disable Metrics/BlockLength
6277 relative_gem_root = Pathname ( gem_root ) . relative_path_from ( Pathname ( example_type . dir ) )
6378 # CLOBBER
6479 desc "Clobbers (deletes) #{ example_type . name_pretty } "
@@ -90,7 +105,11 @@ namespace :shakapacker_examples do # rubocop:disable Metrics/BlockLength
90105 sh_in_dir ( example_type . dir , generator_commands )
91106
92107 # Apply minimum versions for compatibility testing examples
93- apply_minimum_versions ( example_type . dir ) if example_type . minimum_versions
108+ if example_type . minimum_versions
109+ apply_minimum_versions ( example_type . dir )
110+ # Re-run bundle install since Gemfile was updated with pinned shakapacker version
111+ bundle_install_in ( example_type . dir )
112+ end
94113
95114 sh_in_dir ( example_type . dir , "npm install" )
96115 # Generate the component packs after running the generator to ensure all
0 commit comments