UpToDateTask.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /*
  3. * $Id: UpToDateTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. include_once 'phing/tasks/system/condition/Condition.php';
  23. include_once 'phing/util/DirectoryScanner.php';
  24. include_once 'phing/util/SourceFileScanner.php';
  25. include_once 'phing/mappers/MergeMapper.php';
  26. /**
  27. * Sets the given property if the specified target has a timestamp
  28. * greater than all of the source files.
  29. *
  30. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  31. * @author William Ferguson <williamf@mincom.com> (Ant)
  32. * @author Hiroaki Nakamura <hnakamur@mc.neweb.ne.jp> (Ant)
  33. * @author Stefan Bodewig <stefan.bodewig@epost.de> (Ant)
  34. * @version $Revision: 905 $
  35. * @package phing.tasks.system
  36. */
  37. class UpToDateTask extends Task implements Condition {
  38. private $_property;
  39. private $_value;
  40. private $_sourceFile;
  41. private $_targetFile;
  42. private $sourceFileSets = array();
  43. protected $mapperElement = null;
  44. /**
  45. * The property to set if the target file is more up-to-date than
  46. * (each of) the source file(s).
  47. *
  48. * @param property the name of the property to set if Target is up-to-date.
  49. */
  50. public function setProperty($property) {
  51. $this->_property = $property;
  52. }
  53. /**
  54. * The value to set the named property to if the target file is more
  55. * up-to-date than (each of) the source file(s). Defaults to 'true'.
  56. *
  57. * @param value the value to set the property to if Target is up-to-date
  58. */
  59. public function setValue($value) {
  60. $this->_value = $value;
  61. }
  62. /**
  63. * Returns the value, or "true" if a specific value wasn't provided.
  64. */
  65. private function getValue() {
  66. return ($this->_value !== null) ? $this->_value : "true";
  67. }
  68. /**
  69. * The file which must be more up-to-date than (each of) the source file(s)
  70. * if the property is to be set.
  71. *
  72. * @param file the file we are checking against.
  73. */
  74. public function setTargetFile($file) {
  75. if (is_string($file)) {
  76. $file = new PhingFile($file);
  77. }
  78. $this->_targetFile = $file;
  79. }
  80. /**
  81. * The file that must be older than the target file
  82. * if the property is to be set.
  83. *
  84. * @param file the file we are checking against the target file.
  85. */
  86. public function setSrcfile($file) {
  87. if (is_string($file)) {
  88. $file = new PhingFile($file);
  89. }
  90. $this->_sourceFile = $file;
  91. }
  92. /**
  93. * Nested <srcfiles> element.
  94. *
  95. * @deprecated Deprecated since Phing 2.4.0
  96. */
  97. public function createSrcfiles() {
  98. $fs = new FileSet();
  99. $this->sourceFileSets[] = $fs;
  100. return $fs;
  101. }
  102. /**
  103. * Nested <fileset> element.
  104. */
  105. public function createFileset() {
  106. $fs = new FileSet();
  107. $this->sourceFileSets[] = $fs;
  108. return $fs;
  109. }
  110. /**
  111. * Defines the FileNameMapper to use (nested mapper element).
  112. */
  113. public function createMapper() {
  114. if ($this->mapperElement !== null) {
  115. throw new BuildException("Cannot define more than one mapper",
  116. $this->location);
  117. }
  118. $this->mapperElement = new Mapper($this->getProject());
  119. return $this->mapperElement;
  120. }
  121. /**
  122. * Evaluate (all) target and source file(s) to
  123. * see if the target(s) is/are up-to-date.
  124. * @return boolean
  125. */
  126. public function evaluate() {
  127. if (count($this->sourceFileSets) === 0 && $this->_sourceFile === null) {
  128. throw new BuildException("At least one srcfile or a nested "
  129. . "<fileset> element must be set.");
  130. }
  131. if (count($this->sourceFileSets) > 0 && $this->_sourceFile !== null) {
  132. throw new BuildException("Cannot specify both the srcfile "
  133. . "attribute and a nested <fileset> "
  134. . "element.");
  135. }
  136. if ($this->_targetFile === null && $this->mapperElement === null) {
  137. throw new BuildException("The targetfile attribute or a nested "
  138. . "mapper element must be set.");
  139. }
  140. // if the target file is not there, then it can't be up-to-date
  141. if ($this->_targetFile !== null && !$this->_targetFile->exists()) {
  142. return false;
  143. }
  144. // if the source file isn't there, throw an exception
  145. if ($this->_sourceFile !== null && !$this->_sourceFile->exists()) {
  146. throw new BuildException($this->_sourceFile->getAbsolutePath()
  147. . " not found.");
  148. }
  149. $upToDate = true;
  150. for($i=0,$size=count($this->sourceFileSets); $i < $size && $upToDate; $i++) {
  151. $fs = $this->sourceFileSets[$i];
  152. $ds = $fs->getDirectoryScanner($this->project);
  153. $upToDate = $upToDate && $this->scanDir($fs->getDir($this->project),
  154. $ds->getIncludedFiles());
  155. }
  156. if ($this->_sourceFile !== null) {
  157. if ($this->mapperElement === null) {
  158. $upToDate = $upToDate &&
  159. ($this->_targetFile->lastModified() >= $this->_sourceFile->lastModified());
  160. } else {
  161. $sfs = new SourceFileScanner($this);
  162. $upToDate = $upToDate &&
  163. count($sfs->restrict($this->_sourceFile->getAbsolutePath(),
  164. null, null,
  165. $this->mapperElement->getImplementation())) === 0;
  166. }
  167. }
  168. return $upToDate;
  169. }
  170. /**
  171. * Sets property to true if target file(s) have a more recent timestamp
  172. * than (each of) the corresponding source file(s).
  173. * @throws BuildException
  174. */
  175. public function main() {
  176. if ($this->_property === null) {
  177. throw new BuildException("property attribute is required.",
  178. $this->location);
  179. }
  180. $upToDate = $this->evaluate();
  181. if ($upToDate) {
  182. $this->project->setNewProperty($this->_property, $this->getValue());
  183. if ($this->mapperElement === null) {
  184. $this->log("File \"" . $this->_targetFile->getAbsolutePath()
  185. . "\" is up-to-date.", Project::MSG_VERBOSE);
  186. } else {
  187. $this->log("All target files are up-to-date.",
  188. Project::MSG_VERBOSE);
  189. }
  190. }
  191. }
  192. protected function scanDir(PhingFile $srcDir, $files) {
  193. $sfs = new SourceFileScanner($this);
  194. $mapper = null;
  195. $dir = $srcDir;
  196. if ($this->mapperElement === null) {
  197. $mm = new MergeMapper();
  198. $mm->setTo($this->_targetFile->getAbsolutePath());
  199. $mapper = $mm;
  200. $dir = null;
  201. } else {
  202. $mapper = $this->mapperElement->getImplementation();
  203. }
  204. return (count($sfs->restrict($files, $srcDir, $dir, $mapper)) === 0);
  205. }
  206. }