Rake的使用

安装Rake

	gem install rake

查看任务列表

	rake -T

运行一个任务

	rake task_name

指定一个文件名或才文件夹来运行任务

	rake mydoc.pdf

看帮助

	rake -h
	rake --help

定义任务

每个任务都包含三部分内容:

  • 描述(不设置,也可以,只是rake -T的时候,此任务将不会显示)
  • 任务名(如果多个任务名相同,则最后一个定义的将会覆盖前面定义的)
  • 任务执行代码

e.g.

desc "One line task description"
task :name_of_task do
	#your code goes here				
end

任务之间的依赖

	desc "Example of a task with prerequisites"
	task :third_task => ["first_task", "second_task"] do
	#Your code goes here
	end

third_task运行时,首先会检查first_task和second_task是否已经运行。

传参数给任务

	desc "Example of task with parameters and prerequisites"
	task :my_task, [:first_arg, :second_arg] => ["first_task", "second_task"] do |t, args|
	args.with_defaults(:first_arg => "Foo", :last_arg => "Bar")
	puts "First argument was: #{args.first_arg}"
	puts "Second argument was: 			#{args.second_arg}"
	end
	
	rake my_task[one, two]

有时,执行rake my_task[one, two]时会报找不到该任务,这时,你需要这样执行:rake "my_task[one, two]"

参数间用逗号分隔,不能有空格

设置默认任务

	task :default => ['my_task']
	
	
	task :default do
	
	end

有以上两种方式

在其中一个任务中调用另一个任务

desc "Example of task with invoke"
task :first_task do
	Rake::Task[:second_task].invoke
end

清理任务

	require 'rake/clean'
	CLEAN.include('*.tmp')
	CLOBBER.include('*.tmp','build/*')

在任务中使用其它库

require 'erb'

OUTPUT_FILE='README.html'
TEMPLATE_FILE='template.html.erb'

def get_template
	File.read(TEMPLATE_FILE)
end

desc "Builds the HTML file, using ERB."
file OUTPUT_FILE do
	File.open(OUTPUT_FILE, "w+") do |f|
		f.write(ERB.new(get_template).result())
	end
end

task :default => [OUTPUT_FILE]

使用shell命令

require 'fileutils'

#Stuff...

task :run_command do
	sh %{ space separated command and options }
end

需要引入fileutils

sh方法输出:状态、运行结果

sh %{grep pattern file} do |ok, res|
	if ! ok
	puts "pattern not found (status = #{res.exitstatus})"
	end
end

使用环境变量

desc "Task description"
	task :name_of_task do
	my_setting1 = ENV['HOME']
	my_setting2 = ENV['MY_VAR']
	# Your code goes here
end

命名空间

为了避免任务名之间的冲突。

namespace 'build' do

	# tasks...  

end

namespace 'test' do

	# tasks...

	namespace 'unit' do
	# tasks...
	end

end

使用的时候就是,rake 命名空间:命名空间:任务名

可动态定义task

require 'yaml'

# Uses FileList to get an Array of the configuration files
CONFIG_FILES=FileList['config/*.yml']

# Returns the configuration from the file as a Hash object
def get_config(file)
  YAML.load_file(file)
end

CONFIG_FILES.each do |f|

  config = get_config(f)

  namespace config[:name] do

    # Generate tasks

    desc "First task for #{config[:name]}"
    task config[:first] do
      # Code goes here
    end

    desc "Second task for #{config[:name]}"
    task config[:second] do
      # Code goes here
    end

    # more...

  end

end

rake特定Rakefile

	rake --rakefile my_task_file my_task
取消rake输出
rake --silent my_task

为任务设置特定环境亦是

	rake my_task my_var1='Some value' my_var2='Another value'

调试

  • 查看执行步骤,而不真正执行

      rake --dry-run my_task
    
  • 日志

      rake --trace my_task
    

资料

转载于:https://my.oschina.net/zjzhai/blog/278895

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值