Skip to content

Commit

Permalink
Rakefile: Conditionally require local dependencies (#1186)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
stevepolitodesign authored Apr 9, 2024
1 parent 8edaa4b commit 473e483
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
13 changes: 9 additions & 4 deletions lib/generators/suspenders/advisories_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ def add_bundler_audit
end

def modify_rakefile
insert_into_file "Rakefile", "\nrequire \"bundler/audit/task\"",
after: 'require_relative "config/application"'
insert_into_file "Rakefile", "\nBundler::Audit::Task.new",
after: 'require "bundler/audit/task"'
content = <<~RUBY
if Rails.env.local?
require "bundler/audit/task"
Bundler::Audit::Task.new
end
RUBY

insert_into_file "Rakefile", content, after: /require_relative "config\/application"\n/
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion lib/generators/suspenders/rake_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ class RakeGenerator < Rails::Generators::Base
TEXT

def configure_default_rake_task
append_to_file "Rakefile", %(task default: "suspenders:rake")
append_to_file "Rakefile", <<~RUBY
if Rails.env.local?
task default: "suspenders:rake"
end
RUBY
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions test/fixtures/files/Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require_relative "config/application"
require "bundler/audit/task"
Bundler::Audit::Task.new

Rails.application.load_tasks

if Rails.env.local?
task default: "suspenders:rake"
end
8 changes: 8 additions & 0 deletions test/fixtures/files/Rakefile_advisories
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative "config/application"

if Rails.env.local?
require "bundler/audit/task"
Bundler::Audit::Task.new
end

Rails.application.load_tasks
2 changes: 1 addition & 1 deletion test/generators/suspenders/advisories_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AdvisoriesGeneratorTest < Rails::Generators::TestCase
Rails.application.load_tasks
TEXT
expected_rakefile = file_fixture("Rakefile").read
expected_rakefile = file_fixture("Rakefile_advisories").read

run_generator

Expand Down
4 changes: 3 additions & 1 deletion test/generators/suspenders/rake_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class RakeGeneratorTest < Rails::Generators::TestCase
teardown :restore_destination

test "modifies existing Rakefile" do
content = file_fixture("Rakefile").read

run_generator

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

Expand Down

0 comments on commit 473e483

Please sign in to comment.