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