Skip to content

Commit b368389

Browse files
Refactor Rails::Commands::RakeCommand
This factors out a `with_rake` method so that `perform` and `rake_tasks` share a common path.
1 parent a32ff0e commit b368389

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

railties/lib/rails/commands/rake/rake_command.rb

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ class RakeCommand < Base # :nodoc:
99

1010
class << self
1111
def printing_commands
12-
formatted_rake_tasks
12+
rake_tasks.filter_map do |task|
13+
if task.comment && task.locations.any?(non_app_file_pattern)
14+
[task.name_with_args, task.comment]
15+
end
16+
end
1317
end
1418

1519
def perform(task, args, config)
16-
require_rake
17-
18-
Rake.with_application do |rake|
19-
rake.init("bin/rails", [task, *args])
20-
rake.load_rakefile
20+
with_rake(task, *args) do |rake|
2121
if unrecognized_task = rake.top_level_tasks.find { |task| !rake.lookup(task[/[^\[]+/]) }
2222
raise UnrecognizedCommandError.new(unrecognized_task)
2323
end
@@ -32,25 +32,21 @@ def non_app_file_pattern
3232
/\A(?!#{Regexp.quote Rails::Command.root.to_s})/
3333
end
3434

35-
def rake_tasks
36-
require_rake
37-
38-
return @rake_tasks if defined?(@rake_tasks)
39-
35+
def with_rake(*args, &block)
36+
require "rake"
4037
Rake::TaskManager.record_task_metadata = true
41-
Rake.application.instance_variable_set(:@name, "rails")
42-
Rake.application.load_rakefile
43-
@rake_tasks = Rake.application.tasks.select do |task|
44-
task.comment && task.locations.any?(non_app_file_pattern)
45-
end
46-
end
4738

48-
def formatted_rake_tasks
49-
rake_tasks.map { |t| [ t.name_with_args, t.comment ] }
39+
result = nil
40+
Rake.with_application do |rake|
41+
rake.init(bin, args) unless args.empty?
42+
rake.load_rakefile
43+
result = block.call(rake)
44+
end
45+
result
5046
end
5147

52-
def require_rake
53-
require "rake" # Defer booting Rake until we know it's needed.
48+
def rake_tasks
49+
@rake_tasks ||= with_rake(&:tasks)
5450
end
5551
end
5652
end

0 commit comments

Comments
 (0)