我有以下示例是基于我想要我的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
任务总是按照相同的顺序运行吗?我读到某个地方,他们不会,还有别的地方,所以我不知道.
或者,如果您可以建议一个更好的方法来做我正在尝试实现的(例如,有一个常见的初始化和清理步骤围绕依赖环境的步骤),那也是很好的.
谢谢
解决方法
从耙源码:
# 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
所以你是对的,都可以是真的,但是只有在你的任务前面加上多任务的时候,命令才能被关掉它看起来像是常规的任务按顺序运行.