Class: MaquinaComponents::Generators::InstallGenerator

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

Constant Summary collapse

TOKENS_MARKER =

Marker comment carried by the shape/state token block. Its presence is what makes a second rails g maquina_components:install a no-op for that block instead of a second copy.

"maquina_components:shape-state-tokens"
APP_CSS_PATH =
"app/assets/tailwind/application.css"

Instance Method Summary collapse

Instance Method Details

#add_engine_css_importObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/maquina_components/install/install_generator.rb', line 35

def add_engine_css_import
  css_path = "app/assets/tailwind/application.css"
  full_path = File.join(destination_root, css_path)

  unless File.exist?(full_path)
    say_status :skip, "#{css_path} not found", :yellow
    return
  end

  import_line = '@import "../builds/tailwind/maquina_components_engine.css";'

  if File.read(full_path).include?(import_line)
    say_status :skip, "Engine CSS import already present", :blue
    return
  end

  # Insert after @import "tailwindcss"; or @import 'tailwindcss'; line
  inject_into_file css_path, after: /@import\s+["']tailwindcss["'];?\n/ do
    "\n#{import_line}\n"
  end

  say_status :insert, "Added maquina_components engine CSS import", :green
end

#add_shape_and_state_tokensObject

Safe to re-run against an app that installed an earlier version: the existing palette is left exactly as it is and only the new shape/state token block is appended, guarded by TOKENS_MARKER.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/generators/maquina_components/install/install_generator.rb', line 85

def add_shape_and_state_tokens
  return if options[:skip_theme]

  full_path = File.join(destination_root, APP_CSS_PATH)

  unless File.exist?(full_path)
    say_status :skip, "#{APP_CSS_PATH} not found", :yellow
    return
  end

  if File.read(full_path).include?(TOKENS_MARKER)
    say_status :skip, "Shape/state tokens already present", :blue
    return
  end

  tokens = File.read(File.expand_path("templates/shape_state_tokens.css.tt", __dir__))
  append_to_file APP_CSS_PATH, "\n#{tokens}"

  say_status :append, "Added shape/state token block to #{APP_CSS_PATH}", :green
end

#add_theme_variablesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/generators/maquina_components/install/install_generator.rb', line 59

def add_theme_variables
  return if options[:skip_theme]

  css_path = "app/assets/tailwind/application.css"
  full_path = File.join(destination_root, css_path)

  unless File.exist?(full_path)
    say_status :skip, "#{css_path} not found", :yellow
    return
  end

  if File.read(full_path).include?("--color-primary:")
    say_status :skip, "Theme variables already present", :blue
    return
  end

  theme_content = File.read(File.expand_path("templates/theme.css.tt", __dir__))

  append_to_file css_path, "\n#{theme_content}"

  say_status :append, "Added theme variables to application.css", :green
end

#check_tailwindcss_railsObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/generators/maquina_components/install/install_generator.rb', line 23

def check_tailwindcss_rails
  css_path = "app/assets/tailwind/application.css"

  unless File.exist?(File.join(destination_root, css_path))
    say_status :warning, "tailwindcss-rails doesn't appear to be installed", :yellow
    say "Please install tailwindcss-rails first:"
    say "  bundle add tailwindcss-rails"
    say "  rails tailwindcss:install"
    say ""
  end
end

#create_helperObject



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

def create_helper
  return if options[:skip_helper]

  helper_path = "app/helpers/maquina_components_helper.rb"
  full_path = File.join(destination_root, helper_path)

  if File.exist?(full_path)
    say_status :skip, "#{helper_path} already exists", :blue
    return
  end

  template "maquina_components_helper.rb.tt", helper_path
  say_status :create, helper_path, :green
end

#show_post_install_messageObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/generators/maquina_components/install/install_generator.rb', line 121

def show_post_install_message
  say ""
  say "=" * 60
  say "  maquina_components installed successfully!", :green
  say "=" * 60
  say ""
  say "Next steps:"
  say ""
  say "1. Customize theme variables in app/assets/tailwind/application.css"
  say "   to match your design system. Shape, focus-ring, elevation and"
  say "   control-mark tokens are listed there too, commented out at their"
  say "   0.5.1 values - uncomment a line to pin the old look."
  say ""
  say "   Upgrading an existing app? Run `bin/rails maquina:doctor` for a"
  say "   report of app CSS the 0.6 token layer makes redundant or breaks."
  say ""
  say "2. Override the icon helper in app/helpers/maquina_components_helper.rb"
  say "   to use your own icon system (Heroicons, Lucide, etc.)."
  say ""
  say "3. Start using components in your views:"
  say ""
  say '   <%= render "components/card" do %>'
  say '     <%= render "components/card/header" do %>'
  say '       <%= render "components/card/title", text: "Hello" %>'
  say "     <% end %>"
  say "   <% end %>"
  say ""
  say "4. For form elements, use data attributes:"
  say ""
  say '   <%= f.text_field :email, data: { component: "input" } %>'
  say '   <%= f.submit "Save", data: { component: "button", variant: "primary" } %>'
  say ""
  say "Documentation: https://github.com/maquina-app/maquina_components"
  say ""
end