Class: Odt::Generators::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#copy_stimulus_controller_and_registry ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/odt/install_generator.rb', line 28

def copy_stimulus_controller_and_registry
  say "Copying Stimulus controller and component registry...", :green
  template "odt_registry.js", "app/javascript/odt_registry.js"
  template "odt_component_controller.js", "app/javascript/controllers/odt_component_controller.js"
  
  # If stimulus manifest exists (rails stimulus:manifest:update)
  if File.exist?("bin/rails")
    run "bin/rails stimulus:manifest:update", abort_on_failure: false
  end
end

#inject_stylesheets ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/generators/odt/install_generator.rb', line 39

def inject_stylesheets
  say "Configuring ODT stylesheets...", :green

  css_candidates = [
    "app/assets/stylesheets/application.tailwind.css",
    "app/assets/stylesheets/application.css",
    "app/assets/stylesheets/application.scss",
    "app/javascript/application.css",
    "app/javascript/entrypoints/application.css"
  ]

  target_css = css_candidates.find { |file| File.exist?(file) }

  if target_css
    imports = %(\n/* ODT-Lightweight-UI tokens & styles */\n@import "odt-lightweight-ui/tokens.css";\n@import "odt-lightweight-ui/styles.css";\n)
    append_to_file target_css, imports
    say "Added ODT CSS imports to #{target_css}", :green
  else
    say "Could not automatically locate your main CSS file. Please add:\n  @import \"odt-lightweight-ui/tokens.css\";\n  @import \"odt-lightweight-ui/styles.css\";\nmanually to your application stylesheet.", :yellow
  end
end

#install_javascript_dependencies ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/generators/odt/install_generator.rb', line 10

def install_javascript_dependencies
  say "Installing JS dependencies (odt-lightweight-ui, react, react-dom)...", :green

  packages = "odt-lightweight-ui react react-dom @hotwired/stimulus"

  if File.exist?("yarn.lock")
    run "yarn add #{packages}"
  elsif File.exist?("pnpm-lock.yaml")
    run "pnpm add #{packages}"
  elsif File.exist?("bun.lockb") || File.exist?("bun.lock")
    run "bun add #{packages}"
  elsif File.exist?("package.json")
    run "npm install #{packages}"
  else
    say "No package.json detected. Please ensure your Rails JS bundler (esbuild, vite, or importmap) is configured.", :yellow
  end
end

#show_finish_message ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/generators/odt/install_generator.rb', line 61

def show_finish_message
  say ""
  say "=========================================================", :cyan
  say " 🎉 ODT-Lightweight-UI for Rails successfully installed!", :cyan
  say "=========================================================", :cyan
  say ""
  say "Usage Example in any ERB view (.html.erb):"
  say '  <%= odt_button "Click me", variant: "primary" %>'
  say '  <%= odt_card title: "Profile" do %>'
  say '    <%= odt_input :email, label: "Email" %>'
  say '  <% end %>'
  say ""
end