rake pass parameters to dependent tasks - ruby

Here is the current way I run rak dependent tasks
task :test => [:prepare_testdir,:run_tests]
currently there is no parameters for these two dependent tasks. But I need to add parameters to one of tasks. It should be running like on command line
rake prepare_testdir[mydir]
How do I pass this new parameter to this
task :test => [:prepare_testdir,:run_tests]
I have tried
task :test => [:prepare_testdir[mydir],:run_tests]
and
task :test => [:prepare_testdir['mydir'],:run_tests]
both are not working.
Thanks in advance

Inside the rake file
task :test, [:dir] => [:prepare_testdir] do |t,args|
puts args.inspect # {:dir=>"foo"}
end
task :prepare_testdir, :dir do |t, args|
puts args.inspect # {:dir=>"foo"}
end
Invocation
rake test[foo]

Related

How to reference a function in a different rake file

I want to call a function that is in another rake file.
Rake File 1:
task :build => [:some_other_tasks] do
foo
end
def foo(type = :debug)
# ...
end
Rake File 2:
require_relative 'path_to_rake_file_1'
task :foo2 => [:some_other_tasks] do
foo
end
I am currently getting a no such file to load error despite confirming the path is absolutely correct.
Instead of defining methods inside rake files and sharing them among rake tasks, it is best practice to create a RakeHelper module and include it in your rake file. So, you could have something like:
rake_helper.rb
module RakeHelper
def self.foo
end
end
task1.rake
include RakeHelper
task :build => [:some_other_tasks] do
RakeHelper.foo
end
task2.rake
include RakeHelper
task :foo2 => [:some_other_tasks] do
RakeHelper.foo
end

Rake before task hook

Is there a straight forward way to modify a Rake task to run some bit of code before running the existing task? I'm looking for something equivalent to enhance, that runs at the beginning rather than the end of the task.
Rake::Task['lame'].enhance(['i_run_afterwards_ha_ha'])
You can use the dependency of Rake task to do that, and the fact that Rake allows you to redefine existing task.
Rakefile
task :your_task do
puts 'your_task'
end
task :before do
puts "before"
end
task :your_task => :before
As result
$ rake your_task
before
your_task
Or you could use the rake-hooks gem to do before and after hooks:
https://github.com/guillermo/rake-hooks
namespace :greetings do
task :hola do puts "Hola!" end ;
task :bonjour do puts "Bonjour!" end ;
task :gday do puts "G'day!" end ;
end
before "greetings:hola", "greetings:bonjour", "greetings:gday" do
puts "Hello!"
end
rake greetings:hola # => "Hello! Hola!"

How do I run multiple Rake tasks programmatically at once?

At the command line I can run multiple tasks like this
rake environment task1 task2 task3
How can I do this programmatically? I know that I can run one task like this
Rake::Task['task1'].invoke
You can call two tasks:
require 'rake'
task :task1 do |t|
p t
end
task :task2 do |t|
p t
end
Rake::Task["task1"].invoke
Rake::Task["task2"].invoke
I would prefer a new tast with prerequisites:
require 'rake'
task :task1 do |t|
p t
end
task :task2 do |t|
p t
end
desc "Common task"
task :all => [ :task1, :task2 ]
Rake::Task["all"].invoke
If I misunderstood your question and you want to execute the same task twice: You can reenable tasks:
require 'rake'
task :task1 do |t|
p t
end
Rake::Task["task1"].invoke
Rake::Task["task1"].reenable
Rake::Task["task1"].invoke
Make a rake task for it :P
# in /lib/tasks/some_file.rake
namespace :myjobs do
desc "Doing work, son"
task :do_work => :environment do
Rake::Task['resque:work'].invoke
start_some_other_task
end
def start_some_other_task
# custom code here
end
end
Then just call it:
rake myjobs:do_work

Can't pass args through to rake task - has the syntax changed?

I'm using a rails 3.1.3 project which has rake (0.9.2.2). I want to do this in a rake task: call it like
rake tale:import_kml /path/to/file.txt
and then inside the rake task, get access to "/path/to/file.txt" as args.filename
I thought i would be able to do this like so (the puts is there for bit of debugging):
namespace :tale do
desc "Expects to get a file or folder name as the first argument, and passes that to Tale.import_kml"
task(:import_kml, [:filename] => :environment) do |t, args|
puts "args = #{args.inspect}"
if File.exists?(args.filename)
Tale.import_kml(filename)
end
end
end
But, i get this:
** Invoke tale:import_kml (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute tale:import_kml
args = {}
rake aborted!
can't convert nil into String
so, filename isn't making it into args. I can't work out what i'm doing wrong here...
Try such snippet:
namespace :tale do
desc "Expects to get a file or folder ..."
task(:import_kml, [:filename]) do |t, args|
args.with_default(:filename => :environment)
puts "args = #{args.inspect}"
end
end
rake tale:import_kml[foo] # => args = {:filename => "foo"}
WarHog helped me work it out: i had to change the 'task' line to
task :import_kml, [:filename] => [:environment] do |t, args|
and call it like
rake tale:import_kml[/path/to/file.txt]

Alias of task name in Rake

When I need to alias some task's name, how should I do it?
For example, how do I turn the task name:
rake db:table
rake db:create
rake db:schema
rake db:migration
to:
rake db:t
rake db:c
rake db:s
rake db:m
Editing after getting the answer:
def alias_task(tasks)
tasks.each do |new_name, old_name|
task new_name, [*Rake.application[old_name].arg_names] => [old_name]
end
end
alias_task [
[:ds, :db_schema],
[:dc, :db_create],
[:dr, :db_remove]
]
Why do you need an alias? You may introduce a new task without any code, but with a prerequisite to the original task.
namespace :db do
task :table do
puts "table"
end
#kind of alias
task :t => :table
end
This can be combined with parameters:
require 'rake'
desc 'My original task'
task :original_task, [:par1, :par2] do |t, args|
puts "#{t}: #{args.inspect}"
end
#Alias task.
#Parameters are send to prerequisites, if the keys are identic.
task :alias_task, [:par1, :par2] => :original_task
To avoid to search for the parameters names you may read the parameters with arg_names:
#You can get the parameters of the original
task :alias_task2, *Rake.application[:original_task].arg_names, :needs => :original_task
Combine it to a define_alias_task-method:
def define_alias_task(alias_task, original)
desc "Alias #{original}"
task alias_task, *Rake.application[original].arg_names, :needs => original
end
define_alias_task(:alias_task3, :original_task)
Tested with ruby 1.9.1 and rake-0.8.7.
Hmmm, well, I see that's more or less exactly the same solution RyanTM already posted some hours ago.
Here is some code someone wrote to do it: https://gist.github.com/232966
def alias_task(name, old_name)
t = Rake::Task[old_name]
desc t.full_comment if t.full_comment
task name, *t.arg_names do |_, args|
# values_at is broken on Rake::TaskArguments
args = t.arg_names.map { |a| args[a] }
t.invoke(args)
end
end

Resources