-
-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce suspenders:setup
generator
#1157
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c338a85
Introduce `suspenders:setup` generator
stevepolitodesign d283063
Remove option to wipe database
stevepolitodesign 893ad7b
Conditionally generate assets during setup
stevepolitodesign 006b133
Remove reference to wiping database
stevepolitodesign 97068cd
Remove system setup steps
stevepolitodesign b074e46
Remove `dev:prime`
stevepolitodesign f55a1d6
Remove obsolete test
stevepolitodesign c010e4a
Reference correct task
stevepolitodesign File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Suspenders | ||
module Generators | ||
class SetupGenerator < Rails::Generators::Base | ||
source_root File.expand_path("../../templates/setup", __FILE__) | ||
desc <<~TEXT | ||
A holistic setup script. | ||
|
||
```sh | ||
bin/setup | ||
``` | ||
|
||
or | ||
|
||
```sh | ||
WIPE_DATABASE=true bin/setup | ||
``` | ||
TEXT | ||
|
||
def create_brewfile | ||
copy_file "brewfile", "Brewfile" | ||
end | ||
|
||
def create_dev_prime | ||
copy_file "dev_prime.rb", "lib/tasks/dev.rake" | ||
end | ||
|
||
def replace_bin_setup | ||
copy_file "bin_setup.rb", "bin/setup", force: true | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env ruby | ||
require "fileutils" | ||
require "open3" | ||
|
||
# path to your application root. | ||
APP_ROOT = File.expand_path("..", __dir__) | ||
|
||
def system!(*args) | ||
system(*args, exception: true) | ||
end | ||
|
||
def using_node? | ||
File.exist? "package.json" | ||
end | ||
|
||
FileUtils.chdir APP_ROOT do | ||
if ENV["CI"].nil? | ||
stevepolitodesign marked this conversation as resolved.
Show resolved
Hide resolved
|
||
puts "\n== Installing Homebrew Bundle from Brewfile ==" | ||
system! "brew bundle" | ||
|
||
puts "\n== Starting postgresql@15 ==" | ||
system! "brew services restart postgresql@15" | ||
|
||
puts "\n== Starting redis ==" | ||
system! "brew services restart redis" | ||
|
||
_, _, status = Open3.capture3("which asdf") | ||
stevepolitodesign marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if status.success? | ||
puts "\n== Installing tool dependecies via asdf ==" | ||
system! "asdf plugin-add ruby https://github.com/asdf-vm/asdf-ruby" | ||
system! "asdf plugin-add nodejs https://github.com/asdf-vm/asdf-nodejs" if using_node? | ||
system! "asdf plugin-add yarn https://github.com/twuni/asdf-yarn" if using_node? | ||
system! "asdf plugin-update --all" | ||
system! "asdf install" | ||
end | ||
end | ||
|
||
puts "\n== Installing dependencies ==" | ||
system! "gem install bundler --conservative" | ||
system("bundle check") || system!("bundle install") | ||
system("yarn install --check-files") if using_node? | ||
|
||
puts "\n== Preparing database and adding development seed data ==" | ||
if ENV["WIPE_DATABASE"] == true | ||
system! "bin/rails db:reset dev:prime" | ||
else | ||
system! "bin/rails dev:prime" | ||
end | ||
|
||
# https://github.com/rails/rails/pull/47719/files | ||
puts "\n== Setup test environment ==" | ||
system! "bin/rails test:prepare" | ||
|
||
puts "\n== Removing old logs and tempfiles ==" | ||
system! "bin/rails log:clear tmp:clear" | ||
|
||
puts "\n== Restarting application server ==" | ||
system! "bin/rails restart" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# https://formulae.brew.sh/formula/vips | ||
brew "vips" | ||
|
||
# https://formulae.brew.sh/formula/redis | ||
brew "redis" | ||
stevepolitodesign marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# https://formulae.brew.sh/formula/postgresql@15 | ||
# Change version to match production | ||
brew "postgresql@15" | ||
stevepolitodesign marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
if Rails.env.development? || Rails.env.test? | ||
require "factory_bot" if Bundler.rubygems.find_name("factory_bot_rails").any? | ||
stevepolitodesign marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace :dev do | ||
desc "Sample data for local development environment" | ||
task prime: "db:setup" do | ||
stevepolitodesign marked this conversation as resolved.
Show resolved
Hide resolved
|
||
include FactoryBot::Syntax::Methods if Bundler.rubygems.find_name("factory_bot_rails").any? | ||
|
||
# create(:user, email: "[email protected]", password: "password") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require "test_helper" | ||
require "generators/suspenders/setup_generator" | ||
|
||
module Suspenders | ||
module Generators | ||
class SetupGeneratorTest < Rails::Generators::TestCase | ||
include Suspenders::TestHelpers | ||
|
||
tests Suspenders::Generators::SetupGenerator | ||
destination Rails.root | ||
setup :prepare_destination | ||
teardown :restore_destination | ||
|
||
test "creates Brewfile" do | ||
expected = <<~TEXT | ||
# https://formulae.brew.sh/formula/vips | ||
brew "vips" | ||
|
||
# https://formulae.brew.sh/formula/redis | ||
brew "redis" | ||
|
||
# https://formulae.brew.sh/formula/postgresql@15 | ||
# Change version to match production | ||
brew "postgresql@15" | ||
TEXT | ||
|
||
run_generator | ||
|
||
assert_file app_root("Brewfile") do |file| | ||
assert_equal expected, file | ||
end | ||
end | ||
|
||
test "modifies bin/setup" do | ||
expected = bin_setup | ||
|
||
run_generator | ||
|
||
assert_file app_root("bin/setup") do |file| | ||
assert_equal expected, file | ||
end | ||
end | ||
|
||
test "creates dev:prime task" do | ||
expected = <<~RUBY | ||
if Rails.env.development? || Rails.env.test? | ||
require "factory_bot" if Bundler.rubygems.find_name("factory_bot_rails").any? | ||
|
||
namespace :dev do | ||
desc "Sample data for local development environment" | ||
task prime: "db:setup" do | ||
include FactoryBot::Syntax::Methods if Bundler.rubygems.find_name("factory_bot_rails").any? | ||
|
||
# create(:user, email: "[email protected]", password: "password") | ||
end | ||
end | ||
end | ||
RUBY | ||
|
||
run_generator | ||
|
||
assert_file app_root("lib/tasks/dev.rake") do |file| | ||
assert_equal expected, file | ||
end | ||
end | ||
|
||
test "has a custom description" do | ||
assert_no_match(/Description:/, generator_class.desc) | ||
end | ||
|
||
private | ||
|
||
def prepare_destination | ||
backup_file "bin/setup" | ||
end | ||
|
||
def restore_destination | ||
remove_file_if_exists "Brewfile" | ||
restore_file "bin/setup" | ||
remove_dir_if_exists "lib/tasks" | ||
end | ||
|
||
def bin_setup | ||
File.read("./lib/generators/templates/setup/bin_setup.rb") | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I admit, the idea of writing this shell script in Ruby never made sense to me.