Advertisement
Advertisement

More Related Content

Advertisement

Rake: The Familiar Stranger

  1. Rake The Familiar Stranger Josh Nichols technicalpickles.com
  2. What is Rake? Ruby Make Josh Nichols technicalpickles.com
  3. Ruby what? A Ruby build tool Josh Nichols technicalpickles.com
  4. Which means...? A tool for automating tasks on the command line using plain ol’ Ruby Josh Nichols technicalpickles.com
  5. What would you use Rake for? • Define a task • Make the task do something, maybe have it depend on other tasks • You’ve probably already used it • rake db:migrate • rake test Josh Nichols technicalpickles.com
  6. Overview • The basics of Rake • Creating re-usable tasks for Rails • Creating re-usable tasks for any Ruby project • Testing Josh Nichols technicalpickles.com
  7. Hello World # Rakefile (or rakefile or rakefile.rb or Rakefile.rb) require 'rake' desc quot;Hello World!quot; task :hello_world do puts quot;Hello World!quot; end Josh Nichols technicalpickles.com
  8. rake_demo $ rake -T (in /Users/nichoj/Projects/rake_demo) rake hello_world # Hello World! Josh Nichols technicalpickles.com
  9. rake_demo $ rake hello_world Hello World! Josh Nichols technicalpickles.com
  10. Defining tasks • desc • provide a human readable description for a task you're about to define • rake -T doesn’t show the task without this • task • provide a task name (can be a symbol or string) • provide a block • it's just ruby, yo Josh Nichols technicalpickles.com
  11. Defining tasks: playing with the filesystem • FileUtils module is included • cd, pwd, mkdir, mkdir_p, rmdir, ln, ln_s, ln_sf, cp, cp_r, mv, rm, rm_r, rm_rf, install, chmod, touch • FileList lets you specify a pattern for files/directories, then use it as an Enumerable • FileList['**/*.rb'].each {|f| puts f } Josh Nichols technicalpickles.com
  12. Defining tasks: playing with the filesystem require 'rake' desc quot;Clean the build directoryquot; task :clean do rm_r 'build' end desc quot;Make a backup of all ruby filesquot; task :backup do FileList['**/*.rb'].each do |f| cp f, quot;#{f}.bakquot; end end Josh Nichols technicalpickles.com
  13. Dependencies between tasks • Easily declare a dependency on another task • A task that is depended on will only be run once... even if you run multiple times Josh Nichols technicalpickles.com
  14. Dependencies between tasks require 'rake' task :give_presentation => :prepare_presentation do puts quot;So there's this thing called 'Rake'...quot; end task :prepare_presentation => :take_nap do puts quot;Just preparing, nothing to see here, move alongquot; end task :take_nap do puts quot;zzzzzzzzzzzzzzzzzzzquot; end Josh Nichols technicalpickles.com
  15. rake_demo $ rake give_presentation (in /Users/nichoj/Projects/rake_demo) zzzzzzzzzzzzzzzzzzz Nothing to see here, move along So there's this thing called 'Rake'... Josh Nichols technicalpickles.com
  16. rake_demo $ rake take_nap give_presentation zzzzzzzzzzzzzzzzzzz Nothing to see here, move along So there's this thing called 'Rake'... Josh Nichols technicalpickles.com
  17. Group related tasks • Would be annoying to have a flat namespace • This isn’t C! • Rake has the concept of namespaces • rake namespace:task_name Josh Nichols technicalpickles.com
  18. Define a namespace require 'rake' namespace :presentation do task :give => :prepare do puts quot;So there's this thing called 'Rake'...quot; end task :prepare => :take_nap do puts quot;Just preparing, nothing to see here, move alongquot; end end task :take_nap do puts quot;zzzzzzzzzzzzzzzzzzzquot; end Josh Nichols technicalpickles.com
  19. Extending tasks: more dependencies • Just declare the task again, but with dependencies • These get added to whatever dependencies Josh Nichols technicalpickles.com
  20. Moar dependencies namespace :presentation do task :give => :prepare do puts quot;So there's this thing called 'Rake'...quot; end task :prepare => :take_nap do puts quot;Just preparing, nothing to see here, move alongquot; end task :prepare => :watch_tv end task :watch_tv do puts quot;Previously on Heroes...quot; end task :take_nap do puts quot;zzzzzzzzzzzzzzzzzzzquot; end Josh Nichols technicalpickles.com
  21. Using off the shelf tasks • Rake comes with a few bundled • Rake::GemPackageTask, Rake::PackageTask, Rake::RDocTask, Rake::TestTask • Just instantiate an instance in your Rakefile Rake::TestTask.new do |t| t.libs << 'lib' t.pattern = 'test/*_test.rb' t.verbose = false end Josh Nichols technicalpickles.com
  22. Cue live coding ... cuz I ran out of time to make slides Josh Nichols technicalpickles.com
  23. Done, and done. Josh Nichols technicalpickles.com
  24. What’s got two thumbs and is available for hire? http://technicalpickles.com/resume josh@technicalpickles.com Josh Nichols technicalpickles.com
Advertisement