---
title: "rake速记"
author: "bellstar"
date: "2011-05-09T17:58:16Z"
tags: []
categories: ["编程语言"]
slug: "rake"
draft: false
lastmod: "2012-08-17T23:23:30Z"
id: 1037790
url: "https://bellstar.iteye.com/blog/1037790"
---

**参考教程**
  
<http://jasonseifer.com/2010/04/06/rake-tutorial>


**hello rake**

```ruby

#rakefile.rb
task :default => [:hello]
task :hello do
  puts 'hello rake'
end
#$ rake 
#$ rake hello
#$ rake rakefile.rb

```


**namespace**

```ruby

task :default => "morning:turn_of_alarm"
namespace :morning do
    task :turn_of_alarm do
      puts 'ding...'
    end
end
#$rake moring:turn_of_alarm

```


**task description**

```ruby

desc "description of task "
task :hello do
end
#$ rake -T
#=>
#  rake hello # description of task

```



**redefined task**

```ruby

task :default => 'morning:turn_of_alarm'
namespace :morning do
	desc "turn of alarm"
	task :turn_of_alarm do
		puts 'hello'
	end
end

namespace :morning do
	task :turn_of_alarm do
		puts 'world'
	end
end
#$ rake 
#=>
# hello
# world

```



**invoking other task**

```ruby

task :default => 'task:world'
namespace :task do
	desc "hello world"
	task :hello do
		puts 'hello'
	end
end

namespace :task do
	task :world do
		puts '...'
		Rake::Task['task:hello'].invoke
		puts 'world'
	end
end
#Rake::Task['task:hello'].invoke(args.args_name)

```



**task dependencies**

```ruby

task :default => 'task:world'
namespace :task do
	task :hello do
		puts 'hello'
	end
end

namespace :task do
	task :world => :hello do
		puts 'world'
	end
end

```


**task arguments**

```ruby

task :say, [:name,:gender] do |t, args|
	args.with_defaults(:name=>'john',:gender=>'boy')
	puts "hello #{args.name}  #{args.gender}"
end 
#rake say[lucy,girl]

```


**multitask**
Declare a task that performs its prerequisites in parallel. Multitasks does not guarantee that its prerequisites will execute in any given order (which is obvious when you think about it)

```ruby

 multitask :deploy => [:deploy_gem, :deploy_rdoc]

```


**mkdir task**

```ruby

 directory "tmp/abc"
#rake tmp/abc
#mkdir -p tmp/abc
 
#执行前会先建立tmp/abc
 task :task_a => "tmp/abc" do
 
 end

```


**rule task**

```ruby

rule '.o' => ['.c'] do |t|
	sh "cp #{t.source} #{t.name}"
end
#touch a.c
#rake a.o
#cp a.c a.o
#t.source指a.c,  t.name是a.o

```