[英]A rake task by .rake file
I have the following Rakefile in a Ruby 1.9.3 project:我在 Ruby 1.9.3 项目中有以下 Rakefile:
require 'rake/testtask'
require 'json'
Rake::TestTask.new do |t|
t.pattern = "spec/**/*_spec.rb"
t.verbose = true
end
task :default => :test
namespace :omglol do
namespace :file_a do
task :foo do
# do some stuff
end
end
namespace :file_b do
task :bar do
# do some stuff
end
end
end
As you can see, the first part of this file allow to run tests, just using rake
command.如您所见,该文件的第一部分允许运行测试,只需使用
rake
命令即可。 And the second part contains some tasks.第二部分包含一些任务。
Actually, I have a lot of tasks inside omglol:file_a and omglol:file_b namespaces.实际上,我在 omglol:file_a 和 omglol:file_b 命名空间中有很多任务。 That's why I would like to move each of them inside a file, for instance
tasks/omglol/file_a.rake
and tasks/omglol/file_b.rake
.这就是为什么我想将它们中的每一个都移动到一个文件中,例如
tasks/omglol/file_a.rake
和tasks/omglol/file_b.rake
。
Is there a best way to do so?有没有最好的方法? Thanks.
谢谢。
Yes.是的。 Simply move the logic into the appropriate files and then
require
them.只需将逻辑移动到适当的文件中,然后
require
它们。
Example Rakefile:示例 Rakefile:
require 'rake/testtask'
require 'json'
require 'lib/tasks/omglol/file_a.rake' # <= contains your subtasks
Rake::TestTask.new do |t|
t.pattern = "spec/**/*_spec.rb"
t.verbose = true
end
task :default => :test
Then in lib/tasks/omglol/file_a.rake
simply define your tasks as normal:然后在
lib/tasks/omglol/file_a.rake
简单地定义你的任务:
namespace :omglol do
namespace :file_a do
task :foo do
# do some stuff
end
end
end
The pattern would be the same for file_b.rake.该模式与 file_b.rake 相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.