Class: HakumiComponents::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/generators/hakumi_components/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_npm_packageObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/generators/hakumi_components/install_generator.rb', line 13

def add_npm_package
  say "Adding @hakumi-dev/hakumi-components to package.json...", :green

  # Install latest pre-release version
  if package_manager == :npm
    run "npm install @hakumi-dev/hakumi-components@pre --save"
  else
    run "yarn add @hakumi-dev/hakumi-components@pre"
  end
end

#setup_form_builderObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/generators/hakumi_components/install_generator.rb', line 127

def setup_form_builder
  say "Setting up FormBuilder (optional)...", :green

  initializer_file = "config/initializers/hakumi_components.rb"

  if File.exist?(initializer_file)
    say "#{initializer_file} already exists", :yellow
  else
    create_file initializer_file, <<~RUBY
      # frozen_string_literal: true

      # Configure default form builder for Hakumi Components
      # This enables automatic validation styling and label generation
      Rails.application.config.action_view.default_form_builder = HakumiComponents::FormBuilder
    RUBY
    say "  ✓ Created #{initializer_file}", :green
  end
end

#setup_javascriptObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/generators/hakumi_components/install_generator.rb', line 25

def setup_javascript
  say "Setting up JavaScript...", :green

  js_file = detect_js_entry_point

  if js_file
    content = File.read(js_file)

    unless content.include?("hakumi-components")
      # Full import with default export and named export
      import_lines = [
        'import HakumiComponents, { registerHakumiControllers } from "@hakumi-dev/hakumi-components"'
      ]

      # Add global window assignment and registration
      register_lines = [
        "window.HakumiComponents = HakumiComponents",
        "registerHakumiControllers(application)"
      ]

      # Insert imports after other imports
      lines = content.split("\n")
      last_import_index = lines.rindex { |line| line.match?(/^import\s/) }

      if last_import_index
        # Insert after last import
        import_lines.reverse_each do |line|
          lines.insert(last_import_index + 1, line)
        end
      else
        # Add at the top
        lines = import_lines + [ "" ] + lines
      end

      # Find where to add registration (after Stimulus application setup)
      application_index = lines.index { |line| line.include?("Application.start()") || line.include?("application = Application") }

      if application_index
        # Add after application setup
        register_lines.reverse_each do |line|
          lines.insert(application_index + 1, line)
        end
      else
        # Add at the end
        lines += [ "" ] + register_lines
      end

      content = lines.join("\n")
      File.write(js_file, content)
      say "  ✓ Updated #{js_file}", :green
    else
      say "#{js_file} already configured", :yellow
    end
  else
    say "  ⚠ Could not find JavaScript entry point. Please add manually:", :yellow
    say ""
    say "    import HakumiComponents, { registerHakumiControllers } from \"@hakumi-dev/hakumi-components\""
    say "    window.HakumiComponents = HakumiComponents"
    say "    registerHakumiControllers(application)"
    say ""
  end
end

#setup_stylesheetsObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/generators/hakumi_components/install_generator.rb', line 89

def setup_stylesheets
  say "Setting up stylesheets...", :green

  scss_file = detect_scss_file

  if scss_file
    content = File.read(scss_file)

    unless content.include?("hakumi-components")
      # Use modern @use syntax for Vite/Webpack, @import for Sprockets
      import_line = if using_modern_bundler?
        '@use "@hakumi-dev/hakumi-components/styles";'
      else
        '@import "@hakumi-dev/hakumi-components/styles";'
      end

      # Add at the top of the file
      File.write(scss_file, "#{import_line}\n#{content}")
      say "  ✓ Updated #{scss_file}", :green
      say "    Using #{using_modern_bundler? ? '@use' : '@import'} syntax", :cyan
    else
      say "#{scss_file} already configured", :yellow
    end
  else
    say "  ⚠ Could not find SCSS file. Please add manually:", :yellow
    say ""
    if using_modern_bundler?
      say "    // For Vite/Webpack (modern bundlers):"
      say "    @use \"@hakumi-dev/hakumi-components/styles\";"
    else
      say "    // For Sprockets:"
      say "    @import \"@hakumi-dev/hakumi-components/styles\";"
    end
    say ""
  end
end

#show_post_installObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/generators/hakumi_components/install_generator.rb', line 147

def show_post_install
  say ""
  say "=" * 70, :green
  say "🎉 Hakumi Components installed successfully!", :green
  say "=" * 70, :green
  say ""
  say "📋 Next steps:", :cyan
  say ""
  say "  1. Restart your Rails server and asset pipeline"
  say "  2. Try using a component in your views:"
  say ""
  say "     <%= render HakumiComponents::Button::Component.new(type: :primary) do %>", :cyan
  say "       Click me", :cyan
  say "     <% end %>", :cyan
  say ""
  say "  3. Check the browser console for confirmation:"
  say "     → Should see: \"[HakumiComponents] Registered X controllers\""
  say ""
  say "📚 Resources:", :cyan
  say "  • Live Playground: https://antrails.kb714.com"
  say "  • Documentation:   https://github.com/hakumi-dev/hakumi-components"
  say "  • npm Package:     https://www.npmjs.com/package/@hakumi-dev/hakumi-components"
  say ""

  if needs_manual_setup?
    say "⚠️  Manual setup required:", :yellow
    say "  Some files could not be configured automatically."
    say "  Please refer to the output above for manual instructions."
    say ""
  end
end