Rake 0.8.0リリース

Rake 0.8.0 is released. The docs have not been updated yet, but I didn't want to delay the new version for that. We will get the documents up to date shortly.

http://rubyforge.org/forum/forum.php?thread_id=20544&forum_id=20061

Rakeの新しいバージョンがリリースされた。まだ新しいドキュメントが出てきていないのだが、こういうページを作る程度にはRakeが好きなので、待ちきれずに使ってみた。

いくらか嘘をついているかもしれない。わからない部分もある。ドキュメントがリリースされ、正しい情報がわかり次第修正していく予定。

追記:Rake 0.8.2がリリースされたので、また記事を書いた。タスク引数まわりの使い方が変わっている。

以下、変更点を追っていく。

タスクパラメータの導入

  • Added task parameters (e.g. "rake build[version7]")
  • Made task parameters passable to prerequisites.
  • The 'task' command will now accept task argument names.

この3つは同じことを書いている気がするので、まとめて説明。

タスクにパラメータを渡せるようになった。ここでいうタスクには、TaskだけでなくFileTaskとruleも含まれる。

タスクの実行時にパラメータを渡して挙動を制御したくなることは結構あると思うのだが、従来のRakeでは環境変数を利用するしかなかった。

# Rakefile
task :build do
  puts "Build Version: #{ENV['VERSION']}"
end
$ rake build VERSION=3
(in /home/idesaku)
Build Version: 3
$

Rake 0.8.0では、タスクパラメータを使用することでよりスマートに実施できる。

# Rakefile
task :build, :version do |t, args|
  puts "Build Version: #{args[:version]}"
end

taskの先頭のパラメータは、これまで通りタスク名を表す。二つ目は渡すパラメータの名称である。

rake実行時に、タスク名[パラメータ]と指定することで、パラメータを渡せる。

$ rake build[1.0]
(in /home/idesaku)
Build Version: 1.0
$

パラメータにわざわざ名前を付けるようになっているあたりで想像がつくと思うが、パラメータは複数個渡すことができる。カンマ区切りで並べて書けばよい。

# Rakefile
task :build, :os, :version do |t, args|
  puts "Build Version #{args[:version]}, OS=#{args[:os]}"
end
$ rake build[Windows,1.0]
(in /home/idesaku)
Build Version 1.0, OS=Windows
$

パラメータとその名称の対応は、単に順番で決まる。

$ rake build[XXX,YYY]
(in /home/idesaku)
Build Version YYY, OS=XXX
$ rake build[YYY,XXX]
(in /home/idesaku)
Build Version XXX, OS=YYY
$

カンマの前後にスペースを入れることもできるが、その場合シェルからカンマの前後で別々のパラメータであると認識されないようにタスク名をシングルクォーテーションで囲むこと。

$ rake 'build[Windows, 1.0]'
(in /home/idesaku)
Build Version 1.0, OS=Windows
$

これにタスク間の依存関係を追記したい場合は、次のように書く。

# Rakefile
task :build, :os, :version, :needs => :prebuild do |t, args|
  puts "Build Version #{args[:version]}, OS=#{args[:os]}"
end

task :prebuild do
  puts "Prebuild..."
end
$ rake build[Windows,1.0]
(in /home/idesaku)
Prebuild...
Build Version 1.0, OS=Windows
$

:needsがミソ。ソースを見たところこのシンボルで決め打ちされていたので、こう書くものだと覚えるしかないようだ。また、これはパラメータの後ろ(doの直前)に書かねばならない。

一応、次のようにタスク名の直後に持ってくることもできる。

# Rakefile
task({:build => :prebuild}, :os, :version) do |t, args|
  puts "Build Version #{args[:version]}, OS=#{args[:os]}"
end

task :prebuild do
  puts "Prebuild..."
end

しかし、これは従来どおりのパラメータ無しのタスクを扱う処理の副作用であろう。括弧がいくつか出てきてDSLらしくない書き方になるので、あえてこの記法を使う必要は無い。

依存元のタスクパラメータは、依存先に同名のパラメータを定義しておけばそこに引き継がれる。

# Rakefile
task :build, :os, :version, :needs => :prebuild do |t, args|
  puts "Build Version #{args[:version]}, OS=#{args[:os]}"
end

task :prebuild, :os, :version do |t, args|
  puts "Prebuild Version #{args[:version]}, OS=#{args[:os]}"
end
$ rake build[Windows,1.0]
(in /home/idesaku)
Prebuild Version 1.0, OS=Windows
Build Version 1.0, OS=Windows
$

コメント表示形式の変更

  • Comments are limited to 80 columns or so (suggested by Jamis Buck).
  • Added -D to display full comments (suggested by Jamis Buck).

rake -Tしたときの表示が変わった。タスクのコメントを80カラムまで表示して、残りは省略するようになっている。

$ rake -T
(in /home/idesaku)
rake db:fixtures:load          # Load fixtures into the current environment...
rake db:migrate                # Migrate the database through scripts in db...
rake db:schema:dump            # Create a db/schema.rb file that can be por...
(略)

略された部分も参照したいのであれば、-Dオプションを使う。

$ rake -D
(in /home/idesaku)
rake db:fixtures:load
    Load fixtures into the current environment's database.  Load specific fixtures using FIXTURES=x,y

rake db:migrate
    Migrate the database through scripts in db/migrate. Target specific version with VERSION=x

rake db:schema:dump
    Create a db/schema.rb file that can be portably used against any DB supported by AR

(略)

その他

  • The rake program will set the status value used in any explicit exit(n) calls. (patch provided by Stephen Touset)

rakeスクリプトRakefile内部で指定したexitステータスコードを反映させるようになった。

# Rakefile

task :success

task :abort do
  exit 2
end
$ rake _0.7.3_ success; echo $?
0
$ rake _0.7.3_ abort; echo $?
1
$ rake _0.8.0_ success; echo $?
0
$ rake _0.8.0_ abort; echo $?
2

従来のRakeでは、exitステータスコードスクリプトが正常に終了すればゼロ、それ以外は1になると決まっていた。しかし、0.8.0からはコードを指定できるわけだ。他のスクリプトと連携させたいときに重宝するかもしれない。

  • Fixed error in functional tests that were not including session (and silently skipping the functionl tests).

機能テスト時にセッション情報を読み込まず、勝手にテストをスキップしてしまうエラーに対処、かな?

  • Removed --usage and make -h the same as -H.

rakeコマンドの簡単な使用法を表示させるオプション--usageが廃止、代わりに-hが追加された。これは-Hと同じ動作をする。確かに--usageはどうでもいい内容しか表示しないので、廃止して正解だろう。

$ rake _0.7.3_ --usage
rake [-f rakefile] {options} targets...

$ rake -h
rake [-f rakefile] {options} targets...

Options are ...

  --classic-namespace  (-C)
      Put Task and FileTask in the top level namespace
  --describe           (-D)
      Describe the tasks (matching optional PATTERN), then exit.
(略)
  • Make a prettier inspect for tasks.

taskのinspect表示が見やすくなった。

# Rakefile
task :default => [:foo, :bar] do |t|
  p t
end

task :foo
task :bar
$ rake _0.7.3_
(in /home/idesaku)
##, "foo"=>#, @scope=, @actions=, @lock=#, @comment=nil, @prerequisite
s=, @already_invoked=true, @name="foo">, "bar"=>#, @scope=, @actions=, @lock=#, @comment=nil, @prerequisites
=, @already_invoked=true, @name="bar">}, @imported=, @scope=, @original_dir="/home/idesaku"
, @pending_imports=, @last_comment=nil, @default_loader=#, @rakefil
e="Rakefile", @rules=, @loaders={".rake"=>#, ".rf"=>#}, @options=#, @rakefiles=["Rakefile"], @name="rake
">, @scope=[], @actions=[#], @lock=#,
 @comment=nil, @prerequisites=["foo", "bar"], @already_invoked=true, @name="default">

$ rake _0.8.0_
(in /home/idesaku)
 [foo, bar]>

$

なるほど。こりゃ圧倒的に読みやすいな。