Ruby-on-rails 耙任务在哪里定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4742930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:09:19  来源:igfitidea点击:

Where are rake tasks defined?

ruby-on-railsrubyrakerakefile

提问by ryanprayogo

On a freshly created Rails project (generated by rails someName), one can run some 'default' rake tasks like:

在新创建的 Rails 项目(由 生成rails someName)中,可以运行一些“默认”的 rake 任务,例如:

  • rake test
  • rake db:migrate
  • etc
  • rake test
  • rake db:migrate
  • 等等

Question is, where does these tasks get described? The default Rakefiledoesn't have all these tasks.

问题是,这些任务在哪里得到描述?默认Rakefile没有所有这些任务。

Furthermore, I checked out some project that uses rspecand I am able to run rake specto run all the tests. Where does the spectarget defined?

此外,我检查了一些使用的项目,rspec并且我能够运行rake spec以运行所有测试。spec目标在哪里定义?

采纳答案by DanneManne

Rake tasks are automatically loaded from the folder structure lib/tasks/*.rake

Rake 任务会自动从文件夹结构中加载 lib/tasks/*.rake

When we are talking about the task db:migrate for example, it is located within the rails gem in lib/tasks/databases.rake

例如,当我们谈论任务 db:migrate 时,它​​位于 Rails gem 中 lib/tasks/databases.rake

So for a specific project, you will always have the tasks within the project folder structure as well as all tasks within the specified gems.

因此,对于特定项目,您将始终拥有项目文件夹结构中的任务以及指定 gem 中的所有任务。

回答by Adam Groves

If by described you mean defined, rake -W is your friend. Example:

如果描述的意思是定义的,rake -W 是你的朋友。例子:

$ rake -W db:create

=>

=>

rake db:create  /path/to/ruby/gems/1.9.1/gems/activerecord-3.1.11/lib/active_record/railties/databases.rake:39:in `block in <top (required)>'

Just found this out today :)

今天才发现:)

回答by jpgeek

To find the specific files and line numbers where a task is defined and/or modified, do this:

要查找定义和/或修改任务的特定文件和行号,请​​执行以下操作:

Start a rails console:

启动一个 rails 控制台:

rails c

Then run these commands:

然后运行这些命令:

require 'rake'
Rake::TaskManager.record_task_metadata=true
Rake.application.load_rakefile
tsk = Rake.application.tasks.find {|t| t.name =='my_task_name'}
tsk.locations

Rake basically can track the locations internally and has a nifty method to show them upon request. The above code basically loads rake, tells Rake to track the file locations, loads the Rakefile (and all other included ones), finds the task in question, and calls the locations method on it.

Rake 基本上可以在内部跟踪位置,并有一个漂亮的方法可以根据要求显示它们。上面的代码基本上加载 rake,告诉 Rake 跟踪文件位置,加载 Rakefile(和所有其他包含的文件),找到有问题的任务,并调用它的locations 方法。

From sameers comment, for rake v 10.1.0 and possibly older versions of rake you might have to call: tsk.actions instead of tsk.locations

来自相同评论,对于 rake v 10.1.0 和可能的旧版本 rake,您可能必须调用:tsk.actions 而不是 tsk.locations

回答by mraaroncruz

You didn't specify which version of rails you're using but in 3.0.7 the dbtasks are located in the ActiveRecord gem in

您没有指定您使用的是哪个版本的 rails,但在 3.0.7 中,db任务位于 ActiveRecord gem 中

lib/active_record/railties/databases.rake

Update:

更新:

As of rails version 3.2.7, the tasks are still where I stated above.

从 rails 版本 3.2.7 开始,任务仍然是我上面提到的。

回答by AlexChaffee

In Rails 3 the railtiesgem defines a lot of rake tasks.

在 Rails 3 中,railtiesgem 定义了很多 rake 任务。

railties-3.2.5/lib/rails/tasks/annotations.rake
railties-3.2.5/lib/rails/tasks/documentation.rake
railties-3.2.5/lib/rails/tasks/engine.rake
railties-3.2.5/lib/rails/tasks/framework.rake
railties-3.2.5/lib/rails/tasks/log.rake
railties-3.2.5/lib/rails/tasks/middleware.rake
railties-3.2.5/lib/rails/tasks/misc.rake
railties-3.2.5/lib/rails/tasks/routes.rake
railties-3.2.5/lib/rails/tasks/statistics.rake
railties-3.2.5/lib/rails/tasks/tmp.rake
railties-3.2.5/lib/rails/test_unit/testing.rake

If your $EDITORis configured, you can easily see them yourself with the open_gemgem:

如果您$EDITOR已配置,您可以使用open_gemgem轻松查看它们:

gem install open_gem
gem open railties

回答by luigi7up

To list all tasks:

列出所有任务:

rake -P

Since many tasks come from gems you install it's hard to know which ones are added...

由于许多任务来自您安装的宝石,因此很难知道添加了哪些...

回答by David Grayson

The project you checked out probably uses the rspec-railsgem. That gem defines the spectask. You can see the source code for it here:

您签出的项目可能使用了rspec-railsgem。那颗宝石定义了spec任务。你可以在这里看到它的源代码:

https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/tasks/rspec.rake

https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/tasks/rspec.rake