123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- require_once 'phing/Task.php';
- include_once 'phing/types/FileList.php';
- include_once 'phing/types/FileSet.php';
- class AppendTask extends Task {
-
-
- private $to;
-
-
- private $file;
-
-
- private $filesets = array();
-
-
- private $filelists = array();
-
-
- private $filterChains = array();
-
-
- private $text;
-
-
- function setFile(PhingFile $f) {
- $this->file = $f;
- }
-
-
- 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;
- }
-
-
- function setDestFile(PhingFile $f) {
- $this->to = $f;
- }
-
-
- function createFileList() {
- $num = array_push($this->filelists, new FileList());
- return $this->filelists[$num-1];
- }
-
- function createFileSet() {
- $num = array_push($this->filesets, new FileSet());
- return $this->filesets[$num-1];
- }
-
-
- function createFilterChain() {
- $num = array_push($this->filterChains, new FilterChain($this->project));
- return $this->filterChains[$num-1];
- }
-
-
- function setText($txt) {
- $this->text = (string) $txt;
- }
-
- function addText($txt) {
- $this->text = (string) $txt;
- }
-
-
- 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.");
- }
-
-
- $writer = new FileWriter($this->to, $append=true);
-
- if ($this->text !== null) {
-
-
- $this->log("Appending string to " . $this->to->getPath());
-
-
-
- $lines = explode("\n", $this->text);
- foreach($lines as $line) {
- $this->log($line, Project::MSG_VERBOSE);
- }
-
- $writer->write($this->text);
-
- } else {
-
-
- 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);
- }
- }
-
-
- 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);
- }
- }
-
-
- 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);
- }
- }
-
- }
-
- $writer->close();
- }
-
-
- 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);
- }
- }
- }
- }
-
- private function appendFile(FileWriter $writer, PhingFile $f) {
- $in = FileUtils::getChainedReader(new FileReader($f), $this->filterChains, $this->project);
- while(-1 !== ($buffer = $in->read())) {
- $writer->write($buffer);
- }
- $this->log("Appending contents of " . $f->getPath() . " to " . $this->to->getPath());
- }
- }
|