【问题标题】:How to know which task is being executed with rake如何知道正在使用 rake 执行哪个任务
【发布时间】:2015-04-22 06:12:30
【问题描述】:

我想从 rake 中知道正在执行的任务的名称是什么?这个怎么做?例如,在下面的代码中,当我运行rake my_incredible_task 时,它应该打印“my_incredible_task”:

  task :boot do
    task_name = <what comes here?>
    puts task_name
  end

  task :my_incredible_task => [:boot] do
    #do some stuff
  end

【问题讨论】:

    标签: ruby rake rake-task


    【解决方案1】:

    我不确定是否有开箱即用的方法,但是您可以执行以下操作:

    require 'rake'
    
    module Rake
      class Application
        attr_accessor :current_task_name
      end
    
      class Task
        alias :old_execute :execute
        def execute(args=nil)
          Rake.application.current_task_name = @name
          old_execute(args)
        end
      end
    end
    
    
    namespace :so do
    
      task :my_task do
        puts Rake.application.current_task_name
      end
    end
    

    不确定这将如何处理并行运行的任务...

    【讨论】:

      【解决方案2】:

      您可以在块中使用参数:

      task :my_incredible_task do |t|
          puts t.name
      end
      

      编辑

      你可以使用方法来代替:

        task :boot do |t|
          boot(t.name)
        end
      
        task :my_incredible_task do |t|
          boot(t.name)
          #do some stuff
        end
      
      def boot task_name
         # do stuff
      end
      

      【讨论】:

      • 我稍微改变了问题以反映真正的问题。
      猜你喜欢
      • 2018-12-05
      • 2016-06-03
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 2018-07-13
      相关资源
      最近更新 更多