それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

ruby のwdm でWindowsのファイル監視。

windowsでもファイル監視したい!

dot net framework を使えばいいので、FileSystemWaticherなんだけど。。。

windowsではrb-inotify が使えないので、IronRuby/IronPythonでFileSystemWatcherを使うのだが、それすら面倒になってきた。

インターフェース違うじゃん?ruby-win32じゃなくて、ironruby要るじゃん?

wdmというパッケージを使う

これのインストールには gcc(MinGW)が必要。

WDM 使ってみた例

早速使ってみた

監視したいフォルダを指定して、イベントが来るまでthread.join して待ちますよ。

takuya@-PC:~/Desktop$ cat checker.rb
#coding:utf-8
#
require 'wdm'

monitor = WDM::Monitor.new
puts monitor

# Watch a single directory
monitor.watch('C:\Users\takuya\Desktop') { |change|  puts change.path }

t = Thread.new { monitor.run! }
t.join

適当にファイル消して〜イベント発生させてやる

takuya@-PC:~/Desktop$  rm MinGW\ Installer.lnk
takuya@-PC:~/Desktop$ mv mintty.exe\ -\ ショートカット.lnk  cmd\(mintty\).lnk

出力を見る。

C:\Users\takuya\Desktop>ruby checker.rb
#<WDM::Monitor:0x000000030b1190>
C:/Users/takuya/Desktop/MinGW Installer.lnk
C:/Users/takuya/Desktop/mintty.exe - ショートカット.lnk
C:/Users/takuya/Desktop/cmd(mintty).lnk

行けるじゃん。ファイル監視のイベント取得がカンタン。

WDM優秀!

ではWDMmingw-ruby を入れたログ。

RubyInstaller の development kit をダウンロードしてインストールしたけど上手く動かないので、

MinGW/MYS か、CygwinMinGW-gccを使えばいいんじゃないかと。

F:\Ruby\bin>gem install wdm
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing wdm:
        ERROR: Failed to build gem native extension.

    F:/Ruby/bin/ruby.exe -r ./siteconf20140928-3632-b13w3w.rb extconf.rb
checking for main() in -lkernel32... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=F:/Ruby/bin/ruby
        --with-kernel32lib
        --without-kernel32lib
F:/Ruby/lib/ruby/2.1.0/mkmf.rb:456:in `try_do': The compiler failed to generate
an executable file. (RuntimeError)
You have to install development tools first.
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:541:in `try_link0'
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:556:in `try_link'
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:742:in `try_func'
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:973:in `block in have_library'
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:918:in `block in checking_for'
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:351:in `block (2 levels) in postpone
'
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:321:in `open'
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:351:in `block in postpone'
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:321:in `open'
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:347:in `postpone'
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:917:in `checking_for'
        from F:/Ruby/lib/ruby/2.1.0/mkmf.rb:968:in `have_library'
        from extconf.rb:19:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in F:/Ruby/lib/ruby/gems/2.1.0/gems/wdm-0.1.0 fo
r inspection.
Results logged to F:/Ruby/lib/ruby/gems/2.1.0/extensions/x64-mingw32/2.1.0/wdm-0
.1.0/gem_make.out

このようにエラーであふれる。

Development Kit 役立たずやなぁ。たぶんgccが見つからない系・・・インストールしたのに。

CygwinでMinttyを起動して入れる

もうCygwinでいいや、cygwin からビルドすりゃいいや。

apt-cyg install mingw64-x86_64-gcc-core.

その後、

gem install wdm

takuya@-PC:~$ gem install wdm
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Successfully installed wdm-0.1.0
Parsing documentation for wdm-0.1.0
Done installing documentation for wdm after 0 seconds
1 gem installed

Cygwin はホント便利なんだよなぁ。

Cygwinでビルドするのが一番早くないか・・・mingw-gcc使うならcygwin.dllに依存しないしね。

Cygwinmingwのバイナリ作ってインストールした。

追記

後になって分かったことだけれど、

DEVELOPMENT KIT でダウンロードするバージョンを間違えていた。

上にあるもののほうが古い。

http://cdn.rubyinstaller.org/archives/devkits/DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe

しまったダウンロードサイトは普通一番上が一番新しいから読まずに一番上をダウンロードしていた。時間の無駄。

参考資料

https://github.com/Maher4Ever/wdm