Class: Wasmify::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Wasmify::InstallGenerator
- Defined in:
- lib/generators/wasmify/install/install_generator.rb
Constant Summary collapse
- KNOWN_NO_WASM_GEMS =
%w[ bootsnap puma sqlite3 activerecord-enhancedsqlite3-adapter pg mysql2 redis solid_cable solid_queue solid_cache jsbundling-rails cssbundling-rails tailwindcss-rails thruster kamal byebug web-console listen spring debug ].freeze
Instance Method Summary collapse
- #add_tzinfo_data_to_gemfile ⇒ Object
- #add_wasm_group_to_gemfile ⇒ Object
- #configure_action_cable ⇒ Object
- #configure_boot_file ⇒ Object
- #configure_database ⇒ Object
- #configure_gitignore ⇒ Object
- #copy_files ⇒ Object
- #disable_ruby_version_check_in_gemfile ⇒ Object
- #finish ⇒ Object
- #fix_solid_queeue_production_config ⇒ Object
- #inject_wasmify_shim_into_environment ⇒ Object
Instance Method Details
#add_tzinfo_data_to_gemfile ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/generators/wasmify/install/install_generator.rb', line 67 def add_tzinfo_data_to_gemfile # First, comment out the existing tzinfo-data gem declaration comment_lines "Gemfile", /^gem ['"]tzinfo-data['"]/ append_to_file "Gemfile", "\n group :wasm do\n gem \"tzinfo-data\"\n end\n RUBY\nend\n" |
#add_wasm_group_to_gemfile ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/generators/wasmify/install/install_generator.rb', line 102 def add_wasm_group_to_gemfile # This is a very straightforward implementation: # - scan the Gemfile for _root_ dependencies (not within groups) # - add `group: [:default, :wasm]` to those not from the exclude list top_gems = [] File.read("Gemfile").then do |gemfile| gemfile.scan(/^gem ['"]([^'"]+)['"](.*)$/).each do |match| gem_name = match.first top_gems << gem_name unless match.last&.include?(":wasm") end end gems_to_include = top_gems - KNOWN_NO_WASM_GEMS return if gems_to_include.empty? regexp = /^gem ['"](?:#{gems_to_include.join("|")})['"][^#\n]*/ gsub_file "Gemfile", regexp do |match| match << ", group: [:default, :wasm]" end end |
#configure_action_cable ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/generators/wasmify/install/install_generator.rb', line 18 def configure_action_cable in_root do next unless File.file?("config/cable.yml") append_to_file "config/cable.yml", "\n wasm:\n adapter: <%= ENV.fetch(\"ACTION_CABLE_ADAPTER\") { \"inline\" } %>\n EOF\n end\nend\n" |
#configure_boot_file ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/generators/wasmify/install/install_generator.rb', line 50 def configure_boot_file inject_into_file "config/boot.rb", after: /require ['"]bundler\/setup['"]/ do " unless RUBY_PLATFORM =~ /wasm/" end # Disable bootsnap if any inject_into_file "config/boot.rb", after: /require ['"]bootsnap\/setup['"]/ do %q( unless ENV["RAILS_ENV"] == "wasm") end end |
#configure_database ⇒ Object
11 12 13 14 15 16 |
# File 'lib/generators/wasmify/install/install_generator.rb', line 11 def configure_database append_to_file "config/database.yml", " wasm:\n adapter: <%= ENV.fetch(\"ACTIVE_RECORD_ADAPTER\") { \"nulldb\" } %>\n EOF\nend\n" |
#configure_gitignore ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/generators/wasmify/install/install_generator.rb', line 30 def configure_gitignore append_to_file ".gitignore", " # Ignore the compiled WebAssembly modules\n *.wasm\n # Ignore ruby.wasm build artefacts\n build/\n rubies/\n dist/\n EOF\nend\n" |
#copy_files ⇒ Object
6 7 8 9 |
# File 'lib/generators/wasmify/install/install_generator.rb', line 6 def copy_files template "config/wasmify.yml" template "config/environments/wasm.rb" end |
#disable_ruby_version_check_in_gemfile ⇒ Object
61 62 63 64 65 |
# File 'lib/generators/wasmify/install/install_generator.rb', line 61 def disable_ruby_version_check_in_gemfile inject_into_file "Gemfile", after: /^ruby[^#\n]+/ do " unless RUBY_PLATFORM =~ /wasm/" end end |
#finish ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/generators/wasmify/install/install_generator.rb', line 133 def finish run "bundle install" say_status :info, "✅ The application is prepared for Wasm-ification!\n\n" \ "Next steps are:\n" \ " - Check your Gemfile: add `group: [:default, :wasm]` to the dependencies required in Wasm runtime" \ " - Run `bin/rails wasmify:build:core:verify` to see if the bundle compiles" end |
#fix_solid_queeue_production_config ⇒ Object
127 128 129 130 131 |
# File 'lib/generators/wasmify/install/install_generator.rb', line 127 def fix_solid_queeue_production_config inject_into_file "config/environments/production.rb", after: %r{config.solid_queue.connects_to = [^#\n]+} do " if config.respond_to?(:solid_queue)" end end |
#inject_wasmify_shim_into_environment ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/generators/wasmify/install/install_generator.rb', line 41 def inject_wasmify_shim_into_environment inject_into_file "config/application.rb", after: /require_relative\s+['"]boot['"]\n/ do "\n require \"wasmify/rails/shim\"\n RUBY\n end\nend\n" |