Alias ​​name Rake Namespace

Is a namespace alias possible in Rake?


I like the way you can perform aliases:

task :commit => :c 

I would like to do something like this:

 namespace :git => :g 
+4
source share
1 answer

WITH

 task :commit => :c 

you do not define an alias, you set the necessary condition. When you call :commit , prerequsite :c is called first. While there is only one prerequsite, and :commit does not contain its own code, it may look like an alias, but it is not.

Knowing that you can "alias" your namespace if you define a default task for your namespace and set the necessary condition for this task (and a default condition for another namespace to be set again).

But I think there is no need for namespace aliases. It would be enough if you define a default job for the names of the names and possibly the "alias" of this task.


After reading the question again, I have an alternative idea based on Is there a method_missing method for rake tasks? :

 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 

The rule defines the task_missing rule and tries to replace the namespace (in the example, it replaces "short" with "long_namespace").

Disadvantage: the undefined task does not return an error. So you need an adapted version:

 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 

And a more generalized version with the new aliasnamespace method for defining alias namespaces:

 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 
+5
source

Source: https://habr.com/ru/post/1385576/


All Articles