문제

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