Class: RBUI::Generators::InstallGenerator

Inherits:
BaseGenerator show all
Defined in:
lib/generators/rbui/install/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



144
145
146
# File 'lib/generators/rbui/install/install_generator.rb', line 144

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

Instance Method Details

#add_phlex_railsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/generators/rbui/install/install_generator.rb', line 18

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 --version=\"~> 1.2.1\""
  end

  say "Adding tailwind_merge"
  run "bundle add tailwind_merge"

  say "run phlex install"
  run "bin/rails generate phlex:install"
end


148
149
150
# File 'lib/generators/rbui/install/install_generator.rb', line 148

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

#confirm_installationObject



12
13
14
15
16
# File 'lib/generators/rbui/install/install_generator.rb', line 12

def confirm_installation
  return if yes?("You need tailwindcss installed. Continue? (y/n)")
  say "Installation cancelled.", :red
  exit
end

#include_rbuiObject



133
134
135
136
137
138
139
140
141
# File 'lib/generators/rbui/install/install_generator.rb', line 133

def include_rbui
  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

#install_stuffObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/generators/rbui/install/install_generator.rb', line 34

def install_stuff
  # 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

    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

  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/, ""

    # Add the vendor-specific pin
    append_to_file "config/importmap.rb" do
      'pin "tailwindcss-animate", to: "tailwindcss-animate.js", preload: true' + "\n"
    end

  else
    say "Not using importmaps, adding tailwind-animate via yarn"
    run "yarn add tailwindcss-animate"
  end

  # 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, assigns: {using_importmap: using_importmap?}

  say "Add CSS variables"
  template "application.tailwind.css", "app/assets/stylesheets/application.tailwind.css", force: true
end

#pin_rbui_jsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/generators/rbui/install/install_generator.rb', line 92

def pin_rbui_js
  stimulus_path = Rails.root.join("app/javascript/application.js")
  package_name = "rbui-js"

  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 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"

    manifest_path = Rails.root.join("app/assets/config/manifest.js")
    if File.exist?(manifest_path)
      append_to_file manifest_path, "\n//= link rbui/index.js\n"
    end

    fix_import_maps!
  else
    say "Add rbui-js package"
    run "yarn add #{package_name}"

    append_to_file stimulus_path, "\nimport \"../components/rbui\";\n"

    run "yarn build"
  end
end

#revokeObject



152
153
154
# File 'lib/generators/rbui/install/install_generator.rb', line 152

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