|
1 | | -require "net/http" |
2 | | -require_relative "../base_generator" |
| 1 | +require "rails/generators" |
3 | 2 |
|
4 | | -# TODO: make ejctectec components work without the gem |
5 | 3 | module RubyUI |
6 | 4 | module Generators |
7 | | - class InstallGenerator < RubyUI::Generators::BaseGenerator |
| 5 | + class InstallGenerator < Rails::Generators::Base |
8 | 6 | namespace "ruby_ui:install" |
9 | 7 |
|
10 | | - if defined?(Rails::Generators::Base) |
11 | | - source_root File.expand_path("templates", __dir__) |
| 8 | + source_root File.expand_path("templates", __dir__) |
12 | 9 |
|
13 | | - def confirm_installation |
14 | | - return if yes?("You need tailwindcss installed. Continue? (y/n)") |
15 | | - say "Installation cancelled.", :red |
16 | | - exit |
17 | | - end |
| 10 | + def install_phlex_rails |
| 11 | + say "Checking for phlex-rails" |
18 | 12 |
|
19 | | - def add_phlex_rails |
20 | | - say "Checking for Phlex Rails" |
21 | | - if gem_installed?("phlex-rails") |
22 | | - say "Phlex Rails is already installed", :green |
| 13 | + if gem_installed?("phlex-rails") |
| 14 | + if Gem::Specification.find_by_name("phlex-rails").version < "2.0.0.beta2" |
| 15 | + say "You need to upgrade to phlex-rails 2 to use RubyUI", :red |
| 16 | + exit |
23 | 17 | else |
24 | | - say "Adding Phlex Rails" |
25 | | - run "bundle add phlex-rails --version=\"~> 2.0.0.beta2\"" |
| 18 | + say "phlex-rails is already installed", :green |
26 | 19 | end |
| 20 | + else |
| 21 | + say "Adding phlex-rails to Gemfile" |
| 22 | + run %(bundle add phlex-rails --github="phlex-ruby/phlex-rails") |
27 | 23 |
|
28 | | - say "Adding tailwind_merge" |
29 | | - run "bundle add tailwind_merge" |
30 | | - |
31 | | - say "run phlex install" |
| 24 | + say "Running phlex-rails structure" |
32 | 25 | run "bin/rails generate phlex:install" |
33 | 26 | end |
| 27 | + end |
34 | 28 |
|
35 | | - def install_stuff |
36 | | - # make default option no |
37 | | - # extend the yes func to have a default option y/(n) and also allow for enter to accedpt the default |
38 | | - |
39 | | - if ENV["TEST_DATA"] == "true" |
40 | | - say "Do you want to set up the dev test data?" |
41 | | - say "Add index controller" |
42 | | - run "bin/rails generate controller static index --no-helper --no-assets --no-test-framework --no-jbuilder" |
43 | | - |
44 | | - say "Add index view" |
45 | | - run "bin/rails g phlex:view Static::Index" |
46 | | - |
47 | | - append_to_file "app/controllers/static_controller.rb", after: " def index" do |
48 | | - # remove view because phlex is removing view |
49 | | - "\n render Static::IndexView" |
50 | | - end |
51 | | - |
52 | | - template "index_view.rb", "app/views/static/index_view.rb", force: true |
53 | | - |
54 | | - say "Add index route" |
55 | | - append_to_file "config/routes.rb", after: "Rails.application.routes.draw do" do |
56 | | - "\n root to: \"static#index\"\n" |
57 | | - end |
58 | | - end |
59 | | - |
60 | | - tailwind_config_path = Rails.root.join("config/tailwind.config.js") |
61 | | - if !File.exist?(tailwind_config_path) |
62 | | - say "Tailwind CSS is required for RubyUI", :red |
63 | | - end |
64 | | - |
65 | | - say "Add ruby_ui initializer" |
66 | | - template "base_store_initializer.rb", "config/initializers/ruby_ui.rb" |
67 | | - |
68 | | - if using_importmap? |
69 | | - say "Using importmaps, adding tailwind-animate" |
70 | | - run "bin/importmap pin tailwindcss-animate" |
71 | | - |
72 | | - # Remove the default pin |
73 | | - gsub_file "config/importmap.rb", /^pin "tailwindcss-animate".*$\n/, "" |
74 | | - |
75 | | - # Add the vendor-specific pin |
76 | | - append_to_file "config/importmap.rb" do |
77 | | - 'pin "tailwindcss-animate", to: "tailwindcss-animate.js", preload: true' + "\n" |
78 | | - end |
79 | | - |
80 | | - else |
81 | | - say "Not using importmaps, adding tailwind-animate via yarn" |
82 | | - run "yarn add tailwindcss-animate" |
83 | | - end |
84 | | - |
85 | | - # check if tailwind.config is in config dir or in root or ask to specify a path |
86 | | - say "update tailwind.config.js" |
87 | | - template "tailwind.config.js", "config/tailwind.config.js", force: true, assigns: {using_importmap: using_importmap?} |
| 29 | + def install_tailwind_merge |
| 30 | + say "Checking for tailwind_merge" |
88 | 31 |
|
89 | | - say "Add CSS variables" |
90 | | - template "application.tailwind.css", "app/assets/stylesheets/application.tailwind.css", force: true |
| 32 | + if gem_installed?("tailwind_merge") |
| 33 | + say "tailwind_merge is already installed", :green |
| 34 | + else |
| 35 | + say "Adding phlex-rails to Gemfile" |
| 36 | + run %(bundle add tailwind_merge) |
91 | 37 | end |
| 38 | + end |
92 | 39 |
|
93 | | - def pin_ruby_ui_js |
94 | | - stimulus_path = Rails.root.join("app/javascript/application.js") |
95 | | - package_name = "ruby_ui_js" |
96 | | - |
97 | | - say "Add RubyUI Stimulus controllers" |
98 | | - # run "mkdir -p app/javascript/controllers/ruby_ui_js" |
99 | | - template "index.js", "app/components/ruby_ui/index.js" |
100 | | - |
101 | | - if using_importmap? |
102 | | - gsub_file "app/components/ruby_ui/index.js", /^import { application }.*$/ do |
103 | | - 'import { application } from "controllers/application";' |
104 | | - end |
105 | | - |
106 | | - append_to_file Rails.root.join("config/initializers/assets.rb") do |
107 | | - "Rails.application.config.assets.paths << Rails.root.join(\"app/components\")\n" |
108 | | - end |
109 | | - |
110 | | - say "Pin #{package_name}" |
111 | | - append_to_file Rails.root.join("config/importmap.rb") do |
112 | | - "pin_all_from \"app/components/ruby_ui\", under: \"ruby_ui\"\n" |
113 | | - end |
114 | | - |
115 | | - run "bin/importmap pin #{package_name}" |
116 | | - append_to_file stimulus_path, "\nimport \"ruby_ui\";\n" |
117 | | - |
118 | | - manifest_path = Rails.root.join("app/assets/config/manifest.js") |
119 | | - if File.exist?(manifest_path) |
120 | | - append_to_file manifest_path, "\n//= link ruby_ui/index.js\n" |
121 | | - end |
122 | | - |
123 | | - fix_import_maps! |
124 | | - else |
125 | | - say "Add ruby_ui_js package" |
126 | | - run "yarn add #{package_name}" |
127 | | - |
128 | | - append_to_file stimulus_path, "\nimport \"../components/ruby_ui\";\n" |
129 | | - |
130 | | - run "yarn build" |
131 | | - end |
132 | | - end |
| 40 | + def install_ruby_ui_initializer |
| 41 | + say "Creating RubyUI initializer" |
| 42 | + template "ruby_ui.rb.erb", Rails.root.join("config/initializers/ruby_ui.rb") |
| 43 | + end |
133 | 44 |
|
134 | | - def include_ruby_ui |
135 | | - message = "Include RubyUI in your global component layout?\n This allows to call RubyUI.Button {\"button\"} / RubyUI::Button.new {\"button\"} with Button {\"button\"} (y/n)" |
136 | | - if yes?(message) |
137 | | - say "Add RubyUI to your global component layout" |
138 | | - insert_into_file "app/views/application_view.rb", after: "class ApplicationView < ApplicationComponent\n" do |
139 | | - " include RubyUI\n" |
140 | | - end |
141 | | - end |
142 | | - end |
| 45 | + def add_ruby_ui_module_to_components_base |
| 46 | + say "Adding RubyUI Kit to Components::Base" |
| 47 | + insert_into_file Rails.root.join("app/components/base.rb"), after: "include Components" do |
| 48 | + "\n include RubyUI" \ |
| 49 | + end |
| 50 | + end |
143 | 51 |
|
144 | | - else |
145 | | - def self.source_root |
146 | | - File.expand_path("templates", __dir__) |
147 | | - end |
| 52 | + def add_tailwind_css |
| 53 | + say "Adding RubyUI styles to application css" |
| 54 | + template "application.tailwind.css.erb", Rails.root.join("app/assets/stylesheets/application.tailwind.css") |
| 55 | + end |
148 | 56 |
|
149 | | - def add_stylesheet_link |
150 | | - puts "This generator can only be run in a Rails environment." |
151 | | - end |
| 57 | + def add_tailwind_config |
| 58 | + say "Adding RubyUI config to tailwind config" |
| 59 | + template "tailwind.config.js.erb", Rails.root.join("tailwind.config.js") |
| 60 | + end |
152 | 61 |
|
153 | | - def revoke |
154 | | - puts "This generator can only be run in a Rails environment." |
155 | | - end |
| 62 | + def add_ruby_ui_base |
| 63 | + say "Adding RubyUI::Base component" |
| 64 | + template "../../../../ruby_ui/base.rb", Rails.root.join("app/components/ruby_ui/base.rb") |
156 | 65 | end |
157 | 66 |
|
158 | 67 | private |
159 | 68 |
|
160 | | - def fix_import_maps! |
161 | | - importmap_config_path = Rails.root.join("config/importmap.rb") |
162 | | - |
163 | | - gsub_file importmap_config_path, /^pin "date-fns".*$/ do |
164 | | - 'pin "date-fns", to: "https://ga.jspm.io/npm:date-fns@3.3.1/index.mjs"' |
165 | | - end |
166 | | - run "bin/importmap pin @popperjs/core@2.11.8/+esm --from jsdelivr" |
167 | | - |
168 | | - run "mv vendor/javascript/@popperjs--core--+esm.js vendor/javascript/stupid-popper-lib-2024.js" |
169 | | - |
170 | | - append_to_file importmap_config_path do |
171 | | - 'pin "@popperjs/core", to: "stupid-popper-lib-2024.js"' |
172 | | - end |
173 | | - |
174 | | - uri = URI "https://ga.jspm.io/npm:chart.js@3.9.1/dist/chart.min.js" |
175 | | - request = Net::HTTP::Get.new uri |
176 | | - |
177 | | - response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| |
178 | | - http.request(request) |
179 | | - } |
180 | | - |
181 | | - File.write(Rails.root.join("vendor/javascript/chart.js--auto.js"), response.body) if response.is_a?(Net::HTTPSuccess) |
182 | | - |
183 | | - append_to_file Rails.root.join("app/views/layouts/application.html.erb"), before: "</body>" do |
184 | | - "<script> |
185 | | - window.process = { env: { NODE_ENV: 'production'} } |
186 | | - </script>" |
187 | | - end |
188 | | - end |
189 | | - |
190 | 69 | def gem_installed?(name) |
191 | 70 | Gem::Specification.find_all_by_name(name).any? |
192 | 71 | end |
|
0 commit comments