Skip to content

Commit

Permalink
Introduce suspenders:install:web generator and application template (
Browse files Browse the repository at this point in the history
…#1152)

Create generator to invoke all necessary generators. We add it to the
`install` namespace to provide flexibility should we add other
installation options, such as ones for API Only applications.

Introduces [template][] as a means to invoke `suspenders:install:web`
when creating a new application. This serves as an alternative to the
system executable that was removed.

We call `db:prepare` in the template to ensure `bin/setup` works as
expected. This is because we overrode that file to use `dev:prime`,
which assume the database has been created.

[template]: https://guides.rubyonrails.org/rails_application_templates.html
  • Loading branch information
stevepolitodesign authored Apr 14, 2024
1 parent c694b2a commit e8eb46a
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Unreleased
* Introduce `suspenders:environments:production` generator
* Introduce `suspenders:environments:test` generator
* Introduce `suspenders:environments:development` generator
* Introduce `suspenders:install:web` generator

20230113.0 (January, 13, 2023)

Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ if you like missing deadlines.

## Usage

### Existing Rails Applications

```
group :development, :test do
gem "suspenders"
end
```

```
bin/rails g suspenders:all
bin/rails g suspenders:install:web
```

### New Rails Applications

```
rails new my_app \
-d=postgresql \
-m=https://raw.githubusercontent.com/thoughtbot/suspenders/lib/install/web.rb
```

## Generators
Expand Down
67 changes: 67 additions & 0 deletions lib/generators/suspenders/install/web_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module Suspenders
module Generators
module Install
class WebGenerator < Rails::Generators::Base
include Suspenders::Generators::APIAppUnsupported
include Suspenders::Generators::DatabaseUnsupported

desc <<~MARKDOWN
Invokes all necessary generators for new Rails applications generated with Suspenders.
This generatator is intended to be invoked as part of an [application template][].
```
rails new suspenders_qa \
--skip-test \
-d=postgresql \
-m=https://raw.githubusercontent.com/thoughtbot/suspenders/main/lib/install/web.rb
```
[application template]: https://guides.rubyonrails.org/rails_application_templates.html
MARKDOWN

def invoke_generators
# This needs to go first, since it configures `.node-version`
generate "suspenders:prerequisites"

generate "suspenders:accessibility"
generate "suspenders:advisories"
generate "suspenders:email"
generate "suspenders:factories"
generate "suspenders:inline_svg"
generate "suspenders:lint"
generate "suspenders:rake"
generate "suspenders:setup"
generate "suspenders:tasks"
generate "suspenders:testing"
generate "suspenders:views"

# suspenders:jobs needs to be invoked before suspenders:styles, since
# suspenders:styles generator creates Procfile.dev
generate "suspenders:styles"
generate "suspenders:jobs"

# Needs to run after other generators, since some touch the
# configuration files.
generate "suspenders:environments:test"
generate "suspenders:environments:development"
generate "suspenders:environments:production"

# Needs to be run last since it depends on lint, testing, and
# advisories
generate "suspenders:ci"
end

def cleanup
rake "suspenders:cleanup:organize_gemfile"
rake "suspenders:cleanup:generate_readme"
end

def lint
run "yarn run fix:prettier"
run "bundle exec rake standard:fix_unsafely"
end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/install/web.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def apply_template!
if options[:database] == "postgresql" && options[:skip_test]
after_bundle do
gem_group :development, :test do
# TODO: Update once in `main`
gem "suspenders", github: "thoughtbot/suspenders", branch: "suspenders-3-0-0"
end

run "bundle install"

generate "suspenders:install:web"
rails_command "db:prepare"

say "\nCongratulations! You just pulled our suspenders."
end
else
message = <<~ERROR
=== Please use the correct options ===
rails new <app_name> \\
--skip-test \\
-d=postgresql \\
-m=https://raw.githubusercontent.com/thoughtbot/suspenders/main/lib/install/web.rb
ERROR

fail Rails::Generators::Error, message
end
end

apply_template!
43 changes: 43 additions & 0 deletions test/generators/suspenders/install/web_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "test_helper"
require "generators/suspenders/install/web_generator"

module Suspenders
module Generators
module Install
class WebGeneratorTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::Install::WebGenerator
destination Rails.root
setup :prepare_destination
teardown :restore_destination

test "raises if API only application" do
within_api_only_app do
assert_raises Suspenders::Generators::APIAppUnsupported::Error do
run_generator
end
end
end

test "raises if PostgreSQL is not the adapter" do
with_database "unsupported" do
assert_raises Suspenders::Generators::DatabaseUnsupported::Error do
run_generator
end
end
end

private

def prepare_destination
touch "Gemfile"
end

def restore_destination
remove_file_if_exists "Gemfile"
end
end
end
end
end

0 comments on commit e8eb46a

Please sign in to comment.