Class: ReactOnRails::Generators::BaseGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
GeneratorHelper, JsDependencyManager
Defined in:
lib/generators/react_on_rails/base_generator.rb

Constant Summary collapse

CONFIGURE_RSPEC_TO_COMPILE_ASSETS =
<<-STR.strip_heredoc
  RSpec.configure do |config|
    # Ensure that if we are running js tests, we are using latest webpack assets
    # This will use the defaults of :js and :server_rendering meta tags
    ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
  end
STR

Constants included from JsDependencyManager

JsDependencyManager::CSS_DEPENDENCIES, JsDependencyManager::DEV_DEPENDENCIES, JsDependencyManager::REACT_DEPENDENCIES, JsDependencyManager::RSPACK_DEPENDENCIES, JsDependencyManager::RSPACK_DEV_DEPENDENCIES, JsDependencyManager::TYPESCRIPT_DEPENDENCIES

Instance Method Summary collapse

Methods included from GeneratorHelper

#add_documentation_reference, #add_npm_dependencies, #component_extension, #copy_file_and_missing_parent_directories, #dest_dir_exists?, #dest_file_exists?, #empty_directory_with_keep_file, #keep_file, #package_json, #setup_file_error, #shakapacker_version_9_or_higher?, #symlink_dest_file_to_dest_file

Instance Method Details

#add_base_gems_to_gemfileObject



110
111
112
# File 'lib/generators/react_on_rails/base_generator.rb', line 110

def add_base_gems_to_gemfile
  run "bundle"
end

#add_hello_world_routeObject



30
31
32
# File 'lib/generators/react_on_rails/base_generator.rb', line 30

def add_hello_world_route
  route "get 'hello_world', to: 'hello_world#index'"
end

#append_to_spec_rails_helperObject



133
134
135
136
137
138
139
140
141
# File 'lib/generators/react_on_rails/base_generator.rb', line 133

def append_to_spec_rails_helper
  rails_helper = File.join(destination_root, "spec/rails_helper.rb")
  if File.exist?(rails_helper)
    add_configure_rspec_to_compile_assets(rails_helper)
  else
    spec_helper = File.join(destination_root, "spec/spec_helper.rb")
    add_configure_rspec_to_compile_assets(spec_helper) if File.exist?(spec_helper)
  end
end

#copy_base_filesObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/generators/react_on_rails/base_generator.rb', line 42

def copy_base_files
  base_path = "base/base/"
  base_files = %w[app/controllers/hello_world_controller.rb
                  app/views/layouts/hello_world.html.erb
                  Procfile.dev
                  Procfile.dev-static-assets
                  Procfile.dev-prod-assets
                  .dev-services.yml.example
                  bin/shakapacker-precompile-hook]
  base_templates = %w[config/initializers/react_on_rails.rb]
  base_files.each { |file| copy_file("#{base_path}#{file}", file) }
  base_templates.each do |file|
    template("#{base_path}/#{file}.tt", file)
  end

  # Make the hook script executable (copy_file guarantees it exists)
  File.chmod(0o755, File.join(destination_root, "bin/shakapacker-precompile-hook"))
end

#copy_js_bundle_filesObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/generators/react_on_rails/base_generator.rb', line 61

def copy_js_bundle_files
  base_path = "base/base/"
  base_files = %w[app/javascript/packs/server-bundle.js]

  # Only copy HelloWorld.module.css for non-Redux components
  # Redux components handle their own CSS files
  base_files << "app/javascript/src/HelloWorld/ror_components/HelloWorld.module.css" unless options.redux?

  base_files.each { |file| copy_file("#{base_path}#{file}", file) }
end

#copy_packer_configObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/generators/react_on_rails/base_generator.rb', line 92

def copy_packer_config
  # Skip copying if Shakapacker was just installed (to avoid conflicts)
  # Check for a temporary marker file that indicates fresh Shakapacker install
  if File.exist?(".shakapacker_just_installed")
    puts "Skipping Shakapacker config copy (already installed by Shakapacker installer)"
    File.delete(".shakapacker_just_installed") # Clean up marker
    configure_rspack_in_shakapacker if options.rspack?
    return
  end

  puts "Adding Shakapacker #{ReactOnRails::PackerUtils.shakapacker_version} config"
  base_path = "base/base/"
  config = "config/shakapacker.yml"
  # Use template to enable version-aware configuration
  template("#{base_path}#{config}.tt", config)
  configure_rspack_in_shakapacker if options.rspack?
end

#copy_webpack_configObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/generators/react_on_rails/base_generator.rb', line 72

def copy_webpack_config
  puts "Adding Webpack config"
  base_path = "base/base"
  base_files = %w[babel.config.js
                  config/webpack/clientWebpackConfig.js
                  config/webpack/commonWebpackConfig.js
                  config/webpack/test.js
                  config/webpack/development.js
                  config/webpack/production.js
                  config/webpack/serverWebpackConfig.js
                  config/webpack/generateWebpackConfigs.js]
  config = {
    message: "// The source code including full typescript support is available at:"
  }
  base_files.each { |file| template("#{base_path}/#{file}.tt", file, config) }

  # Handle webpack.config.js separately with smart replacement
  copy_webpack_main_config(base_path, config)
end

#create_react_directoriesObject



34
35
36
37
38
39
40
# File 'lib/generators/react_on_rails/base_generator.rb', line 34

def create_react_directories
  # Create auto-bundling directory structure for non-Redux components only
  # Redux components handle their own directory structure
  return if options.redux?

  empty_directory("app/javascript/src/HelloWorld/ror_components")
end

#update_gitignore_for_auto_registrationObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/generators/react_on_rails/base_generator.rb', line 114

def update_gitignore_for_auto_registration
  gitignore_path = File.join(destination_root, ".gitignore")
  return unless File.exist?(gitignore_path)

  gitignore_content = File.read(gitignore_path)

  additions = []
  additions << "**/generated/**" unless gitignore_content.include?("**/generated/**")
  additions << "ssr-generated" unless gitignore_content.include?("ssr-generated")

  return if additions.empty?

  append_to_file ".gitignore" do
    lines = ["\n# Generated React on Rails packs"]
    lines.concat(additions)
    "#{lines.join("\n")}\n"
  end
end