Skip to content
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 8 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Unreleased
* Introduce `suspenders:lint` generator
* Introduce `suspenders:rake` generator
* Introduce `suspenders:views` generator
* Introduce `suspenders:setup` generator

20230113.0 (January, 13, 2023)

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ document [lang][].
[title]: https://github.com/calebhearth/title
[lang]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang

### Setup

A holistic setup script.

```sh
bin/setup
```

## Contributing

See the [CONTRIBUTING] document.
Expand Down
18 changes: 18 additions & 0 deletions lib/generators/suspenders/setup_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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
```
TEXT

def replace_bin_setup
copy_file "bin_setup.rb", "bin/setup", force: true
end
end
end
end
43 changes: 43 additions & 0 deletions lib/generators/templates/setup/bin_setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env ruby
Copy link
Contributor

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.

require "fileutils"
require "bundler"

# 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
puts "== 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 File.exist? "lib/tasks/dev.rake"
system! "bin/rails dev:rake"
stevepolitodesign marked this conversation as resolved.
Show resolved Hide resolved
else
system! "bin/rails db:prepare"
end

if Bundler.rubygems.find_name("sprockets")
puts "\n== Generating assets =="
system! "bin/rails assets:clobber assets:precompile"
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
12 changes: 12 additions & 0 deletions lib/generators/templates/setup/dev_prime.rb
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
45 changes: 45 additions & 0 deletions test/generators/suspenders/setup_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 "modifies bin/setup" do
expected = bin_setup

run_generator

assert_file app_root("bin/setup") 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