Class: ReactOnRails::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
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"

Constants included from JsDependencyManager

JsDependencyManager::CSS_DEPENDENCIES, JsDependencyManager::DEV_DEPENDENCIES, JsDependencyManager::PRO_DEPENDENCIES, JsDependencyManager::REACT_DEPENDENCIES, JsDependencyManager::RSC_DEPENDENCIES, JsDependencyManager::RSPACK_DEPENDENCIES, JsDependencyManager::RSPACK_DEV_DEPENDENCIES, JsDependencyManager::SWC_DEPENDENCIES, JsDependencyManager::TYPESCRIPT_DEPENDENCIES

Instance Method Summary collapse

Methods included from RscSetup

#setup_rsc, #warn_about_react_version_for_rsc

Methods included from ProSetup

#missing_pro_gem?, #setup_pro

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, #package_json, #print_generator_messages, #pro_gem_installed?, #resolve_server_client_or_both_path, #setup_file_error, #shakapacker_version_9_or_higher?, #symlink_dest_file_to_dest_file, #use_pro?, #use_rsc?, #using_rspack?, #using_swc?

Instance Method Details

#run_generatorsObject

Note:

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:

  1. Validating prerequisites

  2. Installing required packages

  3. Generating configuration files

  4. Setting up example components



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/generators/react_on_rails/install_generator.rb', line 94

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? || options.ignore_warnings?
    invoke_generators
    add_bin_scripts
    # Only add the post install message if not using Redux
    # Redux generator handles its own messages
    add_post_install_message unless options.redux?
  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")
  print_generator_messages
end