監視型自動makeを実現するmakeauto.pl

だいぶやっつけ仕事。
適当すぎて、なにからつっこめばいいかわからない。
perlを選んだ理由がわからない。
だれか説明してくれ。

d:id:lurker:20070227:1172511851を見て、監視しつつ自動でmakeしてくれたら楽だなーと思って、ググったりして出てきたソースをパクったりして、作った。
http://nais.to/~yto/tools/miharing/
これをもろパクリしまんた><


makeじゃないコマンドも監視しつつ自動でやってくれる。

$ perl makeauto.pl -d=no -e="perl %f" test.pl

とかやって使う。

僕が常用して使ってるのは

$ perl makeauto.pl -d=no -e="make" *.php */*.php

別コンソールでこれ起動しつつ、Makefile書き換えながらphpの動作チェック。
ソース書きながらも動作チェックするので、ちょっとしたデバッグツールになってたりする。

#!/usr/bin/perl 
### NO WARRANTY, NO COPYRIGHT! ###
# $Id$
# Usage: perl makeauto.pl [-n<second>] [-d=no] [-e="<source>"] <files>

### HELP BEGIN ###################
# 使用法: perl makeauto.pl [オプション]... <ファイル>...
# ファイルの更新を見張って、更新されたらmakeを行う
# 
# オプション
#       -n<seconds>             make間隔を指定する(初期値:2)
#       -d=no                           makeするディレクトリを移動しない
#       -e="<source>"           監視実行コマンドの指定(make以外も指定可能)
#                                               ※特定の文字列を置き換えます
#                                                       %f : 更新ファイル
#
# 使用例: perl makeauto.pl -n1 *.[ch] 
### HELP END #####################

use strict;

use Cwd;
my $cwd = Cwd::getcwd();

#:---------------------------------------:#
#option
my $op_sec=2;
my $op_dir=1;
my $op_src="make; make tag > /dev/null;";

#:---------------------------------------:#
#file listner 
my %up;
my %cur;

#:---------------------------------------:#
#args check
my @files=();
foreach (@ARGV){
        #option -n
        if ( /^\-n(\d+)/ )
        { $op_sec = $1; next; }

        #option -d
        if ( /^\-d\=no/ )
        { $op_dir = 0; next; }

        #option -e
        if ( /^\-e\=(.*)/ )
        { $op_src = $1; next; }

        $up{$_} = $cur{$_} = (stat $_)[9];
        @files = (@files, $_);
}
&usage("missing files") if ( @files == 0 );


print "auto command: $op_src.\n";
#:---------------------------------------:#
#main loop
while (1) {
        foreach my $fn (@files){
                #print "$fn: ".$cur{$fn}."=".$up{$fn}."\n";

                $cur{$fn} = (stat $fn)[9];      # 監視対象ファイルの更新時間
                #print "$up $cur\n";    # for debug
                if ($up{$fn} != $cur{$fn}) {
                        my @dir = split /\//, $fn;
                        if ( -d @dir[0] and $op_dir )
                        {
                                pop(@dir); #remove file
                                my $dir="";
                                foreach( @dir )
                                { $dir .= "$_/"; }

                                chdir $dir;
                                #print "\n\n$dir\n\n";
                        }
                                
                        ### 対象ファイルがアップデートされたときに行う処理
                        # ここから
                        print "$fn: ============ UPDATE ============\n";

                        my $src = $op_src;
                        $src =~ s/\%f/$fn/;
                        system($src);
                        # ここまで

                        chdir $cwd;
                        $up{$fn} = $cur{$fn} = (stat $fn)[9];
                        #print "test=".$up{$fn}." fn=$fn\n";
                }
        }
        sleep $op_sec;                  # 監視間隔
}


#:---------------------------------------:#
#exit program with warrn and usage
sub usage(){
        my $err = shift;
        print "Warrn: $err.\n";
        system("grep '^# Usage:' ".__FILE__." | sed 's/# //'");
        exit(0);
}