Class: ReactOnRails::Generators::ReactWithReduxGenerator

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

Instance Method Summary collapse

Methods included from DemoPageConfig

#build_hello_server_view_config, #build_hello_world_view_config

Methods included from GeneratorHelper

#add_documentation_reference, #add_npm_dependencies, #bundler_flag_given?, #component_extension, #destination_config_path, #detect_react_version, #explicit_bundler_choice, #gem_in_lockfile?, #package_json, #print_generator_messages, #pro_gem_installed?, #resolve_server_client_or_both_path, #root_route_present?, #rsc_plugin_class_name, #rsc_plugin_import_path, #rspack_bundler_default, #shakapacker_version_9_or_higher?, #use_pro?, #use_rsc?, #use_tailwind?, #using_rspack?, #using_swc?

Instance Method Details

#add_redux_npm_dependenciesObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 121

def add_redux_npm_dependencies
  # Add Redux dependencies as regular dependencies
  regular_packages = %w[redux react-redux]

  # Try using GeneratorHelper first (package manager agnostic)
  success = add_npm_dependencies(regular_packages)

  # Fallback to package manager detection if GeneratorHelper fails
  return if success

  package_manager = GeneratorMessages.detect_package_manager(app_root: destination_root)
  return unless package_manager

  install_packages_with_fallback(regular_packages, dev: false, package_manager:)
end

#add_redux_specific_messagesObject



137
138
139
140
141
142
143
144
145
146
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 137

def add_redux_specific_messages
  return if options.invoked_by_install?

  # Append Redux-specific post-install instructions
  GeneratorMessages.add_info(
    GeneratorMessages.helpful_message_after_installation(component_name: "HelloWorldApp", route: "hello_world",
                                                         pro: Gem.loaded_specs.key?("react_on_rails_pro"),
                                                         app_root: destination_root)
  )
end

#copy_base_filesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 52

def copy_base_files
  base_js_path = "redux/base"
  ext = component_extension(options)

  # Copy Redux-connected component to auto-bundling structure
  copy_file("#{base_js_path}/app/javascript/bundles/HelloWorld/startup/HelloWorldApp.client.#{ext}",
            "app/javascript/src/HelloWorldApp/ror_components/HelloWorldApp.client.#{ext}")
  copy_file("#{base_js_path}/app/javascript/bundles/HelloWorld/startup/HelloWorldApp.server.#{ext}",
            "app/javascript/src/HelloWorldApp/ror_components/HelloWorldApp.server.#{ext}")

  unless use_tailwind?
    copy_file("#{base_js_path}/app/javascript/bundles/HelloWorld/components/HelloWorld.module.css",
              "app/javascript/src/HelloWorldApp/components/HelloWorld.module.css")
  end

  # Update import paths in client component
  ror_client_file = "app/javascript/src/HelloWorldApp/ror_components/HelloWorldApp.client.#{ext}"
  gsub_file(ror_client_file, "../store/helloWorldStore", "../store/helloWorldStore")
  gsub_file(ror_client_file, "../containers/HelloWorldContainer",
            "../containers/HelloWorldContainer")
  return unless use_tailwind?

  stylesheet_import = "import '../../../stylesheets/application.css';\n"
  ror_client_file_path = File.join(destination_root, ror_client_file)
  if options[:pretend]
    say_status :pretend, "Would add Tailwind stylesheet import to #{ror_client_file}", :yellow
    return
  end
  return if File.read(ror_client_file_path).include?(stylesheet_import)

  prepend_to_file(ror_client_file, stylesheet_import)
end

#copy_base_redux_filesObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 85

def copy_base_redux_files
  base_hello_world_path = "redux/base/app/javascript/bundles/HelloWorld"
  tailwind_hello_world_path = "redux/tailwind/app/javascript/bundles/HelloWorld"
  redux_extension = options.typescript? ? "ts" : "js"

  # Copy Redux infrastructure files with appropriate extension
  %W[actions/helloWorldActionCreators.#{redux_extension}
     containers/HelloWorldContainer.#{redux_extension}
     constants/helloWorldConstants.#{redux_extension}
     reducers/helloWorldReducer.#{redux_extension}
     store/helloWorldStore.#{redux_extension}].each do |file|
       copy_file("#{base_hello_world_path}/#{file}",
                 "app/javascript/src/HelloWorldApp/#{file}")
     end

  component_file = "components/HelloWorld.#{component_extension(options)}"
  component_source_path = use_tailwind? ? tailwind_hello_world_path : base_hello_world_path
  copy_file("#{component_source_path}/#{component_file}",
            "app/javascript/src/HelloWorldApp/#{component_file}")
end

#create_appropriate_templatesObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 106

def create_appropriate_templates
  base_path = "base/base"

  # Only create the view template - no manual bundle needed for auto-bundling
  template("#{base_path}/app/views/hello_world/index.html.erb.tt",
           "app/views/hello_world/index.html.erb",
           build_hello_world_view_config(
             component_name: "HelloWorldApp",
             source_path: "app/javascript/src/HelloWorldApp/",
             landing_page: new_app_landing_page_available?,
             redux: true,
             rsc_demo: options[:rsc]
           ))
end

#create_redux_directoriesObject



43
44
45
46
47
48
49
50
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 43

def create_redux_directories
  # Create auto-bundling directory structure for Redux
  empty_directory("app/javascript/src/HelloWorldApp/ror_components")

  # Create Redux support directories within the component directory
  dirs = %w[actions constants containers reducers store components]
  dirs.each { |name| empty_directory("app/javascript/src/HelloWorldApp/#{name}") }
end