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:install:web and application template #1152

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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
stevepolitodesign marked this conversation as resolved.
Show resolved Hide resolved
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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to figure this out:

❯ yarn run lint
yarn run v1.22.17
$ run-p lint:eslint lint:stylelint lint:prettier
$ eslint --max-warnings=0 --no-error-on-unmatched-pattern 'app/javascript/**/*.js'
$ prettier --check '**/*' --ignore-unknown
$ stylelint 'app/assets/stylesheets/**/*.css'
Checking formatting...
.github/workflows/ci.yaml
Oops! Something went wrong! :(

ESLint: 9.0.0

Error: Could not find config file.
    at locateConfigFileToUse (/Users/polito/Desktop/suspenders_qa_0/node_modules/eslint/lib/eslint/eslint.js:349:21)
    at async calculateConfigArray (/Users/polito/Desktop/suspenders_qa_0/node_modules/eslint/lib/eslint/eslint.js:384:49)
    at async ESLint.lintFiles (/Users/polito/Desktop/suspenders_qa_0/node_modules/eslint/lib/eslint/eslint.js:814:25)
    at async Object.execute (/Users/polito/Desktop/suspenders_qa_0/node_modules/eslint/lib/cli.js:461:23)
    at async main (/Users/polito/Desktop/suspenders_qa_0/node_modules/eslint/bin/eslint.js:165:22)
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
app/assets/stylesheets/application.postcss.cssERROR: "lint:eslint" exited with 2.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relates to eslint/eslint#18287

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can merge this commit without a solution for the above, since that can be addressed separately, and is not related to with commit.

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
stevepolitodesign marked this conversation as resolved.
Show resolved Hide resolved
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