Class: ReactOnRails::Generators::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- ReactOnRails::Generators::InstallGenerator
- Includes:
- GeneratorHelper, JsDependencyManager, ProSetup, RscSetup
- Defined in:
- lib/generators/react_on_rails/install_generator.rb
Overview
TODO: Extract more modules to reduce class length below 150 lines.
Candidates: ShakapackerSetup (~100 lines), TypeScriptSetup (~60 lines),
ValidationHelpers (~80 lines for Node/package manager checks).
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- SHAKAPACKER_YML_PATH =
Removed: –skip-shakapacker-install (Shakapacker is now a required dependency)
"config/shakapacker.yml"- HELLO_WORLD_ROUTE =
"hello_world"- HELLO_SERVER_ROUTE =
"hello_server"- STOCK_RAILS_BIN_DEV =
Matches the stock ‘bin/dev` written by Rails 8.x. Rails 7.1 commonly generated a foreman-based shell script instead, which stock_rails_bin_dev? also recognizes so the React on Rails template can replace either variant.
<<~RUBY #!/usr/bin/env ruby exec "./bin/rails", "server", *ARGV RUBY
- LEGACY_FOREMAN_BIN_DEV_TEMPLATES =
Recognize only known legacy Rails foreman templates. Any other variant is treated as customized so install does not overwrite app-specific logic.
[ <<~BASH, <<~SH, <<~BASH, <<~SH #!/usr/bin/env sh if ! gem list foreman -i --silent; then gem install foreman fi exec foreman start -f Procfile.dev $@ SH ].map { |template| template.gsub("\r\n", "\n").strip }.freeze
Constants included from RscSetup
RscSetup::DEFAULT_LAYOUT_NAME, RscSetup::LEGACY_LAYOUT_NAME, RscSetup::MAX_LAYOUT_NAME_ATTEMPTS, RscSetup::RSC_FALLBACK_LAYOUT_NAME, RscSetup::RSC_GENERATED_LAYOUT_NAME_PATTERN
Constants included from ProSetup
ProSetup::AUTO_INSTALL_TIMEOUT, ProSetup::PRO_GEM_NAME, ProSetup::TERMINATION_GRACE_PERIOD
Constants included from JsDependencyManager
JsDependencyManager::BABEL_REACT_DEPENDENCIES, JsDependencyManager::CSS_DEPENDENCIES, JsDependencyManager::DEV_DEPENDENCIES, JsDependencyManager::PRO_DEPENDENCIES, JsDependencyManager::REACT_DEPENDENCIES, JsDependencyManager::RSC_DEPENDENCIES, JsDependencyManager::RSC_PACKAGE_VERSION_PIN, JsDependencyManager::RSC_REACT_VERSION_RANGE, JsDependencyManager::RSPACK_DEPENDENCIES, JsDependencyManager::RSPACK_DEV_DEPENDENCIES, JsDependencyManager::SWC_DEPENDENCIES, JsDependencyManager::TYPESCRIPT_DEPENDENCIES
Instance Method Summary collapse
-
#run_generators ⇒ Object
Main generator entry point.
Methods included from RscSetup
#setup_rsc, #warn_about_react_version_for_rsc
Methods included from DemoPageConfig
#build_hello_server_view_config, #build_hello_world_view_config
Methods included from ProSetup
Methods included from GeneratorHelper
#add_documentation_reference, #add_npm_dependencies, #component_extension, #copy_file_and_missing_parent_directories, #dest_dir_exists?, #dest_file_exists?, #destination_config_path, #detect_react_version, #empty_directory_with_keep_file, #gem_in_lockfile?, #keep_file, #mark_pro_gem_installed!, #package_json, #print_generator_messages, #pro_gem_installed?, #resolve_server_client_or_both_path, #root_route_present?, #setup_file_error, #shakapacker_version_9_or_higher?, #symlink_dest_file_to_dest_file, #use_pro?, #use_rsc?, #use_rsc_pro_mode?, #using_rspack?, #using_swc?
Instance Method Details
#run_generators ⇒ Object
Validation Skipping: Sets ENV to prevent version validation from running during generator execution. The npm package isn’t installed until midway through the generator, so validation would fail if run during Rails initialization. The ensure block guarantees cleanup even if the generator fails.
Main generator entry point
Sets up React on Rails in a Rails application by:
-
Validating prerequisites
-
Installing required packages
-
Generating configuration files
-
Setting up example components
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/generators/react_on_rails/install_generator.rb', line 158 def run_generators # Set environment variable to skip validation during generator run # This is inherited by all invoked generators and persists through Rails initialization # See lib/react_on_rails/engine.rb for the validation skip logic ENV["REACT_ON_RAILS_SKIP_VALIDATION"] = "true" if installation_prerequisites_met? || .ignore_warnings? invoke_generators add_bin_scripts else error = <<~MSG.strip 🚫 React on Rails generator prerequisites not met! Please resolve the issues listed above before continuing. All prerequisites must be satisfied for a successful installation. Use --ignore-warnings to bypass checks (not recommended). MSG GeneratorMessages.add_error(error) end ensure # Always clean up ENV variable, even if generator fails # CRITICAL: ENV cleanup must come first to ensure it executes even if # print_generator_messages raises an exception. This prevents ENV pollution # that could affect subsequent processes. ENV.delete("REACT_ON_RAILS_SKIP_VALIDATION") end |