This repository was archived by the owner on May 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgnarly.rb
338 lines (283 loc) · 8.44 KB
/
gnarly.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# Add the current directory to the path Thor uses
# to look up files
def source_paths
Array(super) +
[__dir__]
end
def create_gnarly_rails_app
# This is a really unfortunate, but necessary, line of code that resets the
# cached Gemfile location so the generated application's Gemfile is used
# instead of the generators Gemfile.
ENV["BUNDLE_GEMFILE"] = nil
# Prevent spring cache when generating application
ENV["DISABLE_SPRING"] = "true"
add_gems
run_bundle
after_bundle do
setup_testing
setup_binstubs
setup_database
setup_assets
setup_gitignore
setup_linting
setup_pronto
setup_simplecov
setup_environments
setup_readme
remove_dir "test"
git :init
format_ruby
completion_notification
end
end
def add_gems
gem_group :development, :test do
gem "axe-core-capybara"
gem "axe-core-rspec"
gem "bullet"
gem "dotenv-rails"
gem "factory_bot_rails"
gem "gnar-style", require: false
gem "launchy"
gem "lol_dba"
gem "okcomputer"
gem "pronto"
gem "pronto-rubocop", require: false
gem "pry-byebug"
gem "pry-rails"
gem "rspec-its"
gem "rspec-rails", "~> 4"
gem "rubocop-rspec", require: false
gem "shoulda-matchers"
gem "simplecov", require: false
end
end
def setup_database
remove_file "config/database.yml"
copy_file "templates/database.yml", "config/database.yml"
gsub_file "config/database.yml", "__application_name__", app_name
gsub_file "Gemfile", /.*sqlite.*\n/, ""
end
def setup_assets
run "yarn add esbuild-rails"
remove_file "esbuild.config.js"
copy_file "templates/esbuild.config.js", "esbuild.config.js"
run "npm set-script build 'node esbuild.config.js'"
end
def setup_gitignore
append_to_file ".gitignore" do
<<~GITIGNORE
# Ignore Byebug command history file.
.byebug_history
# Ignore output of simplecov
coverage
GITIGNORE
end
end
def setup_testing
setup_rspec
setup_factory_bot
setup_system_tests
setup_shoulda
setup_bullet
limit_test_logging
end
def setup_rspec
generate "rspec:install"
remove_file ".rspec"
copy_file "templates/.rspec", ".rspec"
gsub_file "spec/rails_helper.rb",
/# Dir\[Rails\.root\.join.*/,
"Dir[Rails.root.join(\"spec/support/**/*.rb\")].each { |f| require f }"
end
def setup_factory_bot
copy_file "templates/spec/support/factory_bot.rb", "spec/support/factory_bot.rb"
end
def setup_system_tests
copy_file "templates/spec/support/system_test_configuration.rb",
"spec/support/system_test_configuration.rb"
insert_into_file "spec/rails_helper.rb", after: "Rails is not loaded until this point!\n" do
system_tests_rails_helper_text
end
append_to_file "spec/rails_helper.rb" do
"\nCapybara.default_max_wait_time = 3\n"
end
end
def system_tests_rails_helper_text
<<~SYSTEM_TESTS
require "capybara/rails"
require "axe-rspec"
require "axe-capybara"
require "selenium/webdriver"
SYSTEM_TESTS
end
def setup_shoulda
append_to_file "spec/rails_helper.rb" do
shoulda_rails_helper_text
end
end
def shoulda_rails_helper_text
<<~SHOULDA
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
SHOULDA
end
def setup_bullet
insert_into_file "config/environments/test.rb", after: "Rails.application.configure do" do
bullet_test_env_text
end
insert_into_file "spec/rails_helper.rb", after: "RSpec.configure do |config|" do
bullet_rails_helper_text
end
end
def bullet_test_env_text
<<-BULLET_TEST
# Bullet gem initialization
config.after_initialize do
Bullet.enable = true
Bullet.bullet_logger = true
Bullet.raise = true
end
BULLET_TEST
end
def bullet_rails_helper_text
<<-BULLET_RAILS
if Bullet.enable?
config.before(:each) do
Bullet.start_request
end
config.after(:each) do
Bullet.perform_out_of_channel_notifications if Bullet.notification?
Bullet.end_request
end
end
BULLET_RAILS
end
def limit_test_logging
insert_into_file "config/environments/test.rb", after: "Rails.application.configure do" do
<<-TEST_LOGGING
unless ENV[\"RAILS_ENABLE_TEST_LOG\"]
config.logger = Logger.new(nil)
config.log_level = :fatal
end
TEST_LOGGING
end
end
def setup_binstubs
remove_file "bin/setup"
copy_file "templates/bin/setup", "bin/setup"
run "chmod +x bin/setup"
copy_file "templates/bin/rspec", "bin/rspec"
run "chmod +x bin/rspec"
copy_file "templates/bin/rubocop", "bin/rubocop"
run "chmod +x bin/rubocop"
end
def setup_linting
copy_file "templates/.rubocop.yml", ".rubocop.yml"
end
def setup_pronto
copy_file "templates/.pronto.yml", ".pronto.yml"
copy_file "templates/bin/pronto", "bin/pronto"
run "chmod +x bin/pronto"
copy_file ".github/workflows/pronto.yml", ".github/workflows/pronto.yml"
end
def setup_simplecov
prepend_to_file "spec/spec_helper.rb" do
<<~SIMPLECOV
require "simplecov"
# Save to CircleCI's artifacts directory if we're on CircleCI
if ENV["CIRCLE_ARTIFACTS"]
dir = File.join(ENV["CIRCLE_ARTIFACTS"], "coverage")
SimpleCov.coverage_dir(dir)
end
SimpleCov.start "rails" if (ENV["CI"] || ENV["COVERAGE"])
SIMPLECOV
end
end
def setup_environments
setup_dotenv
setup_github_workflows
setup_docker
setup_procfile
configure_i18n
end
def setup_dotenv
copy_file "templates/.env.development", ".env.development"
copy_file "templates/.env.test", ".env.test"
gsub_file ".env.development", "__application_name__", app_name
gsub_file ".env.test", "__application_name__", app_name
end
def setup_github_workflows
copy_file "templates/.github/workflows/run-tests.yml", ".github/workflows/run-tests.yml"
copy_file "templates/.github/workflows/brakeman.yml", ".github/workflows/brakeman.yml"
copy_file ".github/actions/test-rails/action.yml", ".github/actions/test-rails/action.yml"
copy_file ".github/workflows/bundler-audit.yml", ".github/workflows/bundler-audit.yml"
end
def setup_docker
copy_file "templates/Dockerfile", "Dockerfile"
gsub_file "Dockerfile", "__ruby_version__", RUBY_VERSION
setup_docker_standard
end
def setup_procfile
copy_file "templates/Procfile", "Procfile"
end
def setup_docker_standard
copy_file "templates/.env.docker/.env.docker-standard", ".env.docker"
copy_file "templates/docker-compose.yml/docker-compose-standard.yml", "docker-compose.yml"
end
def setup_readme
remove_file "README.md"
copy_file "templates/README.md", "README.md"
gsub_file "README.md", "__application_name__", app_name
end
def configure_i18n
gsub_file(
"config/environments/test.rb",
"# config.action_view.raise_on_missing_translations = true",
"config.action_view.raise_on_missing_translations = true",
)
gsub_file(
"config/environments/development.rb",
"# config.action_view.raise_on_missing_translations = true",
"config.action_view.raise_on_missing_translations = true",
)
end
def ascii_art
puts " _____ __ _ ____ ____ "
puts " / / \\ \\ | \\ | | / \\ | \\ "
puts "| | | | | \\ | | | /\\ | | |\\ \\ "
puts "| | - | \\ | | | | | | | | | | "
puts "| | | |\\ \\ | | | |__| | | |/ / "
puts "| | / -- | | \\ \\ | | | | | | | / "
puts "| | \\ \\ | | \\ \\ | | | | | | | |\\ \\ "
puts " \\ \\ | | | | \\ \\| | | | | | | | \\ \\ "
puts " \\ --/ / | | \\ | | | | | | | \\ \\ "
puts " -----/ |__| \\__| |_| |_| |_| \\_\\ "
end
def post_install_instructions
puts "\n\nNEXT STEPS"
puts "=========="
puts "* Install Google Chrome for acceptance tests"
puts "* Ensure you have Homebrew Cask installed: brew tap homebrew/cask"
puts "* Install ChromeDriver for default headless acceptance tests: brew cask install chromedriver"
puts "* Follow the post-install instructions to set up circle to allow gnarbot to comment on PRs."
puts " * https://github.com/TheGnarCo/gnarails#post-install"
puts "=========="
puts "* Make sure your package.json has the following scripts:"
puts "* \`\"build\": \"node esbuild.config.js\"\`"
puts "* \`\"build:css\": \"sass ./app/assets/stylesheets/application.sass.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules\"\`"
end
def format_ruby
run "bin/rubocop -A"
end
def completion_notification
puts ""
ascii_art
post_install_instructions
puts ""
end
create_gnarly_rails_app