Ruby-on-rails 覆盖 rails 的默认 rake 任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8112074/
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
Overriding rails' default rake tasks
提问by Max Williams
I have a Rails 2.2 project in which I want to override the functionality of the rake db:test:preparetask. I thought this would work, but it doesn't:
我有一个 Rails 2.2 项目,我想在其中覆盖rake db:test:prepare任务的功能。我认为这会起作用,但它不起作用:
#lib/tasks/db.rake
namespace :db do
namespace :test do
desc "Overridden version of rails' standard db:test:prepare task since the schema dump used in that can't handle DB enums"
task :prepare => [:environment] do
puts "doing db:structure:dump"
Rake::Task['db:structure:dump'].invoke
puts "doing db:test:clone_structure"
Rake::Task['db:test:clone_structure'].invoke
end
end
end
I get the standard task's behaviour. If I change the name of the task to :prepare2and then do rake db:test:prepare2, then it works fine. The natural conclusion I draw from this is that my rake tasks are being defined beforethe built-in Rails ones, so mine is overridden by the standard :preparetask.
我得到了标准任务的行为。如果我将任务名称更改为:prepare2然后执行rake db:test:prepare2,则它可以正常工作。我从中得出的自然结论是,我的 rake 任务是在内置 Rails任务之前定义的,所以我的:prepare任务被标准任务覆盖了。
Can anyone see how I can fix this? I'd rather override it than have to use a new task. Thanks, max
谁能看到我如何解决这个问题?我宁愿覆盖它也不必使用新任务。谢谢,最大
回答by Brendon Muir
If you define a rake task that already exists, its execution gets appended to the original task's execution; both tasks will be executed.
如果你定义了一个已经存在的 rake 任务,它的执行会附加到原始任务的执行中;这两个任务都将被执行。
If you want to redefine a task you need to clear the original task first:
如果你想重新定义一个任务,你需要先清除原来的任务:
Rake::Task["db:test:prepare"].clear
It's also useful to note that once a task has been executed in rake, it won't execute again even if you call it again. This is by design but you can call .reseton a task to allow it to be run again.
需要注意的是,一旦任务在 rake 中执行,即使您再次调用它也不会再次执行。这是设计使然,但您可以调用.reset任务以允许它再次运行。
回答by Alex Peattie
You have to remove the default task before adding your own:
在添加自己的任务之前,您必须删除默认任务:
Rake.application.instance_variable_get('@tasks').delete('db:test:prepare')
namespace 'db' do
namespace 'test' do
task 'prepare' do
# ...
end
end
end
A fairly popular idiomis to create a convenience method called remove_tasklike so:
一个相当流行的习惯用法是创建一个方便的方法,remove_task如下所示:
Rake::TaskManager.class_eval do
def remove_task(task_name)
@tasks.delete(task_name.to_s)
end
end
def remove_task(task_name)
Rake.application.remove_task(task_name)
end
回答by Eric Guo
Create a new project.rakefile at lib/tasks/, and paster below code into it.
在lib/tasks/创建一个新的project.rake文件,并将下面的代码粘贴到其中。
namespace :mv do
desc "Display hint and info for your rails 4 project"
task info: :environment do
puts 'Run rake test to test'
end
end
task(:default).clear.enhance ['mv:info']
inspired by Krasimir Angelov's blog

