Class: Ruact::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/ruact/install/install_generator.rb

Overview

Installs ruact into the current Rails application.

Performs the following actions:

  1. Creates config/initializers/ruact.rb

  2. Injects ‘include Ruact::Controller` into ApplicationController

  3. Injects the React root div into app/views/layouts/application.html.erb

  4. Creates app/javascript/components/.keep

  5. Creates vite.config.js (or shows manual instructions if one exists)

  6. Creates app/javascript/application.jsx (or skips if one exists)

Run: rails generate ruact:install

Instance Method Summary collapse

Instance Method Details

#append_gitignore_entriesObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/generators/ruact/install/install_generator.rb', line 74

def append_gitignore_entries
  gitignore = destination_root.join(".gitignore")
  return unless gitignore.exist?

  entries = [
    "app/javascript/.ruact/server-functions.ts",
    # Story 9.3 — the route-driven (v2) parallel inspection target is also
    # generated output, never source.
    "app/javascript/.ruact/server-functions.next.ts",
    "tmp/cache/ruact/"
  ]
  # Substring matches (`existing.include?(entry)`) were unsafe — they
  # would skip "tmp/cache/ruact/" when the file already contained
  # "tmp/cache/ruact/some-cache.bin", leaving the directory itself
  # un-ignored. Match by exact normalized line instead.
  existing_lines = File.read(gitignore).each_line.to_set { |line| line.chomp.strip }
  new_entries = entries.reject { |e| existing_lines.include?(e) }
  return if new_entries.empty?

  append_to_file ".gitignore", "\n# ruact (Story 8.0a — auto-generated server-functions module)\n"
  new_entries.each { |entry| append_to_file ".gitignore", "#{entry}\n" }
end

#create_components_directoryObject



58
59
60
61
62
# File 'lib/generators/ruact/install/install_generator.rb', line 58

def create_components_directory
  empty_directory "app/javascript/components"
  create_file "app/javascript/components/.keep" unless
    File.exist?(destination_root.join("app/javascript/components/.keep"))
end

#create_initializerObject



24
25
26
# File 'lib/generators/ruact/install/install_generator.rb', line 24

def create_initializer
  template "initializer.rb.tt", "config/initializers/ruact.rb"
end

#create_javascript_entryObject



122
123
124
# File 'lib/generators/ruact/install/install_generator.rb', line 122

def create_javascript_entry
  template "application.jsx.tt", "app/javascript/application.jsx"
end

#create_server_functions_directoryObject

Story 8.0a — scaffold the directory the codegen writes into and add the generated artifacts to .gitignore. The TS module is regenerated on every boot from the action and query registries, so it should never be version-controlled; same for the bridge JSON under tmp/cache/.



68
69
70
71
72
# File 'lib/generators/ruact/install/install_generator.rb', line 68

def create_server_functions_directory
  empty_directory "app/javascript/.ruact"
  create_file "app/javascript/.ruact/.gitkeep" unless
    File.exist?(destination_root.join("app/javascript/.ruact/.gitkeep"))
end

#create_vite_configObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/generators/ruact/install/install_generator.rb', line 107

def create_vite_config
  vite_config_file = destination_root.join("vite.config.js")

  if vite_config_file.exist?
    say_status "notice", "vite.config.js already exists — add the plugin manually:", :yellow
    say "  1. At the top of vite.config.js, add:"
    say "       import ruact from '#{Ruact.vite_plugin_path}';"
    say "  2. In the plugins array, add: ruact()"
    say ""
    say "  Re-run `rails generate ruact:install --force` to overwrite vite.config.js."
  else
    template "vite.config.js.tt", "vite.config.js"
  end
end

#inject_controller_concernObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/generators/ruact/install/install_generator.rb', line 28

def inject_controller_concern
  controller_file = "app/controllers/application_controller.rb"
  return unless File.exist?(destination_root.join(controller_file))

  content = File.read(destination_root.join(controller_file))
  if content.include?("Ruact::Controller")
    say_status "skip", "Ruact::Controller already included in ApplicationController", :yellow
    return
  end

  inject_into_file controller_file,
                   "\n  include Ruact::Controller\n",
                   after: /class ApplicationController.*\n/
end

#inject_layout_shellObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/ruact/install/install_generator.rb', line 43

def inject_layout_shell
  layout_file = "app/views/layouts/application.html.erb"
  return unless File.exist?(destination_root.join(layout_file))

  content = File.read(destination_root.join(layout_file))
  if content.include?("ruact: root")
    say_status "skip", "Rails RSC root already present in layout", :yellow
    return
  end

  inject_into_file layout_file,
                   "\n    <%# ruact: root %>\n    <div id=\"root\"></div>\n",
                   before: "  </body>"
end

#prime_server_functions_codegenObject

Invokes ‘ruact:server_functions:generate` so a fresh install completes with the AC8-required empty-but-valid generated module on disk. Failures (a NameBridge violation, a collision, an unwritable `tmp/cache/ruact/` directory) propagate intentionally — silencing them via a rescue would let an install finish in a broken state, which is the bug the Re-run review caught.



103
104
105
# File 'lib/generators/ruact/install/install_generator.rb', line 103

def prime_server_functions_codegen
  rake "ruact:server_functions:generate"
end

#show_post_install_messageObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/generators/ruact/install/install_generator.rb', line 126

def show_post_install_message
  say ""
  say "=" * 60
  say "  ruact installed successfully!"
  say "=" * 60
  say ""
  say "Next steps:"
  say "  1. Start the Vite dev server:  npm run dev"
  say "  2. Start Rails:                rails server"
  say "  3. Add <MyComponent /> to any ERB view"
  say ""
  say "Note: Re-run this generator after updating the ruact gem"
  say "to refresh the bundled Vite plugin path in vite.config.js."
  say ""
end