Rake中任务名称的别名 [英] Alias of task name in Rake

查看:86
本文介绍了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

到:

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

这可以结合参数:

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

为了避免搜索参数名称,您可以使用 arg_names 读取参数:

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

将其与 define_alias_task 方法结合:

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)

使用 ruby​​ 1.9.1 和 rake-0.8.7 测试.

Tested with ruby 1.9.1 and rake-0.8.7.

嗯,嗯,我看到这或多或少完全相同几小时前 RyanTM 已经发布了解决方案.

Hmmm, well, I see that's more or less exactly the same solution RyanTM already posted some hours ago.

这篇关于Rake中任务名称的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆