Spaces:
Sleeping
Sleeping
File size: 14,127 Bytes
07c3cdd | 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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | <?php
/*
* $Id: CopyTask.php 3076 2006-12-18 08:52:12Z fabien $
*
* 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/TaskPhing.php';
include_once 'phing/system/io/PhingFile.php';
include_once 'phing/util/FileUtils.php';
include_once 'phing/util/SourceFileScanner.php';
include_once 'phing/mappers/IdentityMapper.php';
include_once 'phing/mappers/FlattenMapper.php';
/**
* A phing copy task. Copies a file or directory to a new file
* or directory. Files are only copied if the source file is newer
* than the destination file, or when the destination file does not
* exist. It is possible to explictly overwrite existing files.
*
* @author Andreas Aderhold, andi@binarycloud.com
* @version $Revision: 1.16 $ $Date: 2006-06-12 21:46:05 +0200 (Mon, 12 Jun 2006) $
* @package phing.tasks.system
*/
class CopyTask extends TaskPhing {
protected $file = null; // the source file (from xml attribute)
protected $destFile = null; // the destiantion file (from xml attribute)
protected $destDir = null; // the destination dir (from xml attribute)
protected $overwrite = false; // overwrite destination (from xml attribute)
protected $preserveLMT = true; // sync timestamps (from xml attribute)
protected $includeEmpty = true; // include empty dirs? (from XML)
protected $flatten = false; // apply the FlattenMapper right way (from XML)
protected $mapperElement = null;
protected $fileCopyMap = array(); // asoc array containing mapped file names
protected $dirCopyMap = array(); // asoc array containing mapped file names
protected $fileUtils = null; // a instance of fileutils
protected $filesets = array(); // all fileset objects assigned to this task
protected $filterChains = array(); // all filterchains objects assigned to this task
protected $verbosity = PROJECT_MSG_VERBOSE;
/**
* Sets up this object internal stuff. i.e. the Fileutils instance
*
* @return object The CopyTask instnace
* @access public
*/
function __construct() {
$this->fileUtils = new FileUtils();
}
/**
* Set the overwrite flag. IntrospectionHelper takes care of
* booleans in set* methods so we can assume that the right
* value (boolean primitive) is coming in here.
*
* @param boolean Overwrite the destination file(s) if it/they already exist
* @return void
* @access public
*/
function setOverwrite($bool) {
$this->overwrite = (boolean) $bool;
}
/**
* Used to force listing of all names of copied files.
* @param boolean $verbosity
*/
function setVerbose($verbosity) {
if ($verbosity) {
$this->verbosity = PROJECT_MSG_INFO;
} else {
$this->verbosity = PROJECT_MSG_VERBOSE;
}
}
/**
* Set the preserve timestmap flag. IntrospectionHelper takes care of
* booleans in set* methods so we can assume that the right
* value (boolean primitive) is coming in here.
*
* @param boolean Preserve the timestamp on the destination file
* @return void
* @access public
*/
function setTstamp($bool) {
$this->preserveLMT = (boolean) $bool;
}
/**
* Set the include empty dirs flag. IntrospectionHelper takes care of
* booleans in set* methods so we can assume that the right
* value (boolean primitive) is coming in here.
*
* @param boolean Flag if empty dirs should be cpoied too
* @return void
* @access public
*/
function setIncludeEmptyDirs($bool) {
$this->includeEmpty = (boolean) $bool;
}
/**
* Set the file. We have to manually take care of the
* type that is coming due to limited type support in php
* in and convert it manually if neccessary.
*
* @param string/object The source file. Either a string or an PhingFile object
* @return void
* @access public
*/
function setFile(PhingFile $file) {
$this->file = $file;
}
/**
* Set the toFile. We have to manually take care of the
* type that is coming due to limited type support in php
* in and convert it manually if neccessary.
*
* @param string/object The dest file. Either a string or an PhingFile object
* @return void
* @access public
*/
function setTofile(PhingFile $file) {
$this->destFile = $file;
}
/**
* Set the toDir. We have to manually take care of the
* type that is coming due to limited type support in php
* in and convert it manually if neccessary.
*
* @param string/object The directory, either a string or an PhingFile object
* @return void
* @access public
*/
function setTodir(PhingFile $dir) {
$this->destDir = $dir;
}
/**
* Nested creator, creates a FileSet for this task
*
* @access public
* @return object The created fileset object
*/
function createFileSet() {
$num = array_push($this->filesets, new FileSet());
return $this->filesets[$num-1];
}
/**
* Creates a filterchain
*
* @access public
* @return object The created filterchain object
*/
function createFilterChain() {
$num = array_push($this->filterChains, new FilterChain($this->project));
return $this->filterChains[$num-1];
}
/**
* Nested creator, creates one Mapper for this task
*
* @access public
* @return object The created Mapper type object
* @throws BuildException
*/
function createMapper() {
if ($this->mapperElement !== null) {
throw new BuildException("Cannot define more than one mapper", $this->location);
}
$this->mapperElement = new Mapper($this->project);
return $this->mapperElement;
}
/**
* The main entry point where everything gets in motion.
*
* @access public
* @return true on success
* @throws BuildException
*/
function main() {
$this->validateAttributes();
if ($this->file !== null) {
if ($this->file->exists()) {
if ($this->destFile === null) {
$this->destFile = new PhingFile($this->destDir, (string) $this->file->getName());
}
if ($this->overwrite === true || ($this->file->lastModified() > $this->destFile->lastModified())) {
$this->fileCopyMap[$this->file->getAbsolutePath()] = $this->destFile->getAbsolutePath();
} else {
$this->log($this->file->getName()." omitted, is up to date");
}
} else {
// terminate build
throw new BuildException("Could not find file " . $this->file->__toString() . " to copy.");
}
}
$project = $this->getProject();
// process filesets
foreach($this->filesets as $fs) {
$ds = $fs->getDirectoryScanner($project);
$fromDir = $fs->getDir($project);
$srcFiles = $ds->getIncludedFiles();
$srcDirs = $ds->getIncludedDirectories();
$this->_scan($fromDir, $this->destDir, $srcFiles, $srcDirs);
}
// go and copy the stuff
$this->doWork();
if ($this->destFile !== null) {
$this->destDir = null;
}
}
/**
* Validates attributes coming in from XML
*
* @access private
* @return void
* @throws BuildException
*/
private function validateAttributes() {
if ($this->file === null && count($this->filesets) === 0) {
throw new BuildException("CopyTask. Specify at least one source - a file or a fileset.");
}
if ($this->destFile !== null && $this->destDir !== null) {
throw new BuildException("Only one destfile and destdir may be set.");
}
if ($this->destFile === null && $this->destDir === null) {
throw new BuildException("One destfile or destdir must be set.");
}
if ($this->file !== null && $this->file->exists() && $this->file->isDirectory()) {
throw new BuildException("Use a fileset to copy directories.");
}
if ($this->destFile !== null && count($this->filesets) > 0) {
throw new BuildException("Cannot concatenate multiple files into a single file.");
}
if ($this->destFile !== null) {
$this->destDir = new PhingFile($this->destFile->getParent());
}
}
/**
* Compares source files to destination files to see if they
* should be copied.
*
* @access private
* @return void
*/
private function _scan(&$fromDir, &$toDir, &$files, &$dirs) {
/* mappers should be generic, so we get the mappers here and
pass them on to builMap. This method is not redundan like it seems */
$mapper = null;
if ($this->mapperElement !== null) {
$mapper = $this->mapperElement->getImplementation();
} else if ($this->flatten) {
$mapper = new FlattenMapper();
} else {
$mapper = new IdentityMapper();
}
$this->buildMap($fromDir, $toDir, $files, $mapper, $this->fileCopyMap);
$this->buildMap($fromDir, $toDir, $dirs, $mapper, $this->dirCopyMap);
}
/**
* Builds a map of filenames (from->to) that should be copied
*
* @access private
* @return void
*/
private function buildMap(&$fromDir, &$toDir, &$names, &$mapper, &$map) {
$toCopy = null;
if ($this->overwrite) {
$v = array();
foreach($names as $name) {
$result = $mapper->main($name);
if ($result !== null) {
$v[] = $name;
}
}
$toCopy = $v;
} else {
$ds = new SourceFileScanner($this);
$toCopy = $ds->restrict($names, $fromDir, $toDir, $mapper);
}
for ($i=0,$_i=count($toCopy); $i < $_i; $i++) {
$src = new PhingFile($fromDir, $toCopy[$i]);
$mapped = $mapper->main($toCopy[$i]);
$dest = new PhingFile($toDir, $mapped[0]);
$map[$src->getAbsolutePath()] = $dest->getAbsolutePath();
}
}
/**
* Actually copies the files
*
* @access private
* @return void
* @throws BuildException
*/
private function doWork() {
// These "slots" allow filters to retrieve information about the currently-being-process files
$fromSlot = $this->getRegisterSlot("currentFromFile");
$fromBasenameSlot = $this->getRegisterSlot("currentFromFile.basename");
$toSlot = $this->getRegisterSlot("currentToFile");
$toBasenameSlot = $this->getRegisterSlot("currentToFile.basename");
$mapSize = count($this->fileCopyMap);
$total = $mapSize;
if ($mapSize > 0) {
$this->log("Copying ".$mapSize." file".(($mapSize) === 1 ? '' : 's')." to ". $this->destDir->getAbsolutePath());
// walks the map and actually copies the files
$count=0;
foreach($this->fileCopyMap as $from => $to) {
if ($from === $to) {
$this->log("Skipping self-copy of " . $from, $this->verbosity);
$total--;
continue;
}
$this->log("From ".$from." to ".$to, $this->verbosity);
try { // try to copy file
$fromFile = new PhingFile($from);
$toFile = new PhingFile($to);
$fromSlot->setValue($fromFile->getPath());
$fromBasenameSlot->setValue($fromFile->getName());
$toSlot->setValue($toFile->getPath());
$toBasenameSlot->setValue($toFile->getName());
$this->fileUtils->copyFile($fromFile, $toFile, $this->overwrite, $this->preserveLMT, $this->filterChains, $this->getProject());
$count++;
} catch (IOException $ioe) {
$this->log("Failed to copy " . $from . " to " . $to . ": " . $ioe->getMessage(), PROJECT_MSG_ERR);
}
}
}
// handle empty dirs if appropriate
if ($this->includeEmpty) {
$destdirs = array_values($this->dirCopyMap);
$count = 0;
foreach ($destdirs as $destdir) {
$d = new PhingFile((string) $destdir);
if (!$d->exists()) {
if (!$d->mkdirs()) {
$this->log("Unable to create directory " . $d->__toString(), PROJECT_MSG_ERR);
} else {
$count++;
}
}
}
if ($count > 0) {
$this->log("Copied ".$count." empty director" . ($count == 1 ? "y" : "ies") . " to " . $this->destDir->getAbsolutePath());
}
}
}
}
|