本文的例子来源于Ruby on Rails Rake Tutorial
Rake是make衍生出来的一个工具。
像make一样,它需要一个Rakefile的文件来做为默认的任务定制文件。在这个文件里可以指定一些任务,由rake来解释执行。
task :purchaseAlcohol do
puts "Purchased Vodka"
end
task :mixDrink do
puts "Mixed Fuzzy Navel"
end
task :getSmashed do
puts "Dood, everthing's blurry, can I halff noth'r drinnnk?"
end
然后,想make一样,在执行rake时,后面加上任务的名字
$ rake purchaseAlcohol
Purchased Vodka
$ rake mixDrink
Mixed Fuzzy Navel
$ rake getSmashed
Dood, everthing's blurry, can I halff noth'r drinnnk?
在高深一点儿就是,制定任务的顺序,也就是依赖性
task :purchaseAlcohol do
puts "Purchased Vodka"
end
task :mixDrink => :purchaseAlcohol do
puts "Mixed Fuzzy Navel"
end
task :getSmashed => :mixDrink do
puts "Dood, everthing's blurry, can I halff noth'r drinnnk?"
end
要得到当前的Rakefile里面的任务列表很简单
rake -tasks
rake -T
看到这里也许你会想起在rails里面做数据的migrate的时候,很多task都有一个前缀,像
rake db:create:all
这里解释一下,db是一个命名空间(namespace),也就是对一些任务进行分组。
namespace :alcoholic do
desc "This task will purchase your Vodka"
task :purchaseAlcohol do
puts "Purchased Vodka"
end
desc "This task will mix a good cocktail"
task :mixDrink => :purchaseAlcohol do
puts "Mixed Fuzzy Navel"
end
desc "This task will drink one too many"
task :getSmashed => :mixDrink do
puts "Dood, everthing's blurry, can I halff noth'r drinnnk?"
end
end
Rake 的文档和例子 [url]http://docs.rubyrake.org/[/url]