初めての guard-shell
とりあえず、やりたいことはできた。
これ、いろんなことに応用できそう。
まずは、サンプルの設定ファイルを作成。カレントディレクトリにGuardfileが作成される。
hiro@Mac-mini% guard init shell Writing new Guardfile to /Users/hiro/devel/ruby/sandbox/Guardfile shell guard added to Guardfile, feel free to edit it hiro@Mac-mini% cat Guardfile # A sample Guardfile # More info at https://github.com/guard/guard#readme # Add files and commands to this file, like the example: # watch('file/path') { `command(s)` } # guard 'shell' do watch('(.*).txt') {|m| `tail #{m[0]}` } end
そのまま実行すると警告が出るので、watchの文字列を正規表現に変える '(.*).txt' → /(.*).txt/
そして、実行。
hiro@Mac-mini% guard start Guard is now watching at '/Users/hiro/devel/ruby/sandbox'
監視しているディレクトリに、テキストファイルを作成・変更すると、
hiro@Mac-mini% echo foo > foo.txt #別ターミナルで
tailコマンドが自動的に実行される。
hiro@Mac-mini% guard start Guard is now watching at '/Users/hiro/devel/ruby/sandbox' foo
ふと、設定ファイルの指定はどうするんだろうと思い、ざっとドキュメント眺めてみたが、どうも指定する方法はないみたい。
ソースもちらっと見たが、固定でGuardfileになっているようだ。
それから、guardコマンド自体にはデーモン化するオプションはないようなので、やるなら自前でということか。