官术网_书友最值得收藏!

Defining custom rake tasks

So far, we defined only one task named default. Rake allows you to define your custom tasks with any name. The common form of the custom rake task definition is passing a task name to the task method and a block as a second argument. The block defines some action and usually contains some Ruby code. The rake task might have an optional description, which is defined with the desc method. This method accepts a text for the description of the task. The following code snippet is an example of defining a custom rake task:

desc 'Restart web server'
task :restart do
  touch '~/restart.txt'
end

This is an example of a possible rake task to restart Passenger (this is a module for the Nginx web server, which works with the Rails applications). We name the task restart. To run this task, just pass its name as the second argument to the rake command as shown in the following line of code:

$ rake restart

If you have a lot of tasks, it's handy to enclose them to the named spaces, as shown in the following code snippet:

namespace :server do
  desc 'Restart web server'
  task :restart do
    touch './tmp/restart.txt'
  end
end

You can also run the task in the command line using the following command:

$ rake server:restart

Actually, the task method accepts more arguments. However, they are related to other topics that are explained further along in the book in Chapter 2, Working with Files, and Chapter 3, Working with Rules.

主站蜘蛛池模板: 抚顺县| 久治县| 蕲春县| 和田县| 岫岩| 娱乐| 封丘县| 胶州市| 瑞丽市| 东山县| 清涧县| 若尔盖县| 泰安市| 库尔勒市| 温州市| 中江县| 呼伦贝尔市| 沽源县| 都昌县| 英超| 和顺县| 嵊州市| 万安县| 衡南县| 蒙自县| 静宁县| 钟祥市| 长丰县| 重庆市| 安岳县| 易门县| 外汇| 牡丹江市| 奉节县| 康定县| 海安县| 永吉县| 湖州市| 吉林省| 大丰市| 疏勒县|