Вопрос

I tried invoke, but I don't think it's going to help. Here is a short demo of what I am try to achieve.

task :first_task  => :dep do 
   puts "something else"

end

task :dep do
   puts "something"
end

If I run

rake first_task

from the command-line, the dependency gets satisfied and then the first_task gets executed.

 something
 something else

But what if I have a value, say dir = "testdir" in :dep that I want to pass as an argument to :first_task? Hope that makes sense.

I thought google would come up with something, but nope. Thanks a lot :)

Это было полезно?

Решение

Not sure if you can set or pass a parameter, but you can use instance variables:

task :first_task  => :dep do
   puts "something else"
   puts @dir
end

task :dep do
  puts "something"
  @dir = "Foo"
end

Or just local variables:

dir = "default_dir"

task :first_task  => :dep do
   puts "something else"
   puts dir
end

task :dep do
  puts "something"
  dir = "Foo"
end

For some kinds of task, it may make more sense to set environment variables:

task :first_task  => :dep do
   puts "something else"
   puts ENV['dir']
end

task :dep do
  puts "something"
  ENV['dir'] = "Foo"
end

Другие советы

It doesn't fit completely to your question, but is may show another approach for parameters with rake and forwarding them to child tasks. Rake tasks may get parameters.

An example:

task :first_task, [:par1, :par2] do |t, args|
  puts "#{t}: #{args.inspect}"
end

You can call this task with rake first_task[1,2] The result will be:

first_task: {:par1=>1, :par2=>2}

This parameters are forwarded to child tasks, if they share the same key. Example:

task :first_task, [:par1, :par2] => :second_task do |t, args|
  puts "#{t}: #{args.inspect}"
end

task :second_task, :par2 do |t, args|
  puts "#{t}: #{args.inspect}"
end

The result:

second_task: {:par2=>2}
first_task: {:par1=>1, :par2=>2}  
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top