在Rake中是否可以为命名空间设置别名?
我喜欢如何为任务设置别名:
task :commit => :c
我希望能够做类似这样的事情:
namespace :git => :g
在Rake中是否可以为命名空间设置别名?
我喜欢如何为任务设置别名:
task :commit => :c
我希望能够做类似这样的事情:
namespace :git => :g
With
task :commit => :c
你不需要定义别名,只需设置先决条件。
当你调用:commit
时,先决条件:c
会首先被调用。
只要有一个先决条件且:commit
没有自己的代码,它就看起来像是一个别名,但实际上并不是。
知道了这一点,如果你为命名空间定义了一个默认任务并为该任务设置了一个先决条件(而该先决条件可能是另一个命名空间的默认任务),那么你可以'别名'你的命名空间。
但我认为,没有必要给命名空间取别名。如果你为命名空间定义了一个默认任务,或许'别名'那个任务就足够了。
再次阅读问题后,我有一个基于Rake任务是否有“method_missing”方法?的替代想法:
require 'rake'
namespace :long_namespace do
task :a do |tsk|
puts "inside #{tsk.name}"
end
end
rule "" do |tsk|
aliastask = tsk.name.sub(/short:/, 'long_namespace:')
Rake.application[aliastask].invoke
end
Rake.application['short:a'].invoke
require 'rake'
namespace :long_namespace do
task :a do |tsk|
puts "inside #{tsk.name}"
end
end
rule "" do |tsk|
aliastask = tsk.name.sub(/short:/, 'long_namespace:')
if Rake.application.tasks.map{|tsk| tsk.name }.include?( aliastask )
Rake.application[aliastask].invoke
else
raise RuntimeError, "Don't know how to build task '#{tsk.name}'"
end
end
Rake.application['short:a'].invoke
Rake.application['short:undefined'].invoke
还有一种更通用的方法,使用新的方法aliasnamespace
来定义别名命名空间:
require 'rake'
#Extend rake by aliases for namespaces
module Rake
ALIASNAMESPACES = {}
end
def aliasnamespace(alias_ns, original_ns)
Rake::ALIASNAMESPACES[alias_ns] = original_ns
end
rule "" do |tsk|
undefined = true
Rake::ALIASNAMESPACES.each{|aliasname, origin|
aliastask = tsk.name.sub(/#{aliasname}:/, "#{origin}:")
if Rake.application.tasks.map{|tsk| tsk.name }.include?( aliastask )
Rake.application[aliastask].invoke
undefined = false
end
}
raise RuntimeError, "Don't know how to build task '#{tsk.name}'" if undefined
end
#And now the usage:
namespace :long_namespace do
task :a do |tsk|
puts "inside #{tsk.name}"
end
end
aliasnamespace :short, 'long_namespace'
Rake.application['short:a'].invoke
#~ Rake.application['short:undefined'].invoke
rake generate:migration
和rake g:migration
会很棒。 - Jerska