从任务中访问 Rake 任务描述 [英] Access Rake Task Description from within Task

查看:62
本文介绍了从任务中访问 Rake 任务描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 rake 任务中如何查询描述?会给予的东西:

Within a rake task how does one query the description? Something that would give:

desc "Populate DB"
task populate: :environment do
  puts task.desc # "Populate DB"
end

推荐答案

task 必须定义为任务块的参数.

taskmust be defined as a parameter for the task-block.

desc "Populate DB"
task :populate do |task|
  puts task.comment # "Populate DB"
  puts task.full_comment # "Populate DB"
  puts task.name # "populate "
end

此解决方案适用于 rake 0.8.7.至少 rake 0.9.2.2 需要额外的 Rake::TaskManager.record_task_metadata = true(我只检查了这两个版本).

This solution works with rake 0.8.7. At least rake 0.9.2.2 need an additional Rake::TaskManager.record_task_metadata = true (I checked only this two versions).

具有适应性的独立 ruby​​ 脚本:

A stand alone ruby-script with adaption:

gem 'rake'    #'= 0.9.2.2'
require 'rake'

#Needed for rake/gem '= 0.9.2.2'
Rake::TaskManager.record_task_metadata = true

desc "Populate DB"
task :populate do |task|
  p task.comment # "Populate DB"
  p task.full_comment # "Populate DB"
  p task.name # "populate "
end

if $0 == __FILE__
  Rake.application['populate'].invoke()  #all tasks
end

原因:在rake/task_manager.rb第30行(rake 0.9.2.2)是一个检查

Reason: in rake/task_manager.rb line 30 (rake 0.9.2.2) is a check

  if Rake::TaskManager.record_task_metadata
    add_location(task)
    task.add_description(get_description(task))
  end

默认的 false 设置在第 305 行.

The default false is set in line 305.

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

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