Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

guardでファイル監視を行う

guardというgemがあります。
これはテストやビルドの自動化をしてくれるスグレモノです。

目的

ローカルにあるtxtファイルが変更されたらタイムスタンプをログファイルに記録。

準備

Gemfile記述

source 'http://rubygems.org'
gem 'guard'
gem 'guard-shell'

Gemの導入とGuardの実行

$ bundle install
$ guard init

Guardfileというモノが生成されます。

Guardfileの編集

def function(str)                                                                                             
  ary = []                                                                                                    
  File.open("hoge.txt").each do |e|                                                                           
    /wbee (\d+)/ =~ e                                                                                         
    ary << $1                                                                                                 
  end                                                                                                         
                                                                                                              
  return %Q(#{Time.now.strftime("Printed on %k:%M::%m/%d/%Y").to_s} :: #{ary})                                
end                                                                                                           
                                                                                                              
guard :shell do                                                                                               
  watch(%r{hoge.txt}) {|m| File.open("./log.log", "a"){|fp| fp << "#{function(m[0])}\n"}}                     
end               

guradの実行

$ bundle exec guard
22:24:29 - INFO - Guard is using TerminalTitle to send notifications.
22:24:29 - INFO - Guard is now watching at '/Users/shuzo/src/ruby_code/guardtest'
[1] guard(main)> 

とでます。
裏で勝手に動いてくれているので、このまま放置しても大丈夫です。
実行を確認したら、テキストファイル(今回は./hoge.txt)を

wbee 9900                                                                                                     
wbee 8899                                                                                                     
wbee 7790                                                                                                     
wbee 88888                                                                                                    
as you like                                                                                                   
wbee 77                                                                                                       
wbee 66666                                                                                                    
wbee 12345                                                                                                    
Kaiju                                                                                                         
wbee 666 

のように更新すると

Printed on 23:24::12/23/2013 :: ["9900", "8899", "7790", "88888", nil, "77", "66666", "12345", nil, "666"]

といったログが次々と追加されていきます。