日頃の行い

個人的な日頃の行いをつらつら書いてます\\\\ ٩( 'ω' )و ////

ファイルの変更を検知してなんらかのコマンドを走らせるreflexが便利

ファイル変更があったらテストを流すとかいろんなライブラリありますが、
言語に依存しないやつないかなぁと思って探してたらgo製のものがありました。

github.com

go製なのでbinaryが配布されていればそれを持ってくればすぐ使えますね
そう思っている時期が私にはありました。
releasesみたらソースコードがおいてあってbinaryはありませんでした。
悲しい。

Release v0.1.0 · cespare/reflex · GitHub

まあIssueになってたり、READMEにもTODOと書いてあるのでそのうちbinary配布されるでしょう。
今回はとりあえずgo getでインストールして使ってみました。

GitHub - cespare/reflex: Run a command when files change

github.com

今回検証に使ったスクリプトこちらです。
cloneして go get github.com/cespare/reflex をすれば make test/watch, make test/watch/config, make test/watch/config/fancy あたりが動くはずです。

gist.github.com

1. あるファイルが更新されたらそのファイルに対してあるコマンドを走らせる例

$make test/watch
reflex -r '\.go$' -- go test {}
[00] --- FAIL: TestHoge (0.00s)
[00]   hoge_test.go:8: 1 + 1 != 2 !?
[00] FAIL
[00] FAIL  command-line-arguments  0.009s
[00] (error exit: exit status 1)
[00] ok    command-line-arguments  0.014s

別画面でhoge_test.goのテストを変えて、戻してみました。
いい感じに動きます

2. あるファイルにはあるコマンドを走らせて、違うファイルには他のコマンドを走らせる例

ある拡張子のものにはこのコマンドを走らせて、このディレクトリにあるものにはこのコマンドを走らせたい。
みたいなときにはconfigにまとめて使うと良さそうです。

$make test/watch/config
reflex -c reflex.conf
[01] No syntax errors detected in hoge.php
[00] ok    _/Users/a-tanaka/hogehoge   0.008s
[01] No syntax errors detected in hoge.php
[01] PHP Parse error:  syntax error, unexpected '"', expecting ',' or ';' in hoge.php on line 3
[01]
[01] Parse error: syntax error, unexpected '"', expecting ',' or ';' in hoge.php on line 3
[01] Errors parsing hoge.php
[01] (error exit: exit status 255)
[01] No syntax errors detected in hoge.php
[00] --- FAIL: TestHoge (0.00s)
[00]   hoge_test.go:8: 1 + 1 != 2 !?
[00] FAIL
[00] FAIL  _/Users/a-tanaka/hogehoge   0.007s
[00] (error exit: exit status 1)
[00] ok    _/Users/a-tanaka/hogehoge   0.008s

このブログ上だとシンタックスハイライトが効いてますが、 ターミナル上だと殺風景でちょっと見づらいなと思ったらいい感じのオプションがありました。 

-d, –decoration=“plain”: How to decorate command output. Choices: none, plain, fancy.

_人人人人_
> fancy <
 ̄Y^Y^Y ̄

なんかおしゃれになりそう。

Before

f:id:arata3da4:20170622201242p:plain

After

f:id:arata3da4:20170622201245p:plain

なんかおしゃれになりました。

まとめ

  • reflexファイル変更があったときに任意のコマンドを実行できて便利
  • まだbinaryはないけどそのうちできたらgoへの依存もなく使えるので便利になりそう
  • fancy