修改文件时执行phing目标

时间:2013-05-19 16:41:44

标签: phing filemtime

我有一个大约25个文件的文件列表。当其中一个文件的修改时间发生变化时,我需要执行某个phing目标。这样做的最佳方式是什么?

2 个答案:

答案 0 :(得分:1)

如果您正在使用跨平台,那么我猜您可以尝试使用Node.js beholderhound来为Phing生成命令行exec。或者,如果你想自己动手,可以使用一些C ++跨平台观察器库。

答案 1 :(得分:1)

通过编写自己的任务来扩展phing。像这样使用文件集的东西。 仅限于观看文件以进行更改。

<?php
/**

    Example target:

    <target name="mytarget">
      <react refresh="1.7" cmd="dosomething.sh">
        <fileset dir=".">
            <include name="*.txt" />
        </fileset>
      </react>
    </target>

    Will call dosomething.sh script when a txt file has changed in the current
    directory every 1.7 seconds.

    $Id: ReactTask.php 123 2013-07-22 08:16:26Z gregory.vincic $
*/
require_once "phing/Task.php";

class ReactTask extends Task {

    /** Command to execute */
    private $cmd = null;

    public function setCmd($str) {
      $this->cmd = $str;
    }

    /** Refresh time in microseconds, defaults to 1 second. */
    private $refresh = 1000000;

    public function setRefresh($str) {
        if($str != null && is_numeric($str)) {
            $this->refresh = $str*1000000;
        }
    }

    /** Any filesets of files that should be appended. */
    private $filesets = array();

    function createFileSet() {
        $num = array_push($this->filesets, new FileSet());
        return $this->filesets[$num-1];
    }

    /** Uses phps passthru to execute the configured command every X seconds */
    public function main() {
        $lastmtime = null;
        $this->log("Refreshing every " . $this->refresh/1000000 . " seconds.\n", Project::MSG_WARN);
        while(1) {
            $mtimes = $this->rlist();
            if(count($mtimes) > 0 && max($mtimes) > $lastmtime) {
                passthru($this->cmd);
                $lastmtime = max($mtimes); 
            }
            usleep($this->refresh);
        }
    }

    /** Lists modification times of all the files defined by your filesets. */
    private function rlist() {
        $res = array();
        foreach($this->filesets as $fs) {
            try {
                $files = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
                foreach ($files as $file) {
                    $path = $fs->dir . "/" . $file;
                    $res[] = filemtime($path);
                }
            } catch (BuildException $be) {
                $this->log($be->getMessage(), Project::MSG_WARN);
            }
        }
        return $res;
    }
}

?>