-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
988fa8f
commit a286edc
Showing
8 changed files
with
181 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
require "net/http" | ||
|
||
# TODO: make ejctectec components work without the gem | ||
module RBUI | ||
module Generators | ||
class InstallGenerator < defined?(Rails::Generators::Base) ? Rails::Generators::Base : Object | ||
class InstallGenerator < RBUI::Generators::BaseGenerator | ||
namespace "rbui:install" | ||
|
||
if defined?(Rails::Generators::Base) | ||
source_root File.expand_path("templates", __dir__) | ||
|
||
def confirm_installation | ||
return if yes?("You need tailwindcss installed. Continue? (y/n)") | ||
say "Installation cancelled.", :red | ||
exit | ||
end | ||
|
||
def add_phlex_rails | ||
say "Checking for Phlex Rails" | ||
if gem_installed?("phlex-rails") | ||
|
@@ -15,21 +24,24 @@ def add_phlex_rails | |
run "bundle add phlex-rails" | ||
end | ||
|
||
if yes?("Do you want to run the Phlex installer? (y/n)") | ||
say "Run Phlex install" | ||
run "bin/rails generate phlex:install" | ||
end | ||
say "run phlex install" | ||
run "bin/rails generate phlex:install" | ||
end | ||
|
||
def install_stuff | ||
if yes?("Do you want to set up the dev test data? (y/n)") | ||
# make default option no | ||
# extend the yes func to have a default option y/(n) and also allow for enter to accedpt the default | ||
|
||
if ENV["TEST_DATA"] == "true" | ||
say "Do you want to set up the dev test data?" | ||
say "Add index controller" | ||
run "bin/rails generate controller static index --no-helper --no-assets --no-test-framework --no-jbuilder" | ||
|
||
say "Add index view" | ||
run "bin/rails g phlex:view Static::Index" | ||
|
||
append_to_file "app/controllers/static_controller.rb", after: " def index" do | ||
# remove view because phlex is removing view | ||
"\n render Static::IndexView" | ||
end | ||
|
||
|
@@ -41,65 +53,82 @@ def install_stuff | |
end | ||
end | ||
|
||
say "Checking for Tailwind CSS" | ||
if gem_installed?("tailwindcss-rails") | ||
say "Tailwind CSS is already installed", :green | ||
tailwind_config_path = Rails.root.join("config/tailwind.config.js") | ||
if !File.exist?(tailwind_config_path) | ||
say "Tailwind CSS is required for RBUI", :red | ||
end | ||
|
||
say "Add rbui initializer" | ||
template "base_store_initializer.rb", "config/initializers/rbui.rb" | ||
|
||
if using_importmap? | ||
say "Using importmaps, adding tailwind-animate" | ||
run "bin/importmap pin tailwindcss-animate" | ||
|
||
# Remove the default pin | ||
gsub_file "config/importmap.rb", /^pin "tailwindcss-animate".*$\n/, "" | ||
|
||
if yes?("Do you want to run the Tailwind installer? (y/n)") | ||
say "Run Tailwind install" | ||
run "./bin/rails tailwindcss:install" | ||
# Add the vendor-specific pin | ||
append_to_file "config/importmap.rb" do | ||
'pin "tailwindcss-animate", to: "tailwindcss-animate.js", preload: true' + "\n" | ||
end | ||
elsif yes?("Do you want us to install Tailwind CSS? (y/n)") | ||
say "Adding Tailwind CSS" | ||
run "./bin/bundle add tailwindcss-rails" | ||
|
||
say "Run Tailwind install" | ||
run "./bin/rails tailwindcss:install" | ||
else | ||
say "Not using importmaps, adding tailwind-animate via yarn" | ||
run "yarn add tailwindcss-animate" | ||
end | ||
|
||
say "Add tailwind animate" | ||
run "yarn add tailwindcss-animate" | ||
|
||
# check if tailwind.config is in config dir or in root or ask to specify a path | ||
say "update tailwind.config.js" | ||
template "tailwind.config.js", "config/tailwind.config.js", force: true | ||
template "tailwind.config.js", "config/tailwind.config.js", force: true, assigns: {using_importmap: using_importmap?} | ||
|
||
say "Add CSS variables" | ||
template "application.tailwind.css", "app/assets/stylesheets/application.tailwind.css", force: true | ||
end | ||
|
||
def pin_rbui_js | ||
importmap_binstub = Rails.root.join("bin/importmap") | ||
importmap_config_path = Rails.root.join("config/importmap.rb") | ||
stimulus_path = Rails.root.join("app/javascript/application.js") | ||
package_name = "rbui-js" | ||
|
||
if importmap_binstub.exist? | ||
say "Add RBUI Stimulus controllers" | ||
# run "mkdir -p app/javascript/controllers/rbui-js" | ||
template "index.js", "app/components/rbui/index.js" | ||
|
||
if using_importmap? | ||
gsub_file "app/components/rbui/index.js", /^import { application }.*$/ do | ||
'import { application } from "controllers/application";' | ||
end | ||
|
||
append_to_file Rails.root.join("config/initializers/assets.rb") do | ||
"Rails.application.config.assets.paths << Rails.root.join(\"app/components\")\n" | ||
end | ||
|
||
say "Pin #{package_name}" | ||
append_to_file importmap_config_path do | ||
# %(pin "rbui-js", to: "rbui-js.js"\n) | ||
%(pin #{package_name}, to: "rbui-js.js"\n) | ||
append_to_file Rails.root.join("config/importmap.rb") do | ||
"pin_all_from \"app/components/rbui\", under: \"rbui\"\n" | ||
end | ||
|
||
run "bin/importmap pin #{package_name}" | ||
append_to_file stimulus_path, "\nimport \"rbui\";\n" | ||
|
||
fix_import_maps! | ||
else | ||
say "Add rbui-js package" | ||
run "yarn add #{package_name}" | ||
end | ||
|
||
if stimulus_path.exist? | ||
say "Add RBUI Stimulus controllers" | ||
template "#{template_dir}/index.js", "#{destination_path}/index.js" unless File.exist?("#{destination_path}/index.js") | ||
append_to_file stimulus_path, "\nimport \"../components/rbui\";\n" | ||
|
||
run "yarn build" | ||
else | ||
say "Default Stimulus location is missing: app/javascript/controllers/index.js", :red | ||
say " Add to your Stimulus index.js:" | ||
say " import \"#{package_name}\"" | ||
end | ||
end | ||
|
||
def include_rbui | ||
say "Add RBUI to your global component layout" | ||
insert_into_file "app/views/application_view.rb", after: "class ApplicationView < ApplicationComponent\n" do | ||
" include RBUI\n" | ||
message = "Include RBUI in your global component layout?\n This allows to call RBUI.Button {\"button\"} / RBUI::Button.new {\"button\"} with Button {\"button\"} (y/n)" | ||
if yes?(message) | ||
say "Add RBUI to your global component layout" | ||
insert_into_file "app/views/application_view.rb", after: "class ApplicationView < ApplicationComponent\n" do | ||
" include RBUI\n" | ||
end | ||
end | ||
end | ||
|
||
|
@@ -119,6 +148,36 @@ def revoke | |
|
||
private | ||
|
||
def fix_import_maps! | ||
importmap_config_path = Rails.root.join("config/importmap.rb") | ||
|
||
gsub_file importmap_config_path, /^pin "date-fns".*$/ do | ||
'pin "date-fns", to: "https://ga.jspm.io/npm:[email protected]/index.mjs"' | ||
end | ||
run "bin/importmap pin @popperjs/[email protected]/+esm --from jsdelivr" | ||
|
||
run "mv vendor/javascript/@popperjs--core--+esm.js vendor/javascript/stupid-popper-lib-2024.js" | ||
|
||
append_to_file importmap_config_path do | ||
'pin "@popperjs/core", to: "stupid-popper-lib-2024.js"' | ||
end | ||
|
||
uri = URI "https://ga.jspm.io/npm:[email protected]/dist/chart.min.js" | ||
request = Net::HTTP::Get.new uri | ||
|
||
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| | ||
http.request(request) | ||
} | ||
|
||
File.write(Rails.root.join("vendor/javascript/chart.js--auto.js"), response.body) if response.is_a?(Net::HTTPSuccess) | ||
|
||
append_to_file Rails.root.join("app/views/layouts/application.html.erb"), before: "</body>" do | ||
"<script> | ||
window.process = { env: { NODE_ENV: 'production'} } | ||
</script>" | ||
end | ||
end | ||
|
||
def gem_installed?(name) | ||
Gem::Specification.find_all_by_name(name).any? | ||
end | ||
|
6 changes: 3 additions & 3 deletions
6
...ui/templates/base_store_initializer.rb.tt → ...ll/templates/base_store_initializer.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
RBUI.setup do |config| | ||
# RBUI.setup do |config| | ||
# Setting a namespace allows you to access RBUI components through this namespace. | ||
# For example, with namespace set to "UI", you can use: | ||
# UI::Button.new instead of RBUI::Button.new | ||
# UI::Card.new instead of RBUI::Card.new | ||
# This can help avoid naming conflicts and allows for cleaner, more concise code. | ||
# If you prefer to use RBUI components directly, you can leave this unset. | ||
config.namespace = "UI" | ||
end | ||
# config.namespace = "UI" | ||
# end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters