Class: ReactOnRails::Generators::BaseGenerator

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

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, #symlink_dest_file_to_dest_file

Instance Method Details

#add_base_gems_to_gemfileObject



94
95
96
# File 'lib/generators/react_on_rails/base_generator.rb', line 94

def add_base_gems_to_gemfile
  run "bundle"
end

#add_hello_world_routeObject



22
23
24
# File 'lib/generators/react_on_rails/base_generator.rb', line 22

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

#append_to_spec_rails_helperObject



117
118
119
120
121
122
123
124
125
# File 'lib/generators/react_on_rails/base_generator.rb', line 117

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



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generators/react_on_rails/base_generator.rb', line 34

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]
  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
end

#copy_js_bundle_filesObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/react_on_rails/base_generator.rb', line 48

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/generators/react_on_rails/base_generator.rb', line 79

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
    return
  end

  puts "Adding Shakapacker #{ReactOnRails::PackerUtils.shakapacker_version} config"
  base_path = "base/base/"
  config = "config/shakapacker.yml"
  copy_file("#{base_path}#{config}", config)
end

#copy_webpack_configObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/react_on_rails/base_generator.rb', line 59

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



26
27
28
29
30
31
32
# File 'lib/generators/react_on_rails/base_generator.rb', line 26

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/generators/react_on_rails/base_generator.rb', line 98

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