简体   繁体   English

如何找到rake任务的源文件?

[英]How do I find the source file for a rake task?

I know you can view all possible rake tasks by typing 我知道您可以通过键入来查看所有可能的rake任务

rake -T

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. 例如,我正在尝试查找db:schema:dump任务的源代码。

I know this is an old question, but in any case: 我知道这是一个老问题,但无论如何:

rake -W

This was introduced in rake 0.9.0. 这是在rake 0.9.0中引入的。

http://rake.rubyforge.org/doc/release_notes/rake-0_9_0_rdoc.html http://rake.rubyforge.org/doc/release_notes/rake-0_9_0_rdoc.html

Support for the –where (-W) flag for showing where a task is defined. 支持-where(-W)标志,用于显示定义任务的位置。

Despite what others have said, you can programmatically get the source location of rake tasks in a rails application. 尽管其他人已经说过,您可以通过编程方式在rails应用程序中获取rake任务的源位置。 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)

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 (eg 'environment', etc). 您还可以使用#prerequisites而不是#source_location来获取必备任务名称列表(例如“环境”等)。

You can also list all tasks loaded using: 您还可以列出使用以下加载的所有任务:

Rake.application.tasks

UPDATE: See Magne's good answer below. 更新:请参阅下面的Magne的好答案。 For versions of rake >= 0.9.0 you can use rake -W to show the source location of your rake tasks. 对于rake> = 0.9.0的版本,您可以使用rake -W来显示rake任务的源位置。

There is no programmatic way to do this unfortunately. 不幸的是,没有程序化的方法来做到这一点。 Rake tasks can be loaded either from rails itself, lib/tasks, or from any plugin with a tasks directory. Rake任务可以从rails本身,lib / tasks或任何带有tasks目录的插件加载。

This should nab most everything not within Rails itself: 这应该抓住Rails本身内的大部分内容:

find . -name "*.rake" | xargs grep "whatever"

As for db:schema:dump , here's the source: 至于db:schema:dump ,这里是源代码:

desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
task :dump => :environment do
  require 'active_record/schema_dumper'
  File.open(ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb", "w") do |file|
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
  end
end

It can be found on line 242 of lib/tasks/database.rake in the rails 2.2.2 gem. 它可以在rails 2.2.2 gem中的lib / tasks / database.rake的第242行找到。 If you've got a different version of Rails, just search for " namespace :schema ". 如果你有不同版本的Rails,只需搜索“ namespace :schema ”。

You probably actually want the source of the ActiveRecord::SchemaDumper , but I think you should have no trouble figuring out where that is. 你可能真的想要ActiveRecord::SchemaDumper的来源,但我认为你应该毫不费力地找出它的位置。 :-) :-)

For most rake tasks in Rails, look in the Rails gem directory, in lib/tasks. 对于Rails中的大多数rake任务,请查看lib / tasks中的Rails gem目录。

If you've vendored Rails into your app directory structure then look in vendor/rails/railties/lib/tasks instead 如果您已将Rails出售到您的应用程序目录结构中,那么请查看vendor / rails / railties / lib / tasks

Either way, db:schema:dump is in databases.rake. 无论哪种方式,db:schema:dump都在databases.rake中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM