Rake

From Schmid.wiki
(Redirected from Rakefile)
Jump to: navigation, search

Contents

References

Using Rake

rake -T     List available tasks for a Rakefile
rake -n     Dry run - do nothing but list actions
rake -s     Suppress confusing directory messages

Rakefile format

Simple Example

task :default => :hello

desc "This task prints out a 'hello world' message"
task :hello do
    puts "hello world"
end

Tasks

Tasks are created using this syntax:

task :doSomething => :prereq1

A more elaborate example, where 't' is a reference to the task, and the closure may call Rake::Task methods on it:

task :doSomething => [:prereq1, :prereq2] do |t|
    ... # you could call t.name to get 'doSomething'
end

or

Files

How to create some file

file "somefile" do |t|
   ... t.name
end

Rules

Example C++ rule

rule '.o' => ['.cpp'] do |t|
   sh "g++ #{t.source} -c -o #{t.name}" 
end

Clean and Clobber

require 'rake/clean'
CLEAN.include('*.{o,tmp}')
CLOBBER.include('myprogram')

Advanced Rake

Generating Multiple Files

This example includes dependency on a set of files and a directory and removing them when cleaning.

require 'rake/clean'
require 'find'

# clean by removing this dir
CLEAN.include 'testFiles'

# generates an array with 10 'testFiles/tempFile[n]' files
TEMPFILES = (0..9).collect { |x| "testFiles/tempFile#{x}" }

# create this if needed
directory 'testFiles'

task :default => :generate

desc "Generate 10 empty files in testFiles/."
task :generate => ['testFiles'] + TEMPFILES

# How to generate a tempFile
rule( /testFiles\/tempFile.$/ ) do |t|
    touch t.name
end

LaTeX

require 'rake/clean'

task :default => [ 'slides.pdf', :clean ]

# DEPENDENCIES ---------------------------------------------------------------
PUBLISH_TARGET="/home/schmid/web/schmid.dk/htdocs/slides.pdf"
FIGURES=FileList['*.svg']
PDF_FIGURES=FIGURES.ext('pdf')
file 'slides.pdf' => ['slides.tex', 'preamble.tex', 'Rakefile'] + PDF_FIGURES

# OPTIONS --------------------------------------------------------------------
LATEX_OPTIONS="--quiet"

# HOUSECLEANING --------------------------------------------------------------
CLEAN.include('*.{aux,snm,toc,nav,out}')
CLOBBER.include('slides.pdf', PDF_FIGURES)

# COMPILING ------------------------------------------------------------------
rule '.pdf' => '.tex' do |t| # rule: object files are made from source files
    sh "pdflatex #{LATEX_OPTIONS} #{t.source}"
    sh "pdflatex #{LATEX_OPTIONS} #{t.source}"
    sh "pdflatex #{LATEX_OPTIONS} #{t.source}"
end

# GRAPHICS CONVERSION --------------------------------------------------------
rule '.pdf' => '.svg' do |t|
    sh "convert #{t.source} #{t.name}"
end
rule '.pdf' => '.png' do |t|
    sh "convert #{t.source} #{t.name}"
end

Doing Different Stuff on Different Hosts

require 'socket'
case Socket::gethostname
when 'host1'
   ...
else
   ...
end

Subdirectories

task :default do
    sh 'cd largeScale && rake'
    sh 'cd smallArea && rake'
end

Rake on Win32

- see Windows Tools page

Quick-Download Rake to Current Dir

wget http://rubyforge.org/frs/download.php/9500/rake-0.7.1.zip
unzip -j rake-0.7.1.zip rake-0.7.1/lib/rake.rb
rm rake-0.7.1.zip

Using Vim and Rake on Win32

  1. start gvim from msys shell (otherwise rake won't run)
  2. run commands:
:set makeprg=rake
:mak
Personal tools