Class: ReactOnRails::Generators::BaseGenerator
Constant Summary
collapse
- CONFIGURE_RSPEC_TO_COMPILE_ASSETS =
<<~STR
# 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
# Requires config.build_test_command in config/initializers/react_on_rails.rb.
# This is the default setup for React on Rails generated apps.
ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
STR
- CONFIGURE_MINITEST_TO_COMPILE_ASSETS =
<<~STR
# Ensure that tests run against fresh webpack assets.
ActiveSupport::TestCase.setup do
ReactOnRails::TestHelper.ensure_assets_compiled
end
STR
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
#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?, #shakapacker_version_9_or_higher?, #use_pro?, #use_rsc?, #using_rspack?, #using_swc?
Instance Method Details
#add_base_gems_to_gemfile ⇒ Object
280
281
282
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 280
def add_base_gems_to_gemfile
run "bundle"
end
|
#add_hello_world_route ⇒ Object
166
167
168
169
170
171
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 166
def add_hello_world_route
return if use_rsc? && !options.redux?
route "get 'hello_world', to: 'hello_world#index'"
end
|
#add_root_route ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 121
def add_root_route
return unless options.new_app?
return if defined?(@new_app_root_route_added)
@new_app_root_route_added = false
if preexisting_root_route?
say_status :skip, "Root route already exists; keeping existing root route", :yellow
return
end
routes_path = "config/routes.rb"
routes_full_path = File.join(destination_root, routes_path)
unless File.file?(routes_full_path)
say_status :warn, "Could not inject root route; config/routes.rb was not found", :yellow
return
end
routes_draw_declaration = /^\s*Rails\.application\.routes\.draw do\r?\n/
unless File.read(routes_full_path).match?(routes_draw_declaration)
say_status :warn, "Could not inject root route; config/routes.rb format was unexpected", :yellow
return
end
inject_into_file routes_path,
%( root to: "home#index"\n),
after: routes_draw_declaration
if options[:pretend]
@new_app_root_route_added = true
return
end
if File.read(routes_full_path).include?('root to: "home#index"')
@new_app_root_route_added = true
return
end
say_status :warn, "Could not inject root route; config/routes.rb format was unexpected", :yellow
end
|
#append_to_spec_rails_helper ⇒ Object
304
305
306
307
308
309
310
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 304
def append_to_spec_rails_helper
rspec_helper = preferred_rspec_helper_file
add_configure_rspec_to_compile_assets(rspec_helper) if rspec_helper
test_helper = File.join(destination_root, "test/test_helper.rb")
add_configure_minitest_to_compile_assets(test_helper) if File.exist?(test_helper)
end
|
#copy_base_files ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 180
def copy_base_files
ensure_new_app_root_route_initialized
base_path = "base/base/"
base_files = %w[Procfile.dev
Procfile.dev-static-assets
Procfile.dev-prod-assets
.dev-services.yml.example
.env.example
bin/shakapacker-precompile-hook]
base_files << "app/views/layouts/react_on_rails_default.html.erb"
base_files << "app/controllers/hello_world_controller.rb" unless use_rsc? && !options.redux?
base_files << "app/controllers/home_controller.rb" if generate_new_app_home_page?
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
if generate_new_app_home_page?
empty_directory("app/views/home")
template("#{base_path}/app/views/home/index.html.erb.tt", "app/views/home/index.html.erb", home_page_config)
end
if options[:pretend]
say_status :pretend, "Skipping chmod on shakapacker-precompile-hook in --pretend mode", :yellow
else
File.chmod(0o755, File.join(destination_root, "bin/shakapacker-precompile-hook"))
end
end
|
#copy_js_bundle_files ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 218
def copy_js_bundle_files
base_path = "base/base/"
base_files = %w[app/javascript/packs/server-bundle.js]
unless options.redux? || use_rsc?
base_files << "app/javascript/src/HelloWorld/ror_components/HelloWorld.module.css"
end
base_files.each { |file| copy_file("#{base_path}#{file}", file) }
end
|
#copy_packer_config ⇒ Object
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 254
def copy_packer_config
base_path = "base/base/"
config = "config/shakapacker.yml"
if options.shakapacker_just_installed?
say "Replacing Shakapacker default config with React on Rails version"
template("#{base_path}#{config}.tt", config, force: true)
else
say "Adding Shakapacker #{ReactOnRails::PackerUtils.shakapacker_version} config"
template("#{base_path}#{config}.tt", config)
end
configure_rspack_in_shakapacker if using_rspack?
configure_precompile_hook_in_shakapacker
configure_private_output_path_in_shakapacker
end
|
#copy_webpack_config ⇒ Object
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 230
def copy_webpack_config
cleanup_stale_webpack_config_dir_for_rspack
say "Adding #{using_rspack? ? 'Rspack' : '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/ServerClientOrBoth.js]
config = { message: DOCS_REFERENCE_MESSAGE }
base_files.each do |file|
template("#{base_path}/#{file}.tt", destination_config_path(file), config)
end
copy_webpack_main_config(base_path, config)
end
|
#create_react_directories ⇒ Object
173
174
175
176
177
178
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 173
def create_react_directories
return if options.redux? || use_rsc?
empty_directory("app/javascript/src/HelloWorld/ror_components")
end
|
#update_gitignore_for_auto_registration ⇒ Object
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 284
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")
additions << ".env" unless gitignore_content.match?(/^\.env$/)
return if additions.empty?
append_to_file ".gitignore" do
lines = ["\n# React on Rails (generated and local files)"]
lines.concat(additions)
"#{lines.join("\n")}\n"
end
end
|