0

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

4

2 回答 2

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;
    }
}

?>
于 2013-07-22T08:24:46.470 回答
1

如果您追求跨平台,那么我想您可以尝试使用 Node.js旁观者​​猎犬之类的方法来为 Phing 生成命令行 exec。或者,如果您想自行开发,可以使用一些 C++ 跨平台观察程序库。

于 2013-05-27T08:38:43.293 回答