2011-03-16 63 views
9

我有以下的例子是基於我希望我的Rakefile使用結構:任務依賴關係是否始終按照特定的順序運行?

task :default do 
    puts 'Tasks you can run: dev, stage, prod' 
end 

task :dev => [:init,:devrun,:clean] 
task :devrun do 
    puts 'Dev stuff' 
end 

task :stage => [:init,:stagerun,:clean] 
task :stagerun do 
    puts 'Staging stuff' 
end 

task :prod => [:init,:prodrun,:clean] 
task :prodrun do 
    puts 'Production stuff' 
end 

task :init do 
    puts 'Init...' 
end 

task :clean do 
    puts 'Cleanup' 
end 

將任務總是以相同的順序來運行?我在某個地方看過他們不會的地方,還有他們會去的地方,所以我不確定。

或者,如果您可以建議一種更好的方式來實現我想要實現的目標(例如,圍繞依賴於環境的步驟具有共同的初始化和清理步驟),那也會很好。

由於

回答

15

從RAKE源碼:

# Invoke all the prerequisites of a task. 
def invoke_prerequisites(task_args, invocation_chain) # :nodoc: 
    @prerequisites.each { |n| 
    prereq = application[n, @scope] 
    prereq_args = task_args.new_scope(prereq.arg_names) 
    prereq.invoke_with_call_chain(prereq_args, invocation_chain) 
    } 
end 

所以看來該代碼通常只是迭代陣列並依次運行的前提任務。

但是:

# Declare a task that performs its prerequisites in parallel. Multitasks does 
# *not* guarantee that its prerequisites will execute in any given order 
# (which is obvious when you think about it) 
# 
# Example: 
# multitask :deploy => [:deploy_gem, :deploy_rdoc] 
# 
def multitask(args, &block) 
    Rake::MultiTask.define_task(args, &block) 
end 

所以,你是對的,既可以是真實的,但如果你有multitask它看起來像普通的任務,以便運行前綴你的任務訂單隻能關閉。