![]() |
るびくる: |
![]() |
RB(あーるびー): |
![]() |
ねえねえRB、わたしのイラストどのくらい届いてる? |
![]() |
……へ? |
![]() |
ほら、先月パート1を公開したでしょ? |
![]() |
……0枚ですけど。 |
![]() |
えええーーー!? |
![]() |
いや、こっちがえええーーーだよ! |
![]() |
だって、多くの人が見てくれてたみたいだし |
![]() |
なんでVジャンプを例にあげたのかはよく分からないけど |
![]() |
うう……わたし宛のかわいいイラスト……(ぐすぐす) |
![]() |
ほら、元気出して。そろそろパート2始まるよ。 |
![]() |
(ぐすぐす) |
![]() |
あ、その前に冷凍パン温めるけど、るびくるも食べる? |
![]() |
わたしミルクパンを3個! それとコーンスープもお願いします! |
![]() |
ちょっと予想はしてたけど、立ち直るのはやっ! |
![]() |
さて、前回でも話したとおり、今回は実用的なRakefileのサンプルとして |
![]() |
待ってました! 前回の話では、たしかこういうことができるって話だったよね。
|
![]() |
そう。この2つのうち1番に関しては、簡単なHTMLファイルを作るだけのタスクだから |
![]() |
え? わたし前回の話で依存関係のところまでしか聞いてないんだけど、それでもRakefileが書けるものなの? |
![]() |
うん、基本の書き方と依存関係の考え方さえ分かっていれば、HTMLファイルを作るのには十分だよ。 |
![]() |
さて、ここからは実際にrakeコマンドを実行してもらうために |
![]() |
うん、Rake ユーザガイドのインストールページを読んでインストールしておいたから、バッチリだよ! |
![]() |
OK。それじゃ、試しにメニューを差し込んでHTMLファイルを作るためのRakefileを書いてみてもらえる? |
![]() |
わかった! 前回での話を参考にすると…… |
- Rakefile
- template/
- index.html
- header_menu.html
# encoding: utf-8 desc '全HTMLファイルを作成する' task :publish => 'index.html' desc 'トップページのHTMLファイルを作成する' file 'index.html' => ['template/index.html', 'template/header_menu.html'] do # 元になるHTMLファイルの読み込み base_html = File.read('template/index.html', encoding: 'utf-8') menu_html = File.read('template/header_menu.html', encoding: 'utf-8') # トップページHTMLの出力 open('index.html', 'w'){|f| f.write base_html.gsub('{HEADER_MENU_HTML}', menu_html) } end
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>るびくるのグルメサイト「Rubicle Groumet」</title> </head> <body> {HEADER_MENU_HTML} <h1>Rubicle Groumet</h1> <p>るびくるのグルメサイトです。おいしい食べ物についての情報を紹介していきます。</p> </body> </html>
<div id="MENU"> <ul> <li><a href="index.html">トップページ</a></li> <li><a href="50tour.html">そば家50店めぐり</a></li> <li><a href="no-more-weight.html">太らないための食べ方講座</a></li> </ul> </div>
![]() |
よし、書けたよ! |
![]() |
なるほど、templateディレクトリ内の index.html と header_menu.html から |
![]() |
それじゃこのRakefileを使って、rakeを実行してみるね。 |
$ rake -T
rake index.html # トップページのHTMLファイルを作成する
rake publish # 全HTMLファイルを作成する
$ rake publish
$
- index.html
- Rakefile
- template/
- index.html
- header_menu.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>るびくるのグルメサイト「Rubicle Groumet」</title> </head> <body> <div id="MENU"> <ul> <li><a href="index.html">トップページ</a></li> <li><a href="50tour.html">そば家50店めぐり</a></li> <li><a href="no-more-weight.html">太らないための食べ方講座</a></li> </ul> </div> <h1>Rubicle Groumet</h1> <p>るびくるのグルメサイトです。おいしい食べ物についての情報を紹介していきます。</p> </body> </html>
![]() |
できたー! |
![]() |
おめでとう! |
![]() |
|
![]() |
……始めてのrake成功でテンション上がったのは分かったから、rakeコマンドの連打はやめて次に行こうよ。 |
![]() |
それじゃせっかくだし、ここでdefaultタスクの説明もしておこうか。 |
![]() |
毎回rake publishって打つのはめんどくさいです。 |
![]() |
期待通りの答えをありがとう。 |
![]() |
それを敢えて聞いてくるってことは、めんどくさくない方法があるってことだよね! |
![]() |
お、おちついて! ハリーの回数多いよ! |
![]() |
りょーかい! |
# encoding: utf-8 task :default => :publish desc '全HTMLファイルを作成する' task :publish => 'index.html' desc 'トップページのHTMLファイルを作成する' file 'index.html' => ['template/index.html', 'template/header_menu.html'] do # 元になるHTMLファイルの読み込み base_html = File.read('template/index.html', encoding: 'utf-8') menu_html = File.read('template/header_menu.html', encoding: 'utf-8') # トップページHTMLの出力 open('index.html', 'w'){|f| f.write base_html.gsub('{HEADER_MENU_HTML}', menu_html) } end
$ rake -T
rake index.html # トップページのHTMLファイルを作成する
rake publish # 全HTMLファイルを作成する
$ rake
$
![]() |
ほんとだ! rakeって打ち込むだけでちゃんと実行できてる! |
![]() |
そういうこと。これでるびくるも、さっきみたいに何度もrakeしたいときに |
![]() |
これでrakeコマンドも打ち放題だね! |
![]() |
……モニターの前の皆さんのために一応言っておくと、普通は2回以上続けて |
![]() |
でもここまでだと、まだRakeならではの良いところってのがあんまり出て来てないね。 |
![]() |
そうだね。とりあえず、複数ファイルを対象に取るようにしてみたらどう? |
![]() |
そうだね! さっそくHTMLを増やして、Rakefileも書き換えてみるね! |
- Rakefile
- template/
- 50tour.html
- header_menu.html
- index.html
- no-more-weight.html
# encoding: utf-8 task :default => :publish desc '全HTMLファイルを作成する' task :publish => 'index.html' desc 'トップページのHTMLファイルを作成する' file 'index.html' => ['template/index.html', 'template/header_menu.html'] do # 元になるHTMLファイルの読み込み base_html = File.read('template/index.html', encoding: 'utf-8') menu_html = File.read('template/header_menu.html', encoding: 'utf-8') # HTMLの出力 open('index.html', 'w'){|f| f.write base_html.gsub('{HEADER_MENU_HTML}', menu_html) } end desc '「そば屋50店めぐり」ページのHTMLファイルを作成する' file '50tour.html' => ['template/50tour.html', 'template/header_menu.html'] do # 元になるHTMLファイルの読み込み base_html = File.read('template/50tour.html', encoding: 'utf-8') menu_html = File.read('template/header_menu.html', encoding: 'utf-8') # HTMLの出力 open('50tour.html', 'w'){|f| f.write base_html.gsub('{HEADER_MENU_HTML}', menu_html) } end desc '「太らないための食べ方講座」ページのHTMLファイルを作成する' file 'no-more-weight.html' => ['template/no-more-weight.html', 'template/header_menu.html'] do # 元になるHTMLファイルの読み込み base_html = File.read('template/no-more-weight.html', encoding: 'utf-8') menu_html = File.read('template/header_menu.html', encoding: 'utf-8') # HTMLの出力 open('no-more-weight.html', 'w'){|f| f.write base_html.gsub('{HEADER_MENU_HTML}', menu_html) } end
$ rake -D
rake 50tour.html
「そば屋50店めぐり」ページのHTMLファイルを作成する
rake index.html
トップページのHTMLファイルを作成する
rake no-more-weight.html
「太らないための食べ方講座」ページのHTMLファイルを作成する
rake publish
全HTMLファイルを作成する
$
![]() |
……勢いで書き足したけど、さすがにこの書き方は安直すぎるね。 |
![]() |
そうだね、これだとHTMLファイルを増やしたときに、コピー&ペーストの繰り返しで |
![]() |
そうだね。もうちょっと手を加えてみよっか。 |
![]() |
もちろん。メソッドどころかクラスだって定義できるよ。 |
![]() |
やった! それならファイルを検索するくらい簡単だね! |
# encoding: utf-8 #------------------------------------------------------------------------------- # 定数やメソッドの定義 #------------------------------------------------------------------------------- # 元になるHTMLファイルのリスト(配列) SOURCE_HTMLS = Dir.glob('template/*.html') SOURCE_HTMLS.delete('template/header_menu.html') # メニュー用のHTMLは除外 # 出力先HTMLファイルのリスト(配列)をクリア OUTPUT_HTMLS = [] # 「メニューを差し込んだHTMLを生成する」メソッドを定義 def make_html(source_html_name, output_html_name) # 元になるHTMLファイルの読み込み source_html = File.read(source_html_name, encoding: 'utf-8') menu_html = File.read('template/header_menu.html', encoding: 'utf-8') # メニューを差し込んだ状態のHTMLファイルを出力 open(output_html_name, 'w'){|f| f.write source_html.gsub('{HEADER_MENU_HTML}', menu_html) } # 処理内容を出力 $stderr.puts "#{source_html_name} ---> #{output_html_name}" end #------------------------------------------------------------------------------- # Rakeタスク #------------------------------------------------------------------------------- # defaultタスクを定義 task :default => :publish # 元になるHTMLファイル1つごとにループ SOURCE_HTMLS.each do |source_html_name| # 生成するHTMLのファイル名を決定して、出力先HTMLファイルのリストに追加 output_html_name = source_html_name.sub(/^template\//, '') OUTPUT_HTMLS << output_html_name desc "#{output_html_name} を作成する" file output_html_name => [source_html_name, 'template/header_menu.html'] do make_html(source_html_name, output_html_name) end end # 出力先HTMLファイルのリスト(配列)をもとにして、publishタスクを定義 desc '全HTMLファイルを作成する' task :publish => OUTPUT_HTMLS
$ rake -T
rake 50tour.html # 50tour.html を作成する
rake index.html # index.html を作成する
rake no-more-weight.html # no-more-weight.html を作成する
rake publish # 全HTMLファイルを作成する
$
![]() |
これでばっちり! |
![]() |
それじゃるびくる、試しにrakeコマンドを実行してみてくれる? |
![]() |
はーい。 |
$ rake
template/50tour.html ---> 50tour.html
template/index.html ---> index.html
template/no-more-weight.html ---> no-more-weight.html
$
- Rakefile
- 50tour.html
- index.html
- no-more-weight.html
- template/
- 50tour.html
- header_menu.html
- index.html
- no-more-weight.html
![]() |
できた! ためしにもう1回実行してみよっと。 |
$ rake
$
![]() |
あれ、今度は何もタスクが実行されてないよ? |
![]() |
ああ、それは依存してるファイルが更新されてないからだね。 |
![]() |
依存してるファイル……? |
# 元になるHTMLファイル1つごとにループ SOURCE_HTMLS.each do |source_html_name| # 生成するHTMLのファイル名を決定して、出力先HTMLファイルのリストに追加 output_html_name = source_html_name.sub(/^template\//, '') OUTPUT_HTMLS << output_html_name desc "#{output_html_name} を作成する" file output_html_name => [source_html_name, 'template/header_menu.html'] do make_html(source_html_name, output_html_name) end end
![]() |
ええと、今回で言えば、50tour.html の元になってるのは
の2ファイルだから…… |
![]() |
そう。その元になるファイルのうち、どちらか片方が変更されるまでは |
![]() |
そっか。元になってるファイルが更新されるまでは |
$ rm 50tour.html
$ rake
template/50tour.html ---> 50tour.html
$
![]() |
50tour.html だけをもう一回作り直してくれる、ってことなんだ! |
![]() |
お見事! きちんと動きを理解できてるね。 |
![]() |
やった! それじゃ先生、このRakefileの評価は100点満点で何点!? |
![]() |
50点。 |
![]() |
あっれェーーー!? |
![]() |
冒頭でいきなりショックを受けて、立ち直れなくなったわたしに |
![]() |
違う違う! |
![]() |
Rakeっぽい書き方? |
![]() |
そう。Rakeでできるのは、ただシンプルなタスクを定義・実行することだけじゃないんだよ。 |
![]() |
次回の記事で、わたしもついにRakeの匠になれるんだね! |
![]() |
劇的ビフォーアフターとは、また中途半端に渋いネタを…… |
パート3:実践編2に続きます。