Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are task dependencies always run in a specific order with rake?

Tags:

ruby

rake

I have the following example which is based on the structure i want my rakefile to use:

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

Will the tasks always be run in the same order? I read somewhere that they wouldn't, and somewhere else that they would, so i'm not sure.

Or if you can suggest a better way to do what i'm trying to achieve (eg have a common init and cleanup step surrounding a depending-upon-environment step), that'd also be good.

Thanks

like image 841
Chris Avatar asked Mar 16 '11 22:03

Chris


People also ask

How do I run a rake task?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.

What does Rakefile do?

It allows you to use ruby code to define "tasks" that can be run in the command line. Rake can be downloaded and included in ruby projects as a ruby gem. Once installed, you define tasks in a file named "Rakefile" that you add to your project.


1 Answers

From the Rake source code:

# 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

So it appears that the code normally just iterates the array and runs the prerequisite tasks sequentially.

However:

# 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

So you are right, both can be true, but order can only be off if you prefix your task with multitask It looks like regular tasks are run in order.

like image 99
Michael Papile Avatar answered Oct 06 '22 01:10

Michael Papile