ReplaceRegexpTask.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /*
  3. * $Id: ReplaceRegexpTask.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. /**
  23. * ReplaceRegExp is a directory based task for replacing the occurrence of a given regular expression with a substitution
  24. * pattern in a selected file or set of files.
  25. *
  26. * <code>
  27. * <replaceregexp file="${src}/build.properties"
  28. * match="OldProperty=(.*)"
  29. * replace="NewProperty=\1"
  30. * byline="true"/>
  31. * </code>
  32. *
  33. * @author Jonathan Bond-Caron <jbondc@openmv.com>
  34. * @version $Id: ReplaceRegexpTask.php 905 2010-10-05 16:28:03Z mrook $
  35. * @package phing.tasks.system
  36. * @see <http://ant.apache.org/manual/OptionalTasks/replaceregexp.html>
  37. */
  38. class ReplaceRegexpTask extends Task {
  39. /** Single file to process. */
  40. private $file;
  41. /** Any filesets that should be processed. */
  42. private $filesets = array();
  43. /**
  44. * Regular expression
  45. *
  46. * @var RegularExpression
  47. */
  48. private $_regexp;
  49. /**
  50. * File to apply regexp on
  51. *
  52. * @param string $path
  53. */
  54. function setFile(PhingFile $path)
  55. {
  56. $this->file = $path;
  57. }
  58. /**
  59. * Sets the regexp match pattern
  60. *
  61. * @param string $regexp
  62. */
  63. function setMatch( $regexp )
  64. {
  65. $this->_regexp->setPattern( $regexp );
  66. }
  67. /**
  68. * @see setMatch()
  69. */
  70. function setPattern( $regexp )
  71. {
  72. $this->setMatch( $regexp );
  73. }
  74. /**
  75. * Sets the replacement string
  76. *
  77. * @param string $string
  78. */
  79. function setReplace( $string )
  80. {
  81. $this->_regexp->setReplace( $string );
  82. }
  83. /**
  84. * Sets the regexp flags
  85. *
  86. * @param string $flags
  87. */
  88. function setFlags( $flags )
  89. {
  90. // TODO... $this->_regexp->setFlags( $flags );
  91. }
  92. /**
  93. * Match only per line
  94. *
  95. * @param bool $yesNo
  96. */
  97. function setByline( $yesNo )
  98. {
  99. // TODO... $this->_regexp->
  100. }
  101. /** Nested creator, adds a set of files (nested fileset attribute). */
  102. function createFileSet()
  103. {
  104. $num = array_push($this->filesets, new FileSet());
  105. return $this->filesets[$num-1];
  106. }
  107. function init()
  108. {
  109. $this->_regexp = new RegularExpression;
  110. }
  111. function main()
  112. {
  113. if ($this->file === null && empty($this->filesets)) {
  114. throw new BuildException("You must specify a file or fileset(s) for the <ReplaceRegexp> task.");
  115. }
  116. // compile a list of all files to modify, both file attrib and fileset elements
  117. // can be used.
  118. $files = array();
  119. if ($this->file !== null) {
  120. $files[] = $this->file;
  121. }
  122. if (!empty($this->filesets)) {
  123. $filenames = array();
  124. foreach($this->filesets as $fs) {
  125. try {
  126. $ds = $fs->getDirectoryScanner($this->project);
  127. $filenames = $ds->getIncludedFiles(); // get included filenames
  128. $dir = $fs->getDir($this->project);
  129. foreach ($filenames as $fname) {
  130. $files[] = new PhingFile($dir, $fname);
  131. }
  132. } catch (BuildException $be) {
  133. $this->log($be->getMessage(), Project::MSG_WARN);
  134. }
  135. }
  136. }
  137. $this->log("Applying Regexp processing to " . count($files) . " files.");
  138. // These "slots" allow filters to retrieve information about the currently-being-process files
  139. $slot = $this->getRegisterSlot("currentFile");
  140. $basenameSlot = $this->getRegisterSlot("currentFile.basename");
  141. $filter = new FilterChain($this->project);
  142. $r = new ReplaceRegexp;
  143. $r->setRegexps(array($this->_regexp));
  144. $filter->addReplaceRegexp($r);
  145. $filters = array($filter);
  146. foreach($files as $file) {
  147. // set the register slots
  148. $slot->setValue($file->getPath());
  149. $basenameSlot->setValue($file->getName());
  150. // 1) read contents of file, pulling through any filters
  151. $in = null;
  152. try {
  153. $contents = "";
  154. $in = FileUtils::getChainedReader(new FileReader($file), $filters, $this->project);
  155. while(-1 !== ($buffer = $in->read())) {
  156. $contents .= $buffer;
  157. }
  158. $in->close();
  159. } catch (Exception $e) {
  160. if ($in) $in->close();
  161. $this->log("Erorr reading file: " . $e->getMessage(), Project::MSG_WARN);
  162. }
  163. try {
  164. // now create a FileWriter w/ the same file, and write to the file
  165. $out = new FileWriter($file);
  166. $out->write($contents);
  167. $out->close();
  168. $this->log("Applying regexp processing to " . $file->getPath(), Project::MSG_VERBOSE);
  169. } catch (Exception $e) {
  170. if ($out) $out->close();
  171. $this->log("Error writing file back: " . $e->getMessage(), Project::MSG_WARN);
  172. }
  173. }
  174. }
  175. }