tags:

views:

170

answers:

2

Let's say I have a target who needs to compile some files. That target has another target as a prerequisite, one that obtains the files. Let's say this:

task :obtain do
  # obtain files from somewhere
end

task :compile => :obtain do
  # do compilation
end

Let's say that the :obtain target doesn't always places the files in the same folder. How would I pass :compile the path that :obtain found? Environment variables?

+1  A: 

One way would be to store it in a global variable:

task :obtain do
  $obtained_dir = "/tmp/obtained"
end

task :compile => :obtain do
  puts "compiling files in #{$obtained_dir}"
end

Instance variables (i.e. @obtained_dir) should also work.

Another way would be to pull the "obtain" code into a method, as follows:

task :obtain do
  obtain_files
end

task :compile do
  obtained_dir = obtain_files
  puts "compiling files in #{obtained_dir}"
end

def obtain_files
  #obtain files from somewhere
  "/tmp/obtained_files"
end
Greg Campbell
+1  A: 

Using ENV['something'] is in my opinion preferable, because if you do it this way (as opposed to $global or @instance variables) you can treat those as task arguments, and use the sub task from commandline easily.

On the other hand if you keep your code in separate classes / modules / methods, you will not even have to deal with those sorts of hacks + your code will be more testable.

psyho
Yeah. This is how I ended up doing it. Thanks!
Geo