Class: JetUi::Generators::InstallGenerator

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

Overview

Sets up JetUi in the host Rails application.

Usage:

rails generate jet_ui:install

Constant Summary collapse

JS_CONTROLLERS_FILE =
'app/javascript/controllers/index.js'
TAILWIND_SOURCE_FILES =
%w[
  app/assets/tailwind/application.css
  app/assets/stylesheets/application.tailwind.css
  app/assets/stylesheets/application.postcss.css
].freeze

Instance Method Summary collapse

Instance Method Details

#inject_cssObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/jet_ui/install/install_generator.rb', line 37

def inject_css
  if (file = tailwind_source_file)
    content = File.read(File.join(destination_root, file))
    if content.include?('jet_ui')
      say "  JetUi CSS already imported in #{file}", :yellow
    else
      # Tailwind CSS v4: import each file with an absolute path so the
      # Tailwind CLI can process @apply directives at build time.
      append_to_file file, tailwind_imports
      say "  Injected JetUi imports into #{file}", :green
    end
  else
    say '  Could not detect a Tailwind CSS source file.', :yellow
    say '  Add the following to your Tailwind CSS source manually:', :yellow
    say tailwind_imports, :yellow
  end
end

#inject_jsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/generators/jet_ui/install/install_generator.rb', line 55

def inject_js
  path = File.join(destination_root, JS_CONTROLLERS_FILE)
  unless File.exist?(path)
    say '  No Stimulus controllers index found — skipping JS setup.', :yellow
    say '  If you use Stimulus, add this line to your controllers index manually:', :yellow
    say %(    eagerLoadControllersFrom("jet_ui", application)), :yellow
    return
  end

  if File.read(path).include?('jet_ui')
    say "  JetUi controllers already registered in #{JS_CONTROLLERS_FILE}", :yellow
  else
    insert_into_file JS_CONTROLLERS_FILE, after: /eagerLoadControllersFrom\("controllers".*\n/ do
      %(eagerLoadControllersFrom("jet_ui", application)\n)
    end
    say "  Registered JetUi controllers in #{JS_CONTROLLERS_FILE}", :green
  end
end

#show_usageObject



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

def show_usage
  say "\nJetUi installed!\n", :green
  say 'Add flash messages to your layout (e.g. app/views/layouts/application.html.erb):'
  say %(  <%= jet_ui.flash(messages: flash) %>\n)
  say 'Usage in your views:'
  say %(  <%= jet_ui.btn variant: :default do %>Save<% end %>)
  say %(  <%= jet_ui.card do %>)
  say %(    <%= jet_ui.card_header do %>)
  say %(      <%= jet_ui.card_title "Hello" %>)
  say %(    <% end %>)
  say %(    <%= jet_ui.card_body do %>Content<% end %>)
  say %(  <% end %>\n)
  say 'To eject a component for local customization:'
  say '  rails generate jet_ui:eject btn'
  say '  rails generate jet_ui:eject card'
  say "  rails generate jet_ui:eject btn card\n"
end