在Ruby和Rails中的Rake是什麼?

in #cn6 years ago

在Ruby和Rails中的Rake是什麼?

如果你寫了一段時間的Rails,你可能會重複看到像是rake db:seed、rake db:migrate等等的指令,但卻不清楚Rake到底是什麼。

這篇文章會介紹Rake究竟是什麼,以及我們該如何使用Rake。

什麼是Rake

Rake is a software task management and build automation tool. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace.

It is similar to SCons and Make, but it has a number of differences. The tool is written in the Ruby programming language and the Rakefiles (equivalent of Makefiles in Make) use Ruby syntax. It was created by Jim Weirich. — 取自維基百科

簡單來說,Rake是一個工具程式,藉由讀取叫做Rakefile的檔案來執行任務腳本,而這些任務腳本就是所謂的task。

註:如果希望了解更多Rake的定義,可以先看文章最下方的「補充:更多Rake的定義」的章節。

建立task

最簡單建構task的方式就是,透過task這個method後方定義一個task。像是do_something就是這個task的名稱;接著使用block,並把想要執行的Ruby程式碼放在裡面,這樣就成功的定義一個完整的task了。

在Rails的task,都是放在lib/tasks目錄下且附檔名為.rake的檔案。

# lib/tasks/demo.rake
task :do_something do
  puts "Hello"
end

要執行這個task的話,只要在console中下rake do_something,就會執行我們的do_something task。

$ rake do_something
Hello

列出所有的task

一般來說,在Rake裡面通常會有許多task,這些task就是我們想要執行的程式。在一個專案中,我們可以透過rake -Trake --task的縮寫)列出所有可以使用的rake指令。

$ rake -T
rake about                              # List versions of all Rails framew...
rake annotate_models                    # Add schema information (as commen...
rake annotate_routes                    # Adds the route map to routes.rb
rake app:template                       # Applies the template supplied by ...
rake app:update                         # Update configs and some other ini...
rake assets:clean[keep]                 # Remove old compiled assets
...
...

使用description

在前面有提到rake -T這個指令會列出所有的task。但是如果你在上一步有自己定義task並執行rake -T的話,你可能會注意到剛剛定義的task並沒有在列表中。

這是因為rake -T預設只會列出有description的task,所以如果你希望定義的task在列表中顯示的話,可以在task上方使用desc method來加入描述

desc "A task used for understanding rake"
task :do_something
  puts "Hello"
end

這樣你再下rake -T的指令就可以看到我們定義的task了

使用namespace

我們會需要使用namespace的原因,是因為很多方法的命名然後會重複,但是我們不希望使用時產生衝突或著造成困惑,所以就會透過namespace來避免命名衝突。

以rake的例子來說,假設我們在不同的情況下分別定義了兩個create的task,但是當我們呼叫他們的時候可能會產生衝突,所以我們就可以透過namespace來避免這個問題。

像是我們可以為這兩個task分別加上db和example的namespace,這樣我們就可以很清楚地分辨這兩個method。

$ rake db:create
$ rake example:create

在Rack中要使用namespace的方法其實也很簡單,就是使用namespace的method定義一個namespace,然後再把本來的task還有description包在block裡。

namespace :example do
  desc "A task used for understanding rake"
  task :do_something
    puts "Hello"
  end
end

接著下rake -T的指令應該就可以看到以下的結果。

在不同的環境下執行

在不特別指定環境的情況下,rake會預設在development的環境下執行。如果你需要指定執行環境的話,可以特過RAILS_ENV=your_environment的指令來指定。

rake db:schema:dump RAILS_ENV=production

在別的rake task下執行其他task

我們可以在一個task中執行其他的task

namespace :example do
  desc "Rebuild system"
  task :rebuild => ["db:drop", "db:setup"]
end

接著當我們呼叫example:rebuild這個task的時候,就會執行db:dropdb:setup兩個task。

使用:environment dependency

如果你試圖執行一個使用到Rails中的model的task時,你可能會發現遇到了一些錯誤。

task :get_user
  puts "#{User.find(1).email}"
end

這些錯誤會產生的原因是,一般的task並沒有載入環境中的model等等。要解決這個問題,你可以透過加入:environment來避免這個問題。

task :get_user => :environment
  puts "#{User.find(1).email}"
end

補充:更多Rake的定義

Rake is a command line utility program that rails uses to automate commonly performed tasks. It is written in ruby. We already saw rake in action in the previous tutorial where we used it to automatically create the database for us. — 取自### RUBY ON RAILS RAKE

Rake is a utility similar to make in Unix. You can say Rake is the make of ruby - the RubyMake. Rails defines a number of tasks to help you. — 取自Ruby on Rails - Rake

Rake is a Rubygem that automates many common functions required to build, test, package, and install programs; it is part of every modern Ruby installation, so you don't need to install it yourself. — 取自Core Ruby Tools

參考資料

RUBY ON RAILS RAKE
Rake for Rails Developers
錦囊妙計-後端篇
Rake: Automate All the Things
Ruby on Rails - Rake
Launchschool-Rake
Ruby 語法放大鏡之「常在終端機裡下 rake db:migrate 指令,這個 rake 是什麼,後面那個 db:migrate 又是怎麼回事?」

Environment

Intro to Rake
Custom Rake Tasks

Sort:  

@linjiahung, 看到你的帖子,真是我的幸运啊!太棒了!