1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
<?php
/*
* $Id: ec6660c57e64b5a52ceda9bc931d4d85d8288521 $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
require_once 'phing/Task.php';
include_once 'phing/types/FileList.php';
include_once 'phing/types/FileSet.php';
/**
* Appends text, contents of a file or set of files defined by a filelist to a destination file.
*
* <code>
* <append text="And another thing\n" destfile="badthings.log"/>
* </code>
* OR
* <code>
* <append file="header.html" destfile="fullpage.html"/>
* <append file="body.html" destfile="fullpage.html"/>
* <append file="footer.html" destfile="fullpage.html"/>
* </code>
* OR
* <code>
* <append destfile="${process.outputfile}">
* <filterchain>
* <xsltfilter style="${process.stylesheet}">
* <param name="mode" expression="${process.xslt.mode}"/>
* <param name="file_name" expression="%{task.append.current_file.basename}"/> <!-- Example of using a RegisterSlot variable -->
* </xsltfilter>
* </filterchain>
* <filelist dir="book/" listfile="book/PhingGuide.book"/>
* </append>
* </code>
* @package phing.tasks.system
* @version $Id: ec6660c57e64b5a52ceda9bc931d4d85d8288521 $
*/
class AppendTask extends Task {
/** Append stuff to this file. */
private $to;
/** Explicit file to append. */
private $file;
/** Any filesets of files that should be appended. */
private $filesets = array();
/** Any filelists of files that should be appended. */
private $filelists = array();
/** Any filters to be applied before append happens. */
private $filterChains = array();
/** Text to append. (cannot be used in conjunction w/ files or filesets) */
private $text;
/** Sets specific file to append. */
function setFile(PhingFile $f) {
$this->file = $f;
}
/**
* Set target file to append to.
* @deprecated Will be removed with final release.
*/
function setTo(PhingFile $f) {
$this->log("The 'to' attribute is deprecated in favor of 'destFile'; please update your code.", Project::MSG_WARN);
$this->to = $f;
}
/**
* The more conventional naming for method to set destination file.
* @param PhingFile $f
*/
function setDestFile(PhingFile $f) {
$this->to = $f;
}
/**
* Supports embedded <filelist> element.
* @return FileList
*/
function createFileList() {
$num = array_push($this->filelists, new FileList());
return $this->filelists[$num-1];
}
/**
* Nested adder, adds a set of files (nested fileset attribute).
*
* @return void
*/
public function addFileSet(FileSet $fs) {
$this->filesets[] = $fs;
}
/**
* Creates a filterchain
*
* @return FilterChain The created filterchain object
*/
function createFilterChain() {
$num = array_push($this->filterChains, new FilterChain($this->project));
return $this->filterChains[$num-1];
}
/**
* Sets text to append. (cannot be used in conjunction w/ files or filesets).
* @param string $txt
*/
function setText($txt) {
$this->text = (string) $txt;
}
/**
* Sets text to append. Supports CDATA.
* @param string $txt
*/
function addText($txt) {
$this->text = (string) $txt;
}
/** Append the file(s). */
function main() {
if ($this->to === null) {
throw new BuildException("You must specify the 'destFile' attribute");
}
if ($this->file === null && empty($this->filelists) && empty($this->filesets) && $this->text === null) {
throw new BuildException("You must specify a file, use a filelist, or specify a text value.");
}
if ($this->text !== null && ($this->file !== null || !empty($this->filelists))) {
throw new BuildException("Cannot use text attribute in conjunction with file or filelists.");
}
// create a filwriter to append to "to" file.
$writer = new FileWriter($this->to, $append=true);
if ($this->text !== null) {
// simply append the text
$this->log("Appending string to " . $this->to->getPath());
// for debugging primarily, maybe comment
// out for better performance(?)
$lines = explode("\n", $this->text);
foreach($lines as $line) {
$this->log($line, Project::MSG_VERBOSE);
}
$writer->write($this->text);
} else {
// append explicitly-specified file
if ($this->file !== null) {
try {
$this->appendFile($writer, $this->file);
} catch (Exception $ioe) {
$this->log("Unable to append contents of file " . $this->file->getAbsolutePath() . ": " . $ioe->getMessage(), Project::MSG_WARN);
}
}
// append the files in the filelists
foreach($this->filelists as $fl) {
try {
$files = $fl->getFiles($this->project);
$this->appendFiles($writer, $files, $fl->getDir($this->project));
} catch (BuildException $be) {
$this->log($be->getMessage(), Project::MSG_WARN);
}
}
// append any files in filesets
foreach($this->filesets as $fs) {
try {
$files = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
$this->appendFiles($writer, $files, $fs->getDir($this->project));
} catch (BuildException $be) {
$this->log($be->getMessage(), Project::MSG_WARN);
}
}
} // if ($text ) {} else {}
$writer->close();
}
/**
* Append an array of files in a directory.
* @param FileWriter $writer The FileWriter that is appending to target file.
* @param array $files array of files to delete; can be of zero length
* @param PhingFile $dir directory to work from
*/
private function appendFiles(FileWriter $writer, $files, PhingFile $dir) {
if (!empty($files)) {
$this->log("Attempting to append " . count($files) . " files" .($dir !== null ? ", using basedir " . $dir->getPath(): ""));
$basenameSlot = Register::getSlot("task.append.current_file");
$pathSlot = Register::getSlot("task.append.current_file.path");
foreach($files as $filename) {
try {
$f = new PhingFile($dir, $filename);
$basenameSlot->setValue($filename);
$pathSlot->setValue($f->getPath());
$this->appendFile($writer, $f);
} catch (Exception $ioe) {
$this->log("Unable to append contents of file " . $f->getAbsolutePath() . ": " . $ioe->getMessage(), Project::MSG_WARN);
}
}
} // if !empty
}
private function appendFile(FileWriter $writer, PhingFile $f) {
$in = FileUtils::getChainedReader(new FileReader($f), $this->filterChains, $this->project);
while(-1 !== ($buffer = $in->read())) { // -1 indicates EOF
$writer->write($buffer);
}
$this->log("Appending contents of " . $f->getPath() . " to " . $this->to->getPath());
}
}
|