Skip to content

Commit ba5452b

Browse files
Introduce suspenders:setup generator (#1157)
Replaces the [default setup script][] provided by Rails. The trade-off being that the implementation is simple, but the cost is we risk drifting from Rails. After attempting to use [gsub_file][] and [insert_into_file][], it felt simpler to just override the file completely. Conditionally call [dev:prime][] to seed development data on top of [seed][] data necessary for production. A follow-up commit will re-introduce this task. Additionally, we [setup the test environment][] to avoid issues with asset compilation. [default setup script]: https://github.com/rails/rails/blob/main/railties/lib/rails/generators/rails/app/templates/bin/setup.tt [gsub_file]: https://rubydoc.info/gems/thor/Thor/Actions#gsub_file-instance_method [insert_into_file]: https://rubydoc.info/gems/thor/Thor/Actions#insert_into_file-instance_method [dev:prime]: https://thoughtbot.com/blog/priming-the-pump [seed]: https://guides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data [setup the test environment]: https://github.com/rails/rails/pull/47719/files
1 parent 9233e36 commit ba5452b

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Unreleased
1010
* Introduce `suspenders:lint` generator
1111
* Introduce `suspenders:rake` generator
1212
* Introduce `suspenders:views` generator
13+
* Introduce `suspenders:setup` generator
1314

1415
20230113.0 (January, 13, 2023)
1516

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ document [lang][].
134134
[title]: https://github.com/calebhearth/title
135135
[lang]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang
136136

137+
### Setup
138+
139+
A holistic setup script.
140+
141+
```sh
142+
bin/setup
143+
```
144+
137145
## Contributing
138146

139147
See the [CONTRIBUTING] document.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Suspenders
2+
module Generators
3+
class SetupGenerator < Rails::Generators::Base
4+
source_root File.expand_path("../../templates/setup", __FILE__)
5+
desc <<~TEXT
6+
A holistic setup script.
7+
8+
```sh
9+
bin/setup
10+
```
11+
TEXT
12+
13+
def replace_bin_setup
14+
copy_file "bin_setup.rb", "bin/setup", force: true
15+
end
16+
end
17+
end
18+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env ruby
2+
require "fileutils"
3+
require "bundler"
4+
5+
# path to your application root.
6+
APP_ROOT = File.expand_path("..", __dir__)
7+
8+
def system!(*args)
9+
system(*args, exception: true)
10+
end
11+
12+
def using_node?
13+
File.exist? "package.json"
14+
end
15+
16+
FileUtils.chdir APP_ROOT do
17+
puts "== Installing dependencies =="
18+
system! "gem install bundler --conservative"
19+
system("bundle check") || system!("bundle install")
20+
system("yarn install --check-files") if using_node?
21+
22+
puts "\n== Preparing database and adding development seed data =="
23+
if File.exist? "lib/tasks/dev.rake"
24+
system! "bin/rails dev:prime"
25+
else
26+
system! "bin/rails db:prepare"
27+
end
28+
29+
if Bundler.rubygems.find_name("sprockets")
30+
puts "\n== Generating assets =="
31+
system! "bin/rails assets:clobber assets:precompile"
32+
end
33+
34+
# https://github.com/rails/rails/pull/47719/files
35+
puts "\n== Setup test environment =="
36+
system! "bin/rails test:prepare"
37+
38+
puts "\n== Removing old logs and tempfiles =="
39+
system! "bin/rails log:clear tmp:clear"
40+
41+
puts "\n== Restarting application server =="
42+
system! "bin/rails restart"
43+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
if Rails.env.development? || Rails.env.test?
2+
require "factory_bot" if Bundler.rubygems.find_name("factory_bot_rails").any?
3+
4+
namespace :dev do
5+
desc "Sample data for local development environment"
6+
task prime: "db:setup" do
7+
include FactoryBot::Syntax::Methods if Bundler.rubygems.find_name("factory_bot_rails").any?
8+
9+
# create(:user, email: "[email protected]", password: "password")
10+
end
11+
end
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require "test_helper"
2+
require "generators/suspenders/setup_generator"
3+
4+
module Suspenders
5+
module Generators
6+
class SetupGeneratorTest < Rails::Generators::TestCase
7+
include Suspenders::TestHelpers
8+
9+
tests Suspenders::Generators::SetupGenerator
10+
destination Rails.root
11+
setup :prepare_destination
12+
teardown :restore_destination
13+
14+
test "modifies bin/setup" do
15+
expected = bin_setup
16+
17+
run_generator
18+
19+
assert_file app_root("bin/setup") do |file|
20+
assert_equal expected, file
21+
end
22+
end
23+
24+
test "has a custom description" do
25+
assert_no_match(/Description:/, generator_class.desc)
26+
end
27+
28+
private
29+
30+
def prepare_destination
31+
backup_file "bin/setup"
32+
end
33+
34+
def restore_destination
35+
remove_file_if_exists "Brewfile"
36+
restore_file "bin/setup"
37+
remove_dir_if_exists "lib/tasks"
38+
end
39+
40+
def bin_setup
41+
File.read("./lib/generators/templates/setup/bin_setup.rb")
42+
end
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)