Rake的使用

Stella981
• 阅读 540

安装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
    

资料

点赞
收藏
评论区
推荐文章
胖大海 胖大海
1年前
Linux定时任务详解
crond定时任务详解crond是Linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,可以在无需人工干预的情况下运行作业。我的环境是3A服务器搭建centos7.9,延迟低安装crond服务yuminsta
仲远 仲远
1年前
Things3 for Mac(日程和任务管理工具)
Things3forMac是Mac平台上一款非常优秀的任务管理软件,严格按照GTD流程来规划人们的任务安排,设计方式也和很多其他的应用程序有很大差别。things3mac版是一个功能强大,易于使用的任务管理应用程序,可帮助您输入,组织和处理待办事项列表中的
Stella981 Stella981
2年前
IDE netbeans 任务 任务列表 TODO
在代码中写入注释//TODO这里是注释netbeans中的任务列表就会显示这个注释这是Thinkphp中用到的代码版本 |版本信息:ThinkPHP3.0Release2012/3/5 类        abstractclassAction 方法    ajaxReturn行数  329   
Stella981 Stella981
2年前
AWS Batch 是如何向 Docker 容器传递参数
AWSBatch 提供了简单有效的方式来运行Docker 镜像,在单纯执行一个计算任务时比ECS 使用起来要方便许多。AWS Batch 在提交任务时可以执行command,environment 和parameters, 那么它将如何传递那些参数给Docker 容器呢?首先看一下提交任务的脚本awsbatchs
Wesley13 Wesley13
2年前
Java并发(一)wait()与notifyAll()
  当你使用线程来同时执行多个任务时,可以通过使用锁(互斥)来同步两个任务的行为,从而使得一个任务不会干涉另一个任务的资源。也就是说,如果两个任务在交替着使用某项共享资源(通常是内存),你可以使用互斥来是的任何时刻只有一个任务可以访问这项资源。那么,如果线程之间是协作关系,我们必须保证某些步骤在其他步骤之前先被处理。举个例子:必须先挖房子的地基,接下来才
Stella981 Stella981
2年前
Linux的定时任务
任务计划的条件:1.在未来的某个时间点执行一次某个任务(atbatch)2.周期性的执行某个任务(cron)at在指定时间执行任务_用法_at\选项参数\\时间\_选项参数_\l      查看作业\c      显示即将执行任务的细节\d      使用任务id号
Stella981 Stella981
2年前
JS微任务 宏任务,Promise、setTimeout、setImmediate运行顺序实测
结论如下1.虽然理论上应当先运行Promise,再运行setTimeout。但是由于历史版本或使用polyfill,使得Promise未必优先运行。2.setImmediate未必比setTimeout早运行3.在最新浏览器中Promise会早于事件冒泡运行,在设计框架时应考虑这一情况以下是实测情况\
Wesley13 Wesley13
2年前
Java Activiti6.0 spring5 SSM 工作流引擎 审批流程 java项目框架
1.模型管理:web在线流程设计器、预览流程xml、导出xml、部署流程2.流程管理:导入导出流程资源文件、查看流程图、根据流程实例反射出流程模型、激活挂起3.运行中流程:查看流程信息、当前任务节点、当前流程图、作废暂停流程、指派待办人4.历史的流程:查看流程信息、流程用时、流程状态、查看任务发起人信息5.待办任务:查看本人个人任务以及
Stella981 Stella981
2年前
Django之使用celery异步完成发送验证码
使用celery的目的:将项目中耗时的操作放入一个新的进程实现1.安装celerypipinstallcelery2.在项目的文件夹下创建包celery\_tasks用于保存celery异步任务3.在celery\_tasks下新建config.py指定broker到redisbroker_url'redis
Stella981 Stella981
2年前
Python任务调度模块 – APScheduler
APScheduler是一个Python定时任务框架,使用起来十分方便。提供了基于日期、固定时间间隔以及crontab类型的任务,并且可以持久化任务、并以daemon方式运行应用。目前最新版本为3.0.x。在APScheduler中有四个组件:触发器(trigger)包含调度逻辑,每一个作业有它自己的触发器,用于决定接下来哪一个作业会运行