Skip to content

Commit 551d60b

Browse files
Introduce suspenders:rake generator (#1144)
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 Co-authored-by: Mike Burns <[email protected]>
1 parent fe5fc93 commit 551d60b

File tree

8 files changed

+89
-0
lines changed

8 files changed

+89
-0
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Unreleased
88
* Introduce `suspenders:styles` generator
99
* Introduce `suspenders:jobs` generator
1010
* Introduce `suspenders:lint` generator
11+
* Introduce `suspenders:rake` generator
1112

1213
20230113.0 (January, 13, 2023)
1314

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ Installs [Sidekiq][] for background job processing and configures ActiveJob for
116116

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

119+
### Rake
120+
121+
Configures the default Rake task to audit and lint the codebase with
122+
[bundler-audit][] and [standard][] in addition to running the test suite.
123+
124+
`bin/rails g suspenders:rake`
125+
126+
[bundler-audit]: https://github.com/rubysec/bundler-audit
127+
[standard]: https://github.com/standardrb/standard
128+
119129
## Contributing
120130

121131
See the [CONTRIBUTING] document.

bin/rails

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# installed from the root of your application.
44

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

89
# Set up gems listed in the Gemfile.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Suspenders
2+
module Generators
3+
class RakeGenerator < Rails::Generators::Base
4+
source_root File.expand_path("../../templates/rake", __FILE__)
5+
desc(<<~TEXT)
6+
Configures the default Rake task to audit and lint the codebase with
7+
`bundler-audit` and `standard`, in addition to running the test suite.
8+
TEXT
9+
10+
def configure_default_rake_task
11+
append_to_file "Rakefile", %(task default: "suspenders:rake")
12+
end
13+
end
14+
end
15+
end

lib/suspenders.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "suspenders/version"
2+
require "suspenders/engine"
23
require "suspenders/railtie"
34
require "suspenders/generators"
45

lib/suspenders/engine.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Suspenders
2+
class Engine < ::Rails::Engine
3+
isolate_namespace Suspenders
4+
end
5+
end

lib/tasks/suspenders.rake

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace :suspenders do
2+
desc "Extend the default Rails Rake task"
3+
task :rake do
4+
if Bundler.rubygems.find_name("bundler-audit").any?
5+
Rake::Task[:"bundle:audit"].invoke
6+
end
7+
8+
if Bundler.rubygems.find_name("standard").any?
9+
Rake::Task[:standard].invoke
10+
end
11+
end
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require "test_helper"
2+
require "generators/suspenders/rake_generator"
3+
4+
module Suspenders
5+
module Generators
6+
class RakeGeneratorTest < Rails::Generators::TestCase
7+
include Suspenders::TestHelpers
8+
9+
tests Suspenders::Generators::RakeGenerator
10+
destination Rails.root
11+
setup :prepare_destination
12+
teardown :restore_destination
13+
14+
test "modifies existing Rakefile" do
15+
run_generator
16+
17+
assert_file app_root("Rakefile") do |file|
18+
assert_match /task default: "suspenders:rake"/, file
19+
end
20+
end
21+
22+
test "generator has a description" do
23+
description = <<~TEXT
24+
Configures the default Rake task to audit and lint the codebase with
25+
`bundler-audit` and `standard`, in addition to running the test suite.
26+
TEXT
27+
28+
assert_equal description, generator_class.desc
29+
end
30+
31+
private
32+
33+
def prepare_destination
34+
touch "Gemfile"
35+
backup_file "Rakefile"
36+
end
37+
38+
def restore_destination
39+
remove_file_if_exists "Gemfile"
40+
restore_file "Rakefile"
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)