rake 任务在哪里定义的?
在新创建的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果描述你的意思是定义,那么 rake -W 是你的朋友。示例:
=>
今天才发现这个:)
If by described you mean defined, rake -W is your friend. Example:
=>
Just found this out today :)
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.
要查找定义和/或修改任务的特定文件和行号,请执行以下操作:
启动 Rails 控制台:
然后运行这些命令:
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:
Then run these commands:
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
您没有指定您使用的 Rails 版本,但在 3.0.7 中,
db
任务位于更新中的 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 inUpdate:
As of rails version 3.2.7, the tasks are still where I stated above.
在 Rails 3 中,
railties
gem 定义了很多 rake 任务。如果您的
$EDITOR
已配置,您可以使用open_gem
gem 轻松查看它们:In Rails 3 the
railties
gem defines a lot of rake tasks.If your
$EDITOR
is configured, you can easily see them yourself with theopen_gem
gem:列出所有任务:
由于许多任务来自您安装的 gem,因此很难知道添加了哪些任务...
To list all tasks:
Since many tasks come from gems you install it's hard to know which ones are added...
您签出的项目可能使用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