@@ -9,15 +9,15 @@ class RakeCommand < Base # :nodoc:
9
9
10
10
class << self
11
11
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
13
17
end
14
18
15
19
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 |
21
21
if unrecognized_task = rake . top_level_tasks . find { |task | !rake . lookup ( task [ /[^\[ ]+/ ] ) }
22
22
raise UnrecognizedCommandError . new ( unrecognized_task )
23
23
end
@@ -32,25 +32,21 @@ def non_app_file_pattern
32
32
/\A (?!#{ Regexp . quote Rails ::Command . root . to_s } )/
33
33
end
34
34
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"
40
37
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
47
38
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
50
46
end
51
47
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 )
54
50
end
55
51
end
56
52
end
0 commit comments