rake 任务在哪里定义的?

发布于 2024-10-13 02:26:45 字数 304 浏览 5 评论 0原文

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

  • rake test
  • rake db:migrate
  • 等等

问题是,这些任务在哪里描述?默认的 Rakefile 不具备所有这些任务。

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

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

  • rake test
  • rake db:migrate
  • etc

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

Furthermore, I checked out some project that uses rspec and I am able to run rake spec to run all the tests. Where does the spec target defined?

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

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

发布评论

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

评论(7

别再吹冷风 2024-10-20 02:26:45

如果描述你的意思是定义,那么 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)>'

今天才发现这个:)

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

$ 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 :)

半葬歌 2024-10-20 02:26:45

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

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

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

Rake tasks are automatically loaded from the folder structure 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

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.

偏爱你一生 2024-10-20 02:26:45

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

启动 Rails 控制台:

rails c

然后运行这些命令:

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

根据 Sameers 的评论,对于 rake v 10.1.0 以及可能较旧版本的 rake,您可能需要调用:
tsk.actions
而不是
tsk.地点

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

Start a rails console:

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.

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

坦然微笑 2024-10-20 02:26:45

您没有指定您使用的 Rails 版本,但在 3.0.7 中,db 任务位于

lib/active_record/railties/databases.rake

更新中的 ActiveRecord gem 中:

从 Rails 版本 3.2.7 开始,任务仍然位于我上面已经说过了。

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

lib/active_record/railties/databases.rake

Update:

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

迷你仙 2024-10-20 02:26:45

在 Rails 3 中,railties gem 定义了很多 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

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

gem install open_gem
gem open railties

In Rails 3 the railties gem defines a lot of rake tasks.

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 $EDITOR is configured, you can easily see them yourself with the open_gem gem:

gem install open_gem
gem open railties
南风几经秋 2024-10-20 02:26:45

列出所有任务:

rake -P

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

To list all tasks:

rake -P

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

戏蝶舞 2024-10-20 02:26:45

您签出的项目可能使用rspec-rails gem。该 gem 定义了 spec 任务。您可以在此处查看其源代码:

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

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

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

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