rake: the familiar stranger

24
Josh Nichols technicalpickles.com The Familiar Stranger Rake

Upload: josh-nichols

Post on 02-Dec-2014

5.156 views

Category:

Technology


1 download

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

TRANSCRIPT

Page 1: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

The Familiar Stranger

Rake

Page 2: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

Ruby Make

What is Rake?

Page 3: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

A Ruby build tool

Ruby what?

Page 4: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

A tool for automating tasks on the command line using plain ol’ Ruby

Which means...?

Page 5: Rake: The Familiar Stranger

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?

Page 6: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

• The basics of Rake

• Creating re-usable tasks for Rails

• Creating re-usable tasks for any Ruby project

• Testing

Overview

Page 7: Rake: The Familiar Stranger

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

Page 8: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

(in /Users/nichoj/Projects/rake_demo)rake hello_world # Hello World!

rake_demo $ rake -T

Page 9: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

Hello World!

rake_demo $ rake hello_world

Page 10: Rake: The Familiar Stranger

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

Page 11: Rake: The Familiar Stranger

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

Page 12: Rake: The Familiar Stranger

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

Page 13: Rake: The Familiar Stranger

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

Page 14: Rake: The Familiar Stranger

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

Page 15: Rake: The Familiar Stranger

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

Page 16: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

zzzzzzzzzzzzzzzzzzzNothing to see here, move alongSo there's this thing called 'Rake'...

rake_demo $ rake take_nap give_presentation

Page 17: Rake: The Familiar Stranger

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

Page 18: Rake: The Familiar Stranger

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

Page 19: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

• Just declare the task again, but with dependencies

• These get added to whatever dependencies

Extending tasks: more dependencies

Page 20: Rake: The Familiar Stranger

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

Page 21: Rake: The Familiar Stranger

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

Page 22: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

... cuz I ran out of time to make slides

Cue live coding

Page 23: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

Done, and done.

Page 24: Rake: The Familiar Stranger

Josh Nichols technicalpickles.com

http://technicalpickles.com/[email protected]

What’s got two thumbs and is available for hire?