如何找到 rake 任务的源文件? [英] How do I find the source file for a rake task?

查看:37
本文介绍了如何找到 rake 任务的源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以通过输入查看所有可能的佣金任务

I know you can view all possible rake tasks by typing

rake -T

但我需要知道任务到底是做什么的.从输出中,如何找到实际具有该任务的源文件?例如,我正在尝试查找 db:schema:dump 任务的源.

But I need to know what exactly a task does. From the output, how can I find a source file that actually has the task? For example, I'm trying to find the source for the db:schema:dump task.

推荐答案

不管其他人怎么说,您可以以编程方式获取 rails 应用程序中 rake 任务的源位置.为此,只需在您的代码或控制台中运行类似以下内容:

Despite what others have said, you can programmatically get the source location of rake tasks in a rails application. To do this, just run something like the following in your code or from a console:

# load all the tasks associated with the rails app
Rails.application.load_tasks

# get the source locations of actions called by a task
task_name = 'db:schema:load' # fully scoped task name
Rake.application[task_name].actions.map(&:source_location)

这将返回为此任务执行的任何代码的源位置.您还可以使用 #prerequisites 而不是 #source_location 来获取先决任务名称列表(例如环境"等).

This will return the source locations of any code that gets executed for this task. You can also use #prerequisites instead of #source_location to get a list of prerequisite task names (e.g. 'environment', etc).

您还可以列出使用以下方法加载的所有任务:

You can also list all tasks loaded using:

Rake.application.tasks

更新:请参阅下面 Magne 的好回答.对于 rake >= 0.9.0 的版本,您可以使用 rake -W 来显示您的 rake 任务的源位置.

UPDATE: See Magne's good answer below. For versions of rake >= 0.9.0 you can use rake -W to show the source location of your rake tasks.

这篇关于如何找到 rake 任务的源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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