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

Rakefile: Conditionally require local dependencies #1186

Merged
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
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