View
5.160
Download
1
Category
Preview:
DESCRIPTION
Rake is the Ruby build tool. Most Rubyists have at least used it in passing. But how well do you really know it? This presentation was given by Josh Nichols at the December 2008 Boston Ruby Group meeting. See the presentation at http://vimeo.com/2496890
Citation preview
Josh Nichols technicalpickles.com
The Familiar Stranger
Rake
Josh Nichols technicalpickles.com
Ruby Make
What is Rake?
Josh Nichols technicalpickles.com
A Ruby build tool
Ruby what?
Josh Nichols technicalpickles.com
A tool for automating tasks on the command line using plain ol’ Ruby
Which means...?
Josh Nichols technicalpickles.com
• 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
What would you use Rake for?
Josh Nichols technicalpickles.com
• The basics of Rake
• Creating re-usable tasks for Rails
• Creating re-usable tasks for any Ruby project
• Testing
Overview
Josh Nichols technicalpickles.com
# Rakefile (or rakefile or rakefile.rb or Rakefile.rb)
require 'rake'
desc "Hello World!"task :hello_world do puts "Hello World!"end
Hello World
Josh Nichols technicalpickles.com
(in /Users/nichoj/Projects/rake_demo)rake hello_world # Hello World!
rake_demo $ rake -T
Josh Nichols technicalpickles.com
Hello World!
rake_demo $ rake hello_world
Josh Nichols technicalpickles.com
• 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
Defining tasks
Josh Nichols technicalpickles.com
• 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 }
Defining tasks: playing with the filesystem
Josh Nichols technicalpickles.com
require 'rake'
desc "Clean the build directory"task :clean do rm_r 'build'end
desc "Make a backup of all ruby files"task :backup do FileList['**/*.rb'].each do |f| cp f, "#{f}.bak" endend
Defining tasks: playing with the filesystem
Josh Nichols technicalpickles.com
• Easily declare a dependency on another task
• A task that is depended on will only be run once... even if you run multiple times
Dependencies between tasks
Josh Nichols technicalpickles.com
require 'rake'
task :give_presentation => :prepare_presentation do puts "So there's this thing called 'Rake'..."end
task :prepare_presentation => :take_nap do puts "Just preparing, nothing to see here, move along"end
task :take_nap do puts "zzzzzzzzzzzzzzzzzzz"end
Dependencies between tasks
Josh Nichols technicalpickles.com
(in /Users/nichoj/Projects/rake_demo)zzzzzzzzzzzzzzzzzzzNothing to see here, move alongSo there's this thing called 'Rake'...
rake_demo $ rake give_presentation
Josh Nichols technicalpickles.com
zzzzzzzzzzzzzzzzzzzNothing to see here, move alongSo there's this thing called 'Rake'...
rake_demo $ rake take_nap give_presentation
Josh Nichols technicalpickles.com
• Would be annoying to have a flat namespace
• This isn’t C!
• Rake has the concept of namespaces
• rake namespace:task_name
Group related tasks
Josh Nichols technicalpickles.com
require 'rake'
namespace :presentation do task :give => :prepare do puts "So there's this thing called 'Rake'..." end
task :prepare => :take_nap do puts "Just preparing, nothing to see here, move along" endend
task :take_nap do puts "zzzzzzzzzzzzzzzzzzz"end
Define a namespace
Josh Nichols technicalpickles.com
• Just declare the task again, but with dependencies
• These get added to whatever dependencies
Extending tasks: more dependencies
Josh Nichols technicalpickles.com
namespace :presentation do task :give => :prepare do puts "So there's this thing called 'Rake'..." end
task :prepare => :take_nap do puts "Just preparing, nothing to see here, move along" end task :prepare => :watch_tvend
task :watch_tv do puts "Previously on Heroes..."end
task :take_nap do puts "zzzzzzzzzzzzzzzzzzz"end
Moar dependencies
Josh Nichols technicalpickles.com
• 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 = falseend
Using off the shelf tasks
Josh Nichols technicalpickles.com
... cuz I ran out of time to make slides
Cue live coding
Josh Nichols technicalpickles.com
Done, and done.
Josh Nichols technicalpickles.com
http://technicalpickles.com/resumejosh@technicalpickles.com
What’s got two thumbs and is available for hire?
Recommended