-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.rb
More file actions
287 lines (235 loc) · 9.73 KB
/
template.rb
File metadata and controls
287 lines (235 loc) · 9.73 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
def source_paths
super + [File.expand_path(File.dirname(__FILE__) + "/templates")]
end
app_name = Pathname.new(`pwd`).split.last.to_s.strip
if yes?("Customize defaults?")
solid_trifecta = yes?("Setup the Solid Trifecta in development")
mission_control = yes?("Setup Mission Control Jobs")
rails_panel = yes?("Setup Rails Panel")
auth = yes?("Setup Rails 8 authentication")
tailwind = yes?("Use Tailwind application layout?")
flowbite = yes?("Setup Flowbite?") if tailwind
csszero = yes?("Setup css zero?") unless tailwind
admin = yes?("Setup admin dashboard?")
homepage = yes?("Setup site_pages controller and homepage?")
mailcatcher = yes?("Setup mailcatcher")
# else
# solid_trifecta = true
# mission_control = true
# rails_panel = true
# auth = true
# tailwind = true
# admin = true
# homepage = true
# mailcatcher = true
# flowbite = true
end
# Ignore MacOS files
append_file ".gitignore" do
<<-EOS
.DS_Store
EOS
end
if mission_control
gem "mission_control-jobs"
end
if rails_panel
inject_into_file "Gemfile", after: "group :development do\n" do
<<-EOS
gem "meta_request"
EOS
end
end
run "bundle install"
git :init
git add: ".", commit: %(-m "Setup Rails #{Rails::VERSION::STRING} app with Rails Quickstart")
after_bundle do
# Setup database file
db_file = File.read("config/database.yml")
if db_file.include?('postgresql')
db = "postgresql"
elsif db_file.include?('sqlite3')
db = "sqlite3"
end
if ! db.nil?
template "database_#{db}.yml.tt", "config/database.yml", force: true
end
rails_command "db:create"
rails_command "db:migrate"
git add: ".", commit: %(-m "Setup #{db.nil? ? 'database' : db}")
# Generate authentication with a seed user and test helper
if auth
generate "authentication"
gsub_file "test/fixtures/users.yml", "one:", "alice:"
gsub_file "test/fixtures/users.yml", "two:", "bob:"
append_file "db/seeds.rb", %(User.create_with(password: "Test123", password_confirmation: "Test123").find_or_create_by(email_address: "user@host.com"))
template "session_helper.rb", "test/helpers/session_helper.rb"
template "passwords_controller_test.rb", "test/controllers/passwords_controller_test.rb"
template "sessions_controller_test.rb", "test/controllers/sessions_controller_test.rb"
# Over-ride password reset token expiration time to somethign sane
inject_into_file "app/models/user.rb", before: /^end$/ do
<<-EOS
generates_token_for :password_reset, expires_in: 4.hours do
# Last 10 characters of password salt, which changes when password is updated:
password_salt&.last(10)
end
EOS
end
rails_command "db:migrate"
rails_command "db:seed"
git add: ".", commit: %(-m "Generate Rails 8 authentication with a login seed and tests")
end
# Generate a home page
if homepage
generate "controller", "site_pages", "index", "--no-helper", "--no-assets"
gsub_file "app/controllers/site_pages_controller.rb", "ApplicationController\n", %(ApplicationController\n allow_unauthenticated_access\n)
gsub_file "config/routes.rb", %(get "site_pages/index"), %(root to: "site_pages#index")
template "site_pages_controller_test.rb", "test/controllers/site_pages_controller_test.rb", force: true
git add: ".", commit: %(-m "Generate a home page and root route with tests")
end
# Generate an admin dashboard
if admin
generate "controller", "admin/dashboard", "index"
gsub_file "config/routes.rb", %(get "dashboard/index"), %(root to: "dashboard#index")
# gsub_file "app/controllers/admin/dashboard_controller.rb", "ApplicationController", %(Admin::BaseController)
# template "admin_base_controller.rb", "app/controllers/admin/base_controller.rb"
template "dashboard_controller_test.rb", "test/controllers/admin/dashboard_controller_test.rb", force: true
git add: ".", commit: %(-m "Generate an admin dashboard with tests")
end
if solid_trifecta
# Configure Solid Queue in development
environment(nil, env: "development") do
<<-EOS
# Replace the default in-process and non-durable queuing backend for Active Job.
config.active_job.queue_adapter = :solid_queue
config.solid_queue.connects_to = { database: { writing: :queue } }
EOS
end
if File.file?("Procfile.dev")
append_file "Procfile.dev", "jobs: bin/jobs\n"
gsub_file "Procfile.dev", "web: bin/rails server", "web: bin/rails server --binding=0.0.0.0"
else
copy_file "Procfile.dev", "Procfile.dev"
append_file "Procfile.dev", "jobs: bin/jobs\n"
end
git add: ".", commit: %(-m "Setup Solid Queue in development")
# Configure Solid Cache in development
gsub_file "config/cache.yml", "development:\n", %(development:\n database: cache\n)
gsub_file "config/environments/development.rb", ":memory_store", ":solid_cache_store"
git add: ".", commit: %(-m "Setup Solid Cache in development")
# Configure Solid Cable in development
# inject_into_file "config/cable.yml", after: "adapter: async\n" do
# <<-EOS
# connects_to:
# database:
# writing: cable
# polling_interval: 0.1.seconds
# message_retention: 1.day
# EOS
# end
# These next two gsubs are a hack to fix a problem with the above inject_into_files commented out lines
gsub_file "config/cable.yml", "adapter: async\n" do
<<-EOS
adapter: solid_cable
connects_to:
database:
writing: cable
polling_interval: 0.1.seconds
message_retention: 1.day
EOS
end
gsub_file "config/cable.yml", /development:\n[\s\S]*?\n/, "development:\n"
git add: ".", commit: %(-m "Setup Solid Cable in development")
end
# Configure Mission Control
if mission_control
environment(nil, env: "development") do
<<-EOS
# Configure Mission Control
config.mission_control.jobs.http_basic_auth_enabled = false
config.mission_control.jobs.base_controller_class = "ApplicationController"
EOS
end
route %(mount MissionControl::Jobs::Engine, at: "/admin/jobs", as: :admin_jobs)
git add: ".", commit: %(-m "Setup Mission Control")
end
# Configure mailcatcher
if mailcatcher
environment(nil, env: "development") do
<<-EOS
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => '127.0.0.1', :port => 1025 }
EOS
end
git add: ".", commit: %(-m "Configure mailcatcher")
end
# Style the application layout
if tailwind
copy_file "_flash.html.erb", "app/views/layouts/_flash.html.erb"
copy_file "_form_errors.html.erb", "app/views/layouts/_form_errors.html.erb"
gsub_file "app/views/sessions/new.html.erb", /\s*<% if alert = flash[\s\S]*?<% end %>/, ""
gsub_file "app/views/sessions/new.html.erb", /\s*<% if notice = flash[\s\S]*?<% end %>\n/, ""
gsub_file "app/views/layouts/application.html.erb", /\s*<body>[\s\S]*?<\/body>\s*/i do
<<-EOS
<body class="min-h-screen bg-white font-sans flex flex-col">
<header class="bg-white shadow">
<div class="max-w-7xl mx-auto py-4 px-4 sm:px-6 lg:px-8">
<div class="flex justify-between items-center">
<h1 class="text-3xl font-bold text-gray-900"><%= link_to #{app_name.capitalize}, root_path %></h1>
<% if authenticated? %>
<%= button_to "Logout", session_path, method: :delete, class: "px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 transition-colors" %>
<% else %>
<%= link_to "Login", new_session_path, class: "px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 transition-colors" %>
<% end %>
</div>
</div>
</header>
<!-- Horizontal Menu Bar -->
<nav class="bg-gray-100 border-b border-gray-200">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex space-x-8 overflow-x-auto py-3">
<!-- Add your menu items here -->
<%= link_to "Home", root_path, class: "text-gray-900 hover:text-blue-600 font-medium" %>
<%= link_to "Admin", admin_root_path, class: "text-gray-500 hover:text-blue-600 font-medium" %>
<%= link_to "Jobs", admin_jobs_path, class: "text-gray-500 hover:text-blue-600 font-medium" %>
<!-- You can add more links as needed -->
</div>
</div>
</nav>
<main class="flex-grow">
<div class="max-w-7xl mx-auto py-4 px-4 sm:px-6 lg:px-8">
<%= render 'layouts/flash' %>
<%= yield %>
</div>
</main>
<footer class="bg-gray-50">
<div class="max-w-7xl mx-auto py-4 px-4 sm:px-6 lg:px-8">
<p class="text-center text-gray-500 text-sm">
© <%= Time.current.year %> #{app_name.capitalize}. All rights reserved.
</p>
</div>
</footer>
</body>
EOS
end
git add: ".", commit: %(-m "Style the application layout a bit better with Tailwind")
end
# Configure Flowbite
if flowbite
inject_into_file "config/importmap.rb", before: "pin_all_from" do
%(pin "flowbite", to: "https://cdn.jsdelivr.net/npm/flowbite@3.1.2/dist/flowbite.turbo.min.js"\n)
end
append_file "app/javascript/application.js" do
%(import "flowbite")
end
inject_into_file "app/views/layouts/application.html.erb", after: "<%# Includes all stylesheet files in app/assets/stylesheets %>\n" do
%( <%= stylesheet_link_tag "https://cdn.jsdelivr.net/npm/flowbite@3.1.2/dist/flowbite.min.css", "data-turbo-track": "reload" %>\n)
end
git add: ".", commit: %(-m "Setup Flowbite")
end
if csszero
run "bundle add css-zero"
generate "css_zero:install"
git add: ".", commit: %(-m "Setup CSS Zero")
end
end