3

Is there some way to access a variable defined in a prerequisite? e.g.

task :new_file do
    filename = 'foobar.txt' # in reality I ask the user for the filename
    File.write(filename, 'Some glorious content')
end

task :new_file! => [:new_file] do
    exec "vim #{filename.inspect}"
end

I'd like rake new_file! to simply be a shorthand for rake new_file along with launching vim for whatever file I created in the new_file task.

All I can think of is populating a global variable FILENAME in :new_file and using it in new_file! and then clearing it, but if there is a "more Rake" way to do it, I'd like to know.

mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194

1 Answers1

0

One way would be to define the variable outside the tasks like this:

filename = 'default.name'

task :new_file do
    filename = 'foobar.txt' # in reality I ask the user for the filename
    File.write(filename, 'Some glorious content')
end

task :new_file! => [:new_file] do
    # filename will be visible here too, and its value was set in new_file
    exec "vim #{filename.inspect}"
end
Xian Nox
  • 368
  • 2
  • 11