从任务内访问 Rake 任务描述

发布于 2024-12-25 17:36:10 字数 144 浏览 1 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若无相欠,怎会相见 2025-01-01 17:36:10

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

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 (我只检查了这两个版本)。

具有自适应功能的独立 ruby​​ 脚本:

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)是一个检查

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

默认 false 在第 305 行设置

.comment 与 .full_comment

对于那些想知道的人,.comment 打印第一行并.full_comment 将打印所有行(对于多行 desc),并在返回的字符串中用 \n 指示新行。

请参阅此处的文档:

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

Edit:
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).

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

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

The default false is set in line 305.

.comment vs .full_comment

For those wondering, .comment prints the first line and .full_comment will print all lines (for multi-line desc), with new lines indicated with \n in the returning String.

See the docs here:

謸气贵蔟 2025-01-01 17:36:10

遇到类似的问题,我想向用户显示定制的帮助屏幕。这里的答案对我帮助很大。

这一点非常重要

Rake::TaskManager.record_task_metadata = true

在第一次定义任务之前完成

。然后你可以

Rake.application.tasks.each do |t|
    printf("%-}s  # %s\n",
           t.name_with_args,
           t.comment)
  end

通过调查 https:/ 来找到详细信息/github.com/jimweirich/rake/blob/master/lib/rake/application.rb#L284

Having a similar problem, that I wanted to show the user a customized help screen. The answer here helped me a lot.

It is very important that

Rake::TaskManager.record_task_metadata = true

is done before the first definition of tasks.

Then you can do

Rake.application.tasks.each do |t|
    printf("%-}s  # %s\n",
           t.name_with_args,
           t.comment)
  end

Details can be found by investigating https://github.com/jimweirich/rake/blob/master/lib/rake/application.rb#L284

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文