Rake任务构建工具

Rake的意思是Ruby Make,一个用ruby开发的代码构建工具。
可以把rake 作为一个任务管理工具来使用。

几个关键点

  • 命名空间 # namespace
  • desc注释 # rake -T <=> rake –tasks
  • 参数传递 # ENV[‘arg1’] rake xxx arg1=’123’
  • 默认task # task :default => ‘xxx’
  • 任务依赖关系 # task :xx1 => [:xx2, xx3]
  • 调用(调取)task # Rake::Task[‘xxx’].invoke
  • rake 会在当前路径下寻找名为Rakefile/rakefile/Rakefile.rb和rakefile.rb的文件执行
  • rake -T # 显示desc注释的任务
  • task是Rake最重要的方法,它的方法定义是 :task(args, &block),任务体是一个block(同ruby)
  • 依次执行 rake t1 t2 # rake make_coffee walk_dog
task :turn_off_alarm do
 puts "Turned off alarm. Would have liked 5 more minutes, though."
end
 
task :groom_myself do
 puts "Brushed teeth."
 puts "Showered."
 puts "Shaved."
end
 
task :make_coffee do
 cups = ENV["COFFEE_CUPS"] || 2                   ## 参数传递
 puts "Made #{cups} cups of coffee. Shakes are gone."
end
 
task :walk_dog do
 puts "Dog walked."
end
 
task :ready_for_the_day => [:turn_off_alarm, :groom_myself, :make_coffee, :walk_dog] do
 puts "Ready for the day!"
end

namespace :morning do                              ## 命名空间
  desc " 关闹钟 "                                   ## 注释,添加以后可以通过 rake -T 看到具体任务
  task :turn_off_alarm do
    puts "关掉闹钟,不过我想再睡#{ENV['delay'] || 5}分钟。"
  end
end

task :default => 'morning:turn_off_alarm'           ## 默认 task

namespace :afternoon do
  desc " 下午茶 "
  task :make_coffee do
    Rake::Task[:make_coffee].invoke                ## 调取 task
    puts "调用了英文做了#{ENV['COFFEE_CUPS'] || 2}杯下午茶。"
  end
end

### rake 会在当前路径下寻找名叫Rakefile、rakefile、Rakefile.rb和rakefile.rb的rake文件执行脚本。
### rake -T <=> rake --tasks
### task是Rake最重要的方法.它的方法定义是:task(args, &block).任务体是一个block

·