有没有办法知道当前的 rake 任务? [英] Is there a way to know the current rake task?

查看:54
本文介绍了有没有办法知道当前的 rake 任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以知道ruby中当前的rake任务:

Is it possible to know the current rake task within ruby:

# Rakefile
task :install do
  MyApp.somemethod(options)
end

# myapp.rb
class MyApp
  def somemetod(opts)
     ## current_task?
  end
end


编辑

我在询问可以查询的任何环境|全局变量,因为我想让应用程序在 rake 方面变得智能,而不是修改任务本身.我正在考虑让应用在通过 rake 运行时表现出不同的行为.


Edit

I'm asking about any enviroment|global variable that can be queried about that, because I wanted to make an app smart about rake, not modify the task itself. I'm thinking of making an app behave differently when it was run by rake.

推荐答案

我正在考虑让应用在由 rake 运行时表现不同.

I'm thinking of making an app behave different when runned by rake.

检查caller是否已经足够了,如果它是从rake调用的,或者你还需要哪个任务?

Is it already enough to check caller, if it is called from rake, or do you need also which task?

我希望,没关系,当您可以修改 rakefile 时.我有一个引入 Rake.application.current_task 的版本.

I hope, it is ok, when you can modify the rakefile. I have a version which introduce Rake.application.current_task.

# Rakefile
require 'rake'
module Rake
  class Application
    attr_accessor :current_task
  end
  class Task
    alias :old_execute :execute 
    def execute(args=nil)
      Rake.application.current_task = @name  
      old_execute(args)
    end
  end #class Task
end #module Rake  

task :start => :install do; end
task :install => :install2 do
  MyApp.new.some_method()
end
task :install2 do; end

# myapp.rb
class MyApp
  def some_method(opts={})
    ## current_task? -> Rake.application.current_task
    puts "#{self.class}##{__method__} called from task #{Rake.application.current_task}"
  end
end

关于它的两个评论:

  • 您可以在文件中添加 rake 修改并在您的 rakefile 中要求它.
  • 启动和安装任务是要测试的测试任务,如果有多个任务.
  • 我只对副作用进行了小规模测试.我可以想象在实际生产环境中会出现问题.

这篇关于有没有办法知道当前的 rake 任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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