26

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]
]
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
coolesting
  • 1,451
  • 5
  • 18
  • 22

2 Answers2

45

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.

knut
  • 27,320
  • 6
  • 84
  • 112
  • I know this method, but it cannot pass the parameter. – coolesting Oct 05 '11 at 13:00
  • I'm quite sure it can. Wait some minutes, I will prepare an example. – knut Oct 05 '11 at 17:52
  • 1
    I added my example - and detected it's the already posted answer. But perhaps my explanation helps to understand (to be honest, I didn' understand Ryans answer until I made the same answer ;) – knut Oct 05 '11 at 18:15
  • Your example is the better than the first answer that will be showed the alias task name in command `rake -T`, yours are not. – coolesting Oct 06 '11 at 02:53
  • I made two little changes. Now you get the description with -T (You get the information 'alias of...'). RyanTMs answer uses the description of the original task. – knut Oct 06 '11 at 07:17
  • Naming your task's alias `alias_task` and then describing an `alias_task()` method to create aliases is super confusing. Instead, name them something like `actual_task` and then call the alias `alias_for_actual_task`. – Joshua Pinter Jan 22 '18 at 01:28
  • FYI, if you need to alias to a task that is inside of a `namespace`, just use a String like this: `task :top_level_alias => "db:table"` – Joshua Pinter Aug 11 '21 at 14:53
  • Newer syntax, for rubocop: https://stackoverflow.com/a/55930154/4722345 – JBallin Feb 11 '22 at 22:03
5

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
ryantm
  • 8,217
  • 6
  • 45
  • 57