-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrake_task.rb
105 lines (83 loc) · 2.49 KB
/
rake_task.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module RakeUi
class RakeTask
def self.load
Rails.application.load_tasks
Rake::Task.tasks
end
def self.all
self.load.map do |task|
new(task)
end
end
def self.internal
self.load.map do |task|
new(task)
end.select(&:is_internal_task)
end
def self.find_by_id(id)
t = all
i = CGI.unescape(id)
t.find do |task|
task.name == i
end
end
attr_reader :task
delegate :name, :actions, :name_with_args, :arg_description, :full_comment, :locations, :sources, to: :task
def initialize(task)
@task = task
end
def id
CGI.escape(name)
end
# actions will be something like #<Proc:0x000055a2737fe778@/some/rails/app/lib/tasks/auto_annotate_models.rake:4>
def rake_definition_file
actions.first
rescue StandardError
"unable_to_determine_defining_file"
end
def is_internal_task
internal_task?
end
# thinking this is the sanest way to discern application vs gem defined tasks (like rails, devise etc)
def internal_task?
actions.any? {|a| !a.to_s.include? "/ruby/gems" }
# this was my initial thought here, leaving for posterity in case we need to or the definition of custom
# from initial investigation the actions seemed like the most consistent as locations is sometimes empty
# locations.any? do |location|
# !location.match(/\/bundle\/gems/)
# end
end
def call(args: nil, environment: nil)
rake_command = build_rake_command(args: args, environment: environment)
rake_task_log = RakeUi::RakeTaskLog.build_new_for_command(
name: name,
args: args,
environment: environment,
rake_command: rake_command,
rake_definition_file: rake_definition_file,
raker_id: id
)
puts "[rake_ui] [rake_task] [forked] #{rake_task_log.rake_command_with_logging}"
fork do
system(rake_task_log.rake_command_with_logging)
system(rake_task_log.command_to_mark_log_finished)
end
rake_task_log
end
# returns an invokable rake command
# FOO=bar rake create_something[1,2,3]
# rake create_something[1,2,3]
# rake create_something
def build_rake_command(args: nil, environment: nil)
command = ""
if environment
command += "#{environment.to_s} "
end
command += "rake #{name}"
if args
command += "[#{args.to_s}]"
end
command
end
end
end