I am trying to do an iteration with rake tasks, but when I use this example, the output show me always the first item inside my list. I want to reuse the task :create
. Here is my code:
namespace :devops do
desc "Task description"
task :prepend do
%W[config logs].each do | folder |
task("devops:create").invoke("#{folder}")
end
end
desc "Create the folder structure"
task :create, [ :name ] do |t, args|
puts "Creating the structure for <#{args[:name]}> folder"
end
end
Here is the rake execution:
rake devops:prepend
Here is the output:
Creating the structure for <config> folder
It always takes the first argument, why? It is something like the task invoke
breaks the each cycle? I don't know... any tips?
Ok, I keep searching and I found that I miss a peace of code. To run a same task several time I need to do this:
task("devops:create").reenable
So the complete example will be:
namespace :devops do
desc "Task description"
task :prepend do
%W[config logs].each do | folder |
task("devops:create").invoke("#{folder}")
task("devops:create").reenable
end
end
desc "Create the folder structure"
task :create, [ :name ] do |t, args|
puts "Creating the structure for <#{args[:name]}> folder"
end
end
And the result is:
Creating the structure for <config> folder
Creating the structure for <logs> folder