Class: ReactOnRails::Generators::BaseGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- ReactOnRails::Generators::BaseGenerator
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
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
#add_documentation_reference, #add_npm_dependencies, #component_extension, #copy_file_and_missing_parent_directories, #dest_dir_exists?, #dest_file_exists?, #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_swc?
Instance Method Details
#add_base_gems_to_gemfile ⇒ Object
135
136
137
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 135
def add_base_gems_to_gemfile
run "bundle"
end
|
#add_hello_world_route ⇒ Object
42
43
44
45
46
47
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 42
def add_hello_world_route
return if use_rsc? && !options.redux?
route "get 'hello_world', to: 'hello_world#index'"
end
|
#append_to_spec_rails_helper ⇒ Object
158
159
160
161
162
163
164
165
166
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 158
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_files ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 56
def copy_base_files
base_path = "base/base/"
base_files = %w[Procfile.dev
Procfile.dev-static-assets
Procfile.dev-prod-assets
.dev-services.yml.example
bin/shakapacker-precompile-hook]
base_files << "app/views/layouts/hello_world.html.erb"
base_files << "app/controllers/hello_world_controller.rb" unless use_rsc? && !options.redux?
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
File.chmod(0o755, File.join(destination_root, "bin/shakapacker-precompile-hook"))
end
|
#copy_js_bundle_files ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 81
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 113
def copy_packer_config
if File.exist?(".shakapacker_just_installed")
puts "Skipping Shakapacker config copy (already installed by Shakapacker installer)"
File.delete(".shakapacker_just_installed") else
puts "Adding Shakapacker #{ReactOnRails::PackerUtils.shakapacker_version} config"
base_path = "base/base/"
config = "config/shakapacker.yml"
template("#{base_path}#{config}.tt", config)
end
configure_rspack_in_shakapacker if options.rspack?
configure_precompile_hook_in_shakapacker
end
|
#copy_webpack_config ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 93
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/ServerClientOrBoth.js]
config = {
message: "// The source code including full typescript support is available at:"
}
base_files.each { |file| template("#{base_path}/#{file}.tt", file, config) }
copy_webpack_main_config(base_path, config)
end
|
#create_react_directories ⇒ Object
49
50
51
52
53
54
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 49
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 139
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
|