36
loading...
This website collects cookies to deliver better user experience
Rakefile
file in your directory, and putting all the code there.task :default => :hello
desc "Say helo"
task :hello do
puts "Hello, World!"
end
task
block defines a task. desc
before it provides a simple description. Tasks can have dependencies which you can specify with =>
.$ rake hello
Hello, World!
rake default
:$ rake
Hello, World!
rake -T
lists all available tasks. Tasks without descriptions are treated as internal and not displayed by default:$ rake -T
rake hello # Say helo
-A
:$ rake -T -A
rake default #
rake hello # Say helo
--trace
, Rake
will tell you exactly what it's doing:$ rake --trace
** Invoke default (first_time)
** Invoke hello (first_time)
** Execute hello
Hello, World!
** Execute default
file
and list their file dependencies. If you run rake hello.html
, it will only actually do anything if hello.md
changed. It does it by comparing last modified date.task :default => "hello.html"
file "hello.html" => "hello.md" do
sh "pandoc hello.md -o hello.html"
end
*.foo
file can be built from *.bar
files.t.name
is the task or file we're doing, t.source
is the source, and so on.task :default => "hello.html"
rule ".html" => ".md" do |t|
sh "pandoc", t.source, "-o", t.name
end
sh
command. This way Rake has zero issues with escaping file names with special characters. So many Unix shell scripts and Make files just crash if file names contain something as exotic as spaces. It's one of so many advantages of using a real programming language and not shell.require "pathname"
(1..2).each do |i|
file "#{i}.txt" do |t|
Pathname(t.name).write "1"
end
end
(3..100).each do |i|
file "#{i}.txt" => ["#{i-1}.txt", "#{i-2}.txt"] do |t|
Pathname(t.name).write t.sources.map{|n| Pathname(n).read.to_i}.sum
end
end
desc "Cleanup Fibonacci files"
task "clean" do
sh "trash *.txt"
end
1.txt
and 2.txt
the rule is very simple - we create them by writing 1
to the file.$ rake 100.txt
$ cat 100.txt
354224848179261915075
$ rake --trace 10.txt
** Invoke 10.txt (first_time)
** Invoke 9.txt (first_time)
** Invoke 8.txt (first_time)
** Invoke 7.txt (first_time)
** Invoke 6.txt (first_time)
** Invoke 5.txt (first_time)
** Invoke 4.txt (first_time)
** Invoke 3.txt (first_time)
** Invoke 2.txt (first_time)
** Execute 2.txt
** Invoke 1.txt (first_time)
** Execute 1.txt
** Execute 3.txt
** Invoke 2.txt (not_needed)
** Execute 4.txt
** Invoke 3.txt (not_needed)
** Execute 5.txt
** Invoke 4.txt (not_needed)
** Execute 6.txt
** Invoke 5.txt (not_needed)
** Execute 7.txt
** Invoke 6.txt (not_needed)
** Execute 8.txt
** Invoke 7.txt (not_needed)
** Execute 9.txt
** Invoke 8.txt (not_needed)
** Execute 10.txt
$ rake --trace 10.txt
** Invoke 10.txt (first_time, not_needed)
** Invoke 9.txt (first_time, not_needed)
** Invoke 8.txt (first_time, not_needed)
** Invoke 7.txt (first_time, not_needed)
** Invoke 6.txt (first_time, not_needed)
** Invoke 5.txt (first_time, not_needed)
** Invoke 4.txt (first_time, not_needed)
** Invoke 3.txt (first_time, not_needed)
** Invoke 2.txt (first_time, not_needed)
** Invoke 1.txt (first_time, not_needed)
** Invoke 2.txt (not_needed)
** Invoke 3.txt (not_needed)
** Invoke 4.txt (not_needed)
** Invoke 5.txt (not_needed)
** Invoke 6.txt (not_needed)
** Invoke 7.txt (not_needed)
** Invoke 8.txt (not_needed)
desc "FizzBuzz"
task "fizzbuzz", [:n] do |t, args|
n = args.n.to_i
puts ["Fizz"[(n % 3)*4..], "Buzz"[(n % 5)*4..], " #{n}"].join.split.first
end
$ rake 'fizzbuzz[1]'
1
$ rake "fizzbuzz[5]"
Buzz
$ rake "fizzbuzz[9]"
Fizz
$ rake "fizzbuzz[15]"
FizzBuzz