Rake を使ってみた
Rakefile の書き方
# coding: utf-8 task :hello do puts 'do task hello!' end
とか
# coding: utf-8 # CocoaPods の セットアップ namespace :tanjoin do task :setup do show_message() cd 'Example/' do sh 'pod update' sh 'pod install' end end def show_message puts "CocoaPods だよ" end end
名前空間が使えるので衝突回避も容易
ディレクトリの移動
sh 'cd Example' とかやっても移動して
元のディレクトリに戻ってしまうので無意味
なので、下のように実装する
cd 'Example/' do puts 'Hello world' end
do から end までの間は Example ディレクトリに移動した状態で実行される
シェルスクリプト
message = "彼女募集中" sh("echo #{message}")
引数を簡単に使うことができるので非常に便利
joken = [ "かわいくて", "献身的で", "声もかわいくて", "初々しくて", "浮気しない" ] sh("echo '#{joken.join('、')}彼女がほしい'")
配列操作も簡単
ファイル・ディレクトリ操作
ディレクトリ作成
FileUtils.mkdir_p('Directory')
ディレクトリ削除
FileUtils.rmdir('Directory')
ファイルコピー
# @param [String] pattern # It is the pattern for finding files. # # @param [String] dest # Destination directory. # # @note This method copy to the destination directory # the files that matches the pattern. # # @return void # def cp_file(pattern, dest) Dir.glob(pattern).each do |file| puts file filename = File.basename(file) FileUtils.mkdir_p(dest) FileUtils.copy_file(file, File.join(dest, filename)) end end
Dir.glob を使うと正規表現を使って一括コピーが可能
なので、cp_file なるクソみたいな命名のメソッドでも作ると
非常に快適なコピーライフが送れます