Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rake multitask with a variable task list

I've read some posts, tips and tutorials about using rake arguments and rake multitask. The following would be some simple examples.

multitask 'build_parallel' => ['build_a', 'build_z']

or

multitask :mytask => [:task1, :task2, :task3] do
  puts "Completed parallel execution of tasks 1 through 3."
end

My question:

What is the best way to build a global variable in one task that I can then use in my multitask? The following doesn't execute task1, task2, task3...which means the global $build_list is empty

$build_list = []
task :build do
   $build_list << 'task1' 
   $build_list << 'task2' 
   $build_list << 'task3'
   Rake::MultiTask[:build_parallel].invoke # or Rake::Task[:build_parallel].invoke
end

multitask :build_parallel => $build_list

Should I be using an ENV variable here or is some other method preferred?

like image 978
josh803316 Avatar asked Feb 12 '26 21:02

josh803316


2 Answers

The problem is not with the type of variable you choose, but with the fact that you're populating the variable during the execution of a task, while the dependency graph is created before any task is executed.

like image 126
On Freund Avatar answered Feb 15 '26 09:02

On Freund


Thanks to the previous response it led me to the solution:

Calculate the dynamic variable in a method outside the task before running any of the tasks in the dependency list.

# Generate the list in a method instead of a task
def get_list
  build_list = []
  build_list << 'task1' 
  build_list << 'task2' 
  build_list << 'task3'
end

# Make sure the list has been generated before the multitask call
@build_list  = get_list

# Then define the multitask list dependency
multitask :build_parallel => @build_list
like image 25
josh803316 Avatar answered Feb 15 '26 11:02

josh803316