这是 Rake 的初稿
# Rakefile
task "third_task", %w[first_task second_task] do
puts "third task"
end
task "first_task", %w[before_task] do
puts "first task"
end
task "second_task", %w[before_task] do
puts "second task"
end
task "before_task" do
puts "before"
end
让我们来执行 Rake 任务
$> rake third_task
before
first task
second task
third task
这是 Rake 最初如何工作的,让我们再来看看 Rake 脚本内容。
# micro-rake.rb
TASKS = {}
class Task
def initialize(name, deps, action)
@name = name
@deps = deps
@action = action
end
def invoke
return if @already_run # 避免重复执行任务
@deps.each do |dep| TASKS[dep].invoke end # 解决依赖问题
execute # 执行任务
@already_run = true # 标记此任务已执行
end
def execute
@action.call # 在Rakefile里执行block的“task”命令
end
end
def task(name, deps=[], &block)
TASKS[name] = Task.new(name, deps, block)
end
load './Rakefile'
ARGV.each do |arg| TASKS[arg].invoke end
$> ruby micro-rake.rb third_task
before
first task
second task
third task