123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- require_once 'phing/Task.php';
- include_once 'phing/tasks/system/condition/Condition.php';
- include_once 'phing/util/DirectoryScanner.php';
- include_once 'phing/util/SourceFileScanner.php';
- include_once 'phing/mappers/MergeMapper.php';
- class UpToDateTask extends Task implements Condition {
- private $_property;
- private $_value;
- private $_sourceFile;
- private $_targetFile;
- private $sourceFileSets = array();
- protected $mapperElement = null;
-
- public function setProperty($property) {
- $this->_property = $property;
- }
-
- public function setValue($value) {
- $this->_value = $value;
- }
-
- private function getValue() {
- return ($this->_value !== null) ? $this->_value : "true";
- }
-
- public function setTargetFile($file) {
- if (is_string($file)) {
- $file = new PhingFile($file);
- }
- $this->_targetFile = $file;
- }
-
- public function setSrcfile($file) {
- if (is_string($file)) {
- $file = new PhingFile($file);
- }
- $this->_sourceFile = $file;
- }
-
- public function createSrcfiles() {
- $fs = new FileSet();
- $this->sourceFileSets[] = $fs;
- return $fs;
- }
-
- public function createFileset() {
- $fs = new FileSet();
- $this->sourceFileSets[] = $fs;
- return $fs;
- }
-
-
- public function createMapper() {
- if ($this->mapperElement !== null) {
- throw new BuildException("Cannot define more than one mapper",
- $this->location);
- }
- $this->mapperElement = new Mapper($this->getProject());
- return $this->mapperElement;
- }
-
- public function evaluate() {
- if (count($this->sourceFileSets) === 0 && $this->_sourceFile === null) {
- throw new BuildException("At least one srcfile or a nested "
- . "<fileset> element must be set.");
- }
- if (count($this->sourceFileSets) > 0 && $this->_sourceFile !== null) {
- throw new BuildException("Cannot specify both the srcfile "
- . "attribute and a nested <fileset> "
- . "element.");
- }
- if ($this->_targetFile === null && $this->mapperElement === null) {
- throw new BuildException("The targetfile attribute or a nested "
- . "mapper element must be set.");
- }
-
- if ($this->_targetFile !== null && !$this->_targetFile->exists()) {
- return false;
- }
-
- if ($this->_sourceFile !== null && !$this->_sourceFile->exists()) {
- throw new BuildException($this->_sourceFile->getAbsolutePath()
- . " not found.");
- }
- $upToDate = true;
- for($i=0,$size=count($this->sourceFileSets); $i < $size && $upToDate; $i++) {
- $fs = $this->sourceFileSets[$i];
- $ds = $fs->getDirectoryScanner($this->project);
- $upToDate = $upToDate && $this->scanDir($fs->getDir($this->project),
- $ds->getIncludedFiles());
- }
- if ($this->_sourceFile !== null) {
- if ($this->mapperElement === null) {
- $upToDate = $upToDate &&
- ($this->_targetFile->lastModified() >= $this->_sourceFile->lastModified());
- } else {
- $sfs = new SourceFileScanner($this);
- $upToDate = $upToDate &&
- count($sfs->restrict($this->_sourceFile->getAbsolutePath(),
- null, null,
- $this->mapperElement->getImplementation())) === 0;
- }
- }
- return $upToDate;
- }
-
- public function main() {
- if ($this->_property === null) {
- throw new BuildException("property attribute is required.",
- $this->location);
- }
- $upToDate = $this->evaluate();
- if ($upToDate) {
- $this->project->setNewProperty($this->_property, $this->getValue());
- if ($this->mapperElement === null) {
- $this->log("File \"" . $this->_targetFile->getAbsolutePath()
- . "\" is up-to-date.", Project::MSG_VERBOSE);
- } else {
- $this->log("All target files are up-to-date.",
- Project::MSG_VERBOSE);
- }
- }
- }
- protected function scanDir(PhingFile $srcDir, $files) {
- $sfs = new SourceFileScanner($this);
- $mapper = null;
- $dir = $srcDir;
- if ($this->mapperElement === null) {
- $mm = new MergeMapper();
- $mm->setTo($this->_targetFile->getAbsolutePath());
- $mapper = $mm;
- $dir = null;
- } else {
- $mapper = $this->mapperElement->getImplementation();
- }
- return (count($sfs->restrict($files, $srcDir, $dir, $mapper)) === 0);
- }
- }
|