Skip to content

Commit

Permalink
Introduce suspenders:rake generator
Browse files Browse the repository at this point in the history
Add necessary files to make the [plugin][] an [engine][], which
automatically loads Rake tasks located in `lib/tasks`. This means when
the `suspenders` gem is installed, the consumer can run `bundle exec
rake suspenders:rake`, and any future tasks.

Because `suspenders:lint` and `suspenders:advisories` may not
necessarily have been invoked, we need to check if those gems are
installed.

[plugin]: https://guides.rubyonrails.org/plugins.html
[engine]: https://guides.rubyonrails.org/engines.html

To Do

- [ ] RSpec
- [ ] Do we need to require bundler-audit in the Rakefile

Co-authored-by: Mike Burns <[email protected]>
  • Loading branch information
stevepolitodesign and mike-burns committed Dec 12, 2023
1 parent fe5fc93 commit 34fb28a
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Unreleased
* Introduce `suspenders:styles` generator
* Introduce `suspenders:jobs` generator
* Introduce `suspenders:lint` generator
* Introduce `suspenders:rake` generator

20230113.0 (January, 13, 2023)

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Installs [capybara_accessibility_audit] and [capybara_accessible_selectors]
[capybara_accessibility_audit]: https://github.com/thoughtbot/capybara_accessibility_audit
[capybara_accessible_selectors]: https://github.com/citizensadvice/capybara_accessible_selectors

<<<<<<< HEAD
### Advisories

Show security advisories during development.
Expand Down Expand Up @@ -116,6 +117,16 @@ Installs [Sidekiq][] for background job processing and configures ActiveJob for

[Sidekiq]: https://github.com/sidekiq/sidekiq

### Rake

Configures the default Rake task to audit and lint the codebase with
[bundler-audit][] and [standard][].

`/bin/rails g suspenders:rake`

[bundler-audit]: https://github.com/rubysec/bundler-audit
[standard]: https://github.com/standardrb/standard

## Contributing

See the [CONTRIBUTING] document.
Expand Down
1 change: 1 addition & 0 deletions bin/rails
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# installed from the root of your application.

ENGINE_ROOT = File.expand_path("..", __dir__)
ENGINE_PATH = File.expand_path("../lib/suspenders/engine", __dir__)
APP_PATH = File.expand_path("../test/dummy/config/application", __dir__)

# Set up gems listed in the Gemfile.
Expand Down
14 changes: 14 additions & 0 deletions lib/generators/suspenders/rake_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Suspenders
module Generators
class RakeGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates/rake", __FILE__)
desc(<<~TEXT)
Configures the default Rake task to audit and lint the codebase with `bundler-audit` and `standard`.
TEXT

def configure_default_rake_task
append_to_file "Rakefile", %(task default: "suspenders:rake")
end
end
end
end
1 change: 1 addition & 0 deletions lib/suspenders.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "suspenders/version"
require "suspenders/engine"
require "suspenders/railtie"
require "suspenders/generators"

Expand Down
5 changes: 5 additions & 0 deletions lib/suspenders/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Suspenders
class Engine < ::Rails::Engine
isolate_namespace Suspenders
end
end
12 changes: 12 additions & 0 deletions lib/tasks/suspenders.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace :suspenders do
desc "Default task for Suspenders"
task :rake do
if Bundler.rubygems.find_name("bundler-audit").any?
Rake::Task[:"bundle:audit"].invoke
end

if Bundler.rubygems.find_name("standard").any?
Rake::Task[:standard].invoke
end
end
end
43 changes: 43 additions & 0 deletions test/generators/suspenders/rake_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "test_helper"
require "generators/suspenders/rake_generator"

module Suspenders
module Generators
class RakeGeneratorTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

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

test "modifies existing Rakefile" do
run_generator

assert_file app_root("Rakefile") do |file|
assert_match /task default: "suspenders:rake"/, file
end
end

test "generator has a description" do
description = <<~TEXT
Configures the default Rake task to audit and lint the codebase with `bundler-audit` and `standard`.
TEXT

assert_equal description, generator_class.desc
end

private

def prepare_destination
touch "Gemfile"
backup_file "Rakefile"
end

def restore_destination
remove_file_if_exists "Gemfile"
restore_file "Rakefile"
end
end
end
end

0 comments on commit 34fb28a

Please sign in to comment.