-
Notifications
You must be signed in to change notification settings - Fork 16
/
template_bootstrap.rb
177 lines (142 loc) · 5.76 KB
/
template_bootstrap.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
91
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require 'fileutils'
require 'shellwords'
def add_template_repository_to_source_path
if __FILE__ =~ %r{\Ahttps?://}
require 'tmpdir'
source_paths.unshift(tempdir = Dir.mktmpdir('rails-'))
at_exit { FileUtils.remove_entry(tempdir) }
git clone: [
'--quiet',
'https://github.com/IsraelDCastro/rails-vite-tailwindcss-template.git',
tempdir
].map(&:shellescape).join(' ')
if (branch = __FILE__[%r{rails-vite-tailwindcss-template/(.+)/template_bootstrap.rb}, 1])
Dir.chdir(tempdir) { git checkout: branch }
end
else
source_paths.unshift(File.dirname(__FILE__))
end
end
def add_gems
gem 'vite_rails'
gem 'vite_ruby'
gem 'ruby-vips', '~> 2.1', '>= 2.1.4'
gem 'annotate', group: :development
gem 'devise'
gem 'name_of_person'
end
def add_hotwired_gem
gem 'stimulus-rails'
gem 'turbo-rails'
end
def set_application_name
# Add Application Name to Config
environment 'config.application_name = Rails.application.class.module_parent_name'
# Announce the user where they can change the application name in the future.
puts 'You can change application name inside: ./config/application.rb'
end
def add_vite
run 'bundle exec vite install'
end
def add_javascript
run 'yarn add bootstrap @popperjs/core sass vite'
run 'yarn add -D eslint prettier eslint-plugin-prettier eslint-config-prettier path vite-plugin-full-reload vite-plugin-ruby'
end
def add_javascript_vue
run 'yarn add bootstrap @popperjs/core sass vite vue'
run 'yarn add -D @vitejs/plugin-vue @vue/compiler-sfc eslint prettier eslint-plugin-prettier eslint-config-prettier eslint-plugin-vue path vite-plugin-full-reload vite-plugin-ruby'
end
def add_javascript_react
run 'yarn add bootstrap @popperjs/core sass vite react react-dom'
run 'yarn add -D @vitejs/plugin-react-refresh eslint prettier eslint-plugin-prettier eslint-config-prettier eslint-plugin-react path vite-plugin-full-reload vite-plugin-ruby'
end
def add_hotwired
run 'yarn add @hotwired/stimulus @hotwired/turbo-rails'
end
def copy_templates
copy_file 'Procfile.dev', force: true
copy_file 'jsconfig.json', force: true
copy_file '.eslintrc.json'
say 'Remove bootstrap directory from template.', :red
run "rm -rf #{app_name}/bootstrap"
directory 'lib-bootstrap', 'lib', force: true
run 'for file in lib/templates/**/**/*.txt; do mv "$file" "${file%.txt}.tt"; done'
say ' Custom scaffold templates copied', :green
end
def add_pages_controller
generate 'controller Pages home'
route "root to: 'pages#home'"
end
def run_command_flags
ARGV.each do |flag|
# process arguments like so
copy_file 'vite.config-react.ts', 'vite.config.ts' if flag == '--react'
directory 'bootstrap-react/app', 'app', force: true if flag == '--react'
copy_file '.eslintrc-react.json', '.eslintrc.json' if flag == '--react'
add_javascript_react if flag == '--react'
copy_file 'vite.config-vue.ts', 'vite.config.ts' if flag == '--vue'
directory 'bootstrap-vue/app', 'app', force: true if flag == '--vue'
copy_file '.eslintrc-vue.json', '.eslintrc.json' if flag == '--vue'
add_javascript_vue if flag == '--vue'
copy_file 'vite.config.ts' if flag == '--normal'
copy_file '.eslintrc.json' if flag == '--normal'
directory 'bootstrap/app', 'app', force: true if flag == '--normal'
add_javascript if flag == '--normal'
directory 'hotwired-generator', 'lib/generators' if flag == '--hotwired'
add_hotwired_gem if flag == '--hotwired'
add_hotwired if flag == '--hotwired'
if flag == '--hotwired'
inject_into_file('app/frontend/entrypoints/application.js', 'import { Turbo } from "@hotwired/turbo-rails";' "\n\n" 'window.Turbo = Turbo;' "\n\n", before: 'import "./main.scss";')
end
end
end
# Main setup
add_gems
after_bundle do
add_template_repository_to_source_path
set_application_name
add_pages_controller
copy_templates
run_command_flags
add_vite
rails_command 'db:create'
rails_command 'generate devise:install'
rails_command 'generate devise user'
rails_command 'generate migration AddNameFieldsToUser first_name last_name'
inject_into_file('app/models/user.rb', "\n\n" ' has_person_name', after: ':validatable')
inject_into_file('app/controllers/application_controller.rb', "\n\n" ' before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) do |u|
u.permit(:first_name, :last_name, :name, :email, :password)
end
devise_parameter_sanitizer.permit(:account_update) do |u|
u.permit(:first_name, :last_name, :name, :email, :password, :password_confirmation, :current_password)
end
end' "\n\n", after: 'class ApplicationController < ActionController::Base')
rails_command 'active_storage:install'
inject_into_file('config/application.rb', "\n\n" ' config.active_storage.variant_processor = :vips', after: 'config.load_defaults 7.0')
rails_command 'g annotate:install'
rails_command 'db:migrate'
begin
git add: '.'
git commit: %( -m 'Initial commit' )
rescue StandardError => e
puts e.message
end
say
ARGV.each do |flag|
say 'Rails 7 + Vue 3 + ViteJS + Bootstrap created!', :green if flag == '--vue'
say 'Rails 7 + ReactJS 18 + ViteJS + Bootstrap created!', :green if flag == '--react'
say 'Rails 7 + ViteJS + Bootstrap created!', :green if flag == '--normal'
say 'Hotwired + Stimulus were added successfully', :green if flag == '--hotwired'
end
say
say ' To get started with your new app:', :yellow
say " cd #{original_app_name}"
say ' # If bootstrap directory is inside your project, be sure'
say ' # To delete it, it does not affect your project.'
say ' # Please update config/database.yml with your database credentials'
say
say ' rails s'
end