Skip to content

Commit 473e483

Browse files
Rakefile: Conditionally require local dependencies (#1186)
Prior to this commit, the Rakefile required `bundler-audit` and `standard` in all environments. This prevented Rake from running in production, because those dependencies are only loaded in `:development` and `:test`.
1 parent 8edaa4b commit 473e483

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
lines changed

lib/generators/suspenders/advisories_generator.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ def add_bundler_audit
1717
end
1818

1919
def modify_rakefile
20-
insert_into_file "Rakefile", "\nrequire \"bundler/audit/task\"",
21-
after: 'require_relative "config/application"'
22-
insert_into_file "Rakefile", "\nBundler::Audit::Task.new",
23-
after: 'require "bundler/audit/task"'
20+
content = <<~RUBY
21+
22+
if Rails.env.local?
23+
require "bundler/audit/task"
24+
Bundler::Audit::Task.new
25+
end
26+
RUBY
27+
28+
insert_into_file "Rakefile", content, after: /require_relative "config\/application"\n/
2429
end
2530
end
2631
end

lib/generators/suspenders/rake_generator.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ class RakeGenerator < Rails::Generators::Base
88
TEXT
99

1010
def configure_default_rake_task
11-
append_to_file "Rakefile", %(task default: "suspenders:rake")
11+
append_to_file "Rakefile", <<~RUBY
12+
13+
if Rails.env.local?
14+
task default: "suspenders:rake"
15+
end
16+
RUBY
1217
end
1318
end
1419
end

test/fixtures/files/Rakefile

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
14
require_relative "config/application"
2-
require "bundler/audit/task"
3-
Bundler::Audit::Task.new
45

56
Rails.application.load_tasks
7+
8+
if Rails.env.local?
9+
task default: "suspenders:rake"
10+
end
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require_relative "config/application"
2+
3+
if Rails.env.local?
4+
require "bundler/audit/task"
5+
Bundler::Audit::Task.new
6+
end
7+
8+
Rails.application.load_tasks

test/generators/suspenders/advisories_generator_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class AdvisoriesGeneratorTest < Rails::Generators::TestCase
4848
4949
Rails.application.load_tasks
5050
TEXT
51-
expected_rakefile = file_fixture("Rakefile").read
51+
expected_rakefile = file_fixture("Rakefile_advisories").read
5252

5353
run_generator
5454

test/generators/suspenders/rake_generator_test.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ class RakeGeneratorTest < Rails::Generators::TestCase
1212
teardown :restore_destination
1313

1414
test "modifies existing Rakefile" do
15+
content = file_fixture("Rakefile").read
16+
1517
run_generator
1618

1719
assert_file app_root("Rakefile") do |file|
18-
assert_match(/task default: "suspenders:rake"/, file)
20+
assert_equal content, file
1921
end
2022
end
2123

0 commit comments

Comments
 (0)