Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add install generator #96

Merged
merged 7 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
node_modules
/app/assets/builds/*
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2024 PhlexUI
Copyright (c) 2024 RBUI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
15 changes: 15 additions & 0 deletions lib/generators/rbui/base_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "rails/generators"

module RBUI
module Generators
class BaseGenerator < defined?(Rails::Generators::Base) ? Rails::Generators::Base : Object
namespace "rbui:base"

source_root File.join(__dir__, "templates")

def copy_templates
template "base_store_initializer.rb", "config/initializers/rbui.rb"
end
end
end
end
128 changes: 128 additions & 0 deletions lib/generators/rbui/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
module RBUI
module Generators
class InstallGenerator < defined?(Rails::Generators::Base) ? Rails::Generators::Base : Object
namespace "rbui:install"

if defined?(Rails::Generators::Base)
source_root File.expand_path("templates", __dir__)

def add_phlex_rails
say "Checking for Phlex Rails"
if gem_installed?("phlex-rails")
say "Phlex Rails is already installed", :green
else
say "Adding 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
end

def install_stuff
if yes?("Do you want to set up the dev test data? (y/n)")
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
"\n render Static::IndexView"
end

template "index_view.rb", "app/views/static/index_view.rb", force: true

say "Add index route"
append_to_file "config/routes.rb", after: "Rails.application.routes.draw do" do
"\n root to: \"static#index\"\n"
end
end

say "Checking for Tailwind CSS"
if gem_installed?("tailwindcss-rails")
say "Tailwind CSS is already installed", :green

if yes?("Do you want to run the Tailwind installer? (y/n)")
say "Run Tailwind install"
run "./bin/rails tailwindcss:install"
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"
end

say "Add tailwind animate"
run "yarn add tailwindcss-animate"

say "update tailwind.config.js"
template "tailwind.config.js", "config/tailwind.config.js", force: true

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 "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)
end
else
say "Add rbui-js package"
run "yarn add #{package_name}"
end

if stimulus_path.exist?
say "Add RBUI Stimulus controllers"
append_to_file stimulus_path do
"\nimport \"#{package_name}\";\n"
end
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"
end
end

else
def self.source_root
File.expand_path("templates", __dir__)
end

def add_stylesheet_link
puts "This generator can only be run in a Rails environment."
end

def revoke
puts "This generator can only be run in a Rails environment."
end
end

private

def gem_installed?(name)
Gem::Specification.find_all_by_name(name).any?
end
end
end
end
Empty file.
76 changes: 76 additions & 0 deletions lib/generators/rbui/install/templates/application.tailwind.css.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
@tailwind base;
SethHorsley marked this conversation as resolved.
Show resolved Hide resolved
@tailwind components;
@tailwind utilities;

@layer base {
:root {
--background: 0 0% 100%;
--foreground: 240 10% 3.9%;
--card: 0 0% 100%;
--card-foreground: 240 10% 3.9%;
--popover: 0 0% 100%;
--popover-foreground: 240 10% 3.9%;
--primary: 240 5.9% 10%;
--primary-foreground: 0 0% 98%;
--secondary: 240 4.8% 95.9%;
--secondary-foreground: 240 5.9% 10%;
--muted: 240 4.8% 95.9%;
--muted-foreground: 240 3.8% 46.1%;
--accent: 240 4.8% 95.9%;
--accent-foreground: 240 5.9% 10%;
--destructive: 0 84.2% 60.2%;
--destructive-foreground: 0 0% 98%;
--border: 240 5.9% 90%;
--input: 240 5.9% 90%;
--ring: 240 5.9% 10%;
--radius: 0.5rem;

/* rbui especific */
--warning: 38 92% 50%;
--warning-foreground: 0 0% 100%;
--success: 87 100% 37%;
--success-foreground: 0 0% 100%;
}

.dark {
--background: 240 10% 3.9%;
--foreground: 0 0% 98%;
--card: 240 10% 3.9%;
--card-foreground: 0 0% 98%;
--popover: 240 10% 3.9%;
--popover-foreground: 0 0% 98%;
--primary: 0 0% 98%;
--primary-foreground: 240 5.9% 10%;
--secondary: 240 3.7% 15.9%;
--secondary-foreground: 0 0% 98%;
--muted: 240 3.7% 15.9%;
--muted-foreground: 240 5% 64.9%;
--accent: 240 3.7% 15.9%;
--accent-foreground: 0 0% 98%;
--destructive: 0 62.8% 30.6%;
--destructive-foreground: 0 0% 98%;
--border: 240 3.7% 15.9%;
--input: 240 3.7% 15.9%;
--ring: 240 4.9% 83.9%;

/* rbui especific */
--warning: 38 92% 50%;
--warning-foreground: 0 0% 100%;
--success: 84 81% 44%;
--success-foreground: 0 0% 100%;
}
}

@layer base {
* {
@apply border-border;
}
body {
@apply bg-background text-foreground;
font-feature-settings: "rlig" 1, "calt" 1;

/* docs specific */
/* https://css-tricks.com/snippets/css/system-font-stack/ */
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
}
29 changes: 29 additions & 0 deletions lib/generators/rbui/install/templates/index_view.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

class Static::IndexView < ApplicationView
def template
render RBUI::Button.new { "click me" }
br {}

# UI.Button { "click me" }

AlertDialog do
AlertDialogTrigger do
Button { "Show dialog" }
end
AlertDialogContent do
AlertDialogHeader do
AlertDialogTitle { "Are you absolutely sure?" }
AlertDialogDescription { "This action cannot be undone. This will permanently delete your account and remove your data from our servers." }
end
AlertDialogFooter do
AlertDialogCancel { "Cancel" }
AlertDialogAction { "Continue" } # Will probably be a link to a controller action (e.g. delete account)
end
end
end
h1 { "Static::Index" }
p { "Find me in app/views/static/index_view.rb" }
end
end

85 changes: 85 additions & 0 deletions lib/generators/rbui/install/templates/tailwind.config.js.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// For importing tailwind styles from rbui gem
const execSync = require('child_process').execSync;
SethHorsley marked this conversation as resolved.
Show resolved Hide resolved

// Import rbui gem path
const outputRBUI = execSync('bundle show rbui', { encoding: 'utf-8' });
const rbui_path = outputRBUI.trim() + '/**/*.rb';

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
darkMode: ["class"],
content: [
'./app/views/**/*.{erb,haml,html,slim,rb}',
'./app/helpers/**/*.rb',
'./app/assets/stylesheets/**/*.css',
'./app/javascript/**/*.js',
rbui_path
],
theme: {
container: {
center: true,
padding: "2rem",
screens: {
"2xl": "1400px",
},
},
extend: {
colors: {
border: "hsl(var(--border))",
input: "hsl(var(--input))",
ring: "hsl(var(--ring))",
background: "hsl(var(--background))",
foreground: "hsl(var(--foreground))",
primary: {
DEFAULT: "hsl(var(--primary))",
foreground: "hsl(var(--primary-foreground))",
},
secondary: {
DEFAULT: "hsl(var(--secondary))",
foreground: "hsl(var(--secondary-foreground))",
},
destructive: {
DEFAULT: "hsl(var(--destructive))",
foreground: "hsl(var(--destructive-foreground))",
},
muted: {
DEFAULT: "hsl(var(--muted))",
foreground: "hsl(var(--muted-foreground))",
},
accent: {
DEFAULT: "hsl(var(--accent))",
foreground: "hsl(var(--accent-foreground))",
},
popover: {
DEFAULT: "hsl(var(--popover))",
foreground: "hsl(var(--popover-foreground))",
},
card: {
DEFAULT: "hsl(var(--card))",
foreground: "hsl(var(--card-foreground))",
},
/* rbui especific */
warning: {
DEFAULT: "hsl(var(--warning))",
foreground: "hsl(var(--warning-foreground))",
},
success: {
DEFAULT: "hsl(var(--success))",
foreground: "hsl(var(--success-foreground))",
},
},
borderRadius: {
lg: `var(--radius)`,
md: `calc(var(--radius) - 2px)`,
sm: "calc(var(--radius) - 4px)",
},
fontFamily: {
sans: ["var(--font-sans)", ...defaultTheme.fontFamily.sans],
},
},
},
plugins: [
require("tailwindcss-animate"),
],
}
9 changes: 9 additions & 0 deletions lib/generators/rbui/templates/base_store_initializer.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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
5 changes: 5 additions & 0 deletions lib/rbui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ def self.create_namespace_module

# Manually require all the files
Dir.glob(File.join(__dir__, "rbui", "**", "*.rb")).sort.each { |file| require file }

# If you need to require generators (assuming they're needed)
if defined?(Rails::Generators)
require_relative "generators/rbui/install/install_generator"
end
17 changes: 17 additions & 0 deletions lib/rbui/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module RBUI
if defined?(Rails)
class Railtie < ::Rails::Railtie
generators do
require_relative "../generators/rbui/install/install_generator"

config.app_generators do |g|
g.templates.unshift File.expand_path("../templates", __FILE__)
end

initializer "rbui.set_generator_namespace" do
Rails::Generators.namespace(RBUI::Generators, as: "rbui")
end
end
end
end
end
1 change: 1 addition & 0 deletions rbui.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Gem::Specification.new do |s|
s.authors = ["George Kettle"]
s.email = "[email protected]"
s.files = Dir["lib/**/*.rb", "tasks/**/*.rake"]
s.require_path = "lib"
s.homepage =
"https://rubygems.org/gems/rbui"
s.license = "MIT"
Expand Down
Loading