123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- require_once 'phing/Task.php';
- class DeleteTask extends Task {
- protected $file;
- protected $dir;
- protected $filesets = array();
- protected $includeEmpty = false;
- protected $quiet = false;
- protected $failonerror = true;
- protected $verbosity = Project::MSG_VERBOSE;
-
-
- private $filelists = array();
-
-
- function setFile(PhingFile $file) {
- $this->file = $file;
- }
-
- function setDir(PhingFile $dir) {
- $this->dir = $dir;
- }
-
- function setVerbose($verbosity) {
- if ($verbosity) {
- $this->verbosity = Project::MSG_INFO;
- } else {
- $this->verbosity = Project::MSG_VERBOSE;
- }
- }
-
- function setQuiet($bool) {
- $this->quiet = $bool;
- if ($this->quiet) {
- $this->failonerror = false;
- }
- }
-
- function setFailOnError($bool) {
- $this->failonerror = $bool;
- }
-
- function setIncludeEmptyDirs($includeEmpty) {
- $this->includeEmpty = (boolean) $includeEmpty;
- }
-
- function createFileSet() {
- $num = array_push($this->filesets, new FileSet());
- return $this->filesets[$num-1];
- }
-
-
- function createFileList() {
- $num = array_push($this->filelists, new FileList());
- return $this->filelists[$num-1];
- }
-
- function main() {
- if ($this->file === null && $this->dir === null && count($this->filesets) === 0 && count($this->filelists) === 0) {
- throw new BuildException("At least one of the file or dir attributes, or a fileset element, or a filelist element must be set.");
- }
- if ($this->quiet && $this->failonerror) {
- throw new BuildException("quiet and failonerror cannot both be set to true", $this->location);
- }
-
- if ($this->file !== null) {
- if ($this->file->exists()) {
- if ($this->file->isDirectory()) {
- $this->log("Directory " . $this->file->__toString() . " cannot be removed using the file attribute. Use dir instead.");
- } else {
- $this->log("Deleting: " . $this->file->__toString());
- try {
- $this->file->delete();
- } catch(Exception $e) {
- $message = "Unable to delete file " . $this->file->__toString() .": " .$e->getMessage();
- if($this->failonerror) {
- throw new BuildException($message);
- } else {
- $this->log($message, $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
- }
- }
- }
- } else {
- $this->log("Could not find file " . $this->file->getAbsolutePath() . " to delete.",Project::MSG_VERBOSE);
- }
- }
-
- if ($this->dir !== null && $this->dir->exists() && $this->dir->isDirectory()) {
- if ($this->verbosity === Project::MSG_VERBOSE) {
- $this->log("Deleting directory " . $this->dir->__toString());
- }
- $this->removeDir($this->dir);
- }
-
-
- foreach($this->filelists as $fl) {
- try {
- $files = $fl->getFiles($this->project);
- $this->removeFiles($fl->getDir($this->project), $files, $empty=array());
- } catch (BuildException $be) {
-
- if ($this->failonerror) {
- throw $be;
- } else {
- $this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
- }
- }
- }
-
-
- foreach($this->filesets as $fs) {
- try {
- $ds = $fs->getDirectoryScanner($this->project);
- $files = $ds->getIncludedFiles();
- $dirs = $ds->getIncludedDirectories();
- $this->removeFiles($fs->getDir($this->project), $files, $dirs);
- } catch (BuildException $be) {
-
- if ($this->failonerror) {
- throw $be;
- } else {
- $this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
- }
- }
- }
- }
-
-
- private function removeDir($d) {
- $list = $d->listDir();
- if ($list === null) {
- $list = array();
- }
-
- foreach($list as $s) {
- $f = new PhingFile($d, $s);
- if ($f->isDirectory()) {
- $this->removeDir($f);
- } else {
- $this->log("Deleting " . $f->__toString(), $this->verbosity);
- try {
- $f->delete();
- } catch (Exception $e) {
- $message = "Unable to delete file " . $f->__toString() . ": " . $e->getMessage();
- if($this->failonerror) {
- throw new BuildException($message);
- } else {
- $this->log($message, $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
- }
- }
- }
- }
- $this->log("Deleting directory " . $d->getAbsolutePath(), $this->verbosity);
- try {
- $d->delete();
- } catch (Exception $e) {
- $message = "Unable to delete directory " . $d->__toString() . ": " . $e->getMessage();
- if($this->failonerror) {
- throw new BuildException($message);
- } else {
- $this->log($message, $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
- }
- }
- }
-
- private function removeFiles(PhingFile $d, &$files, &$dirs) {
- if (count($files) > 0) {
- $this->log("Deleting " . count($files) . " files from " . $d->__toString());
- for ($j=0,$_j=count($files); $j < $_j; $j++) {
- $f = new PhingFile($d, $files[$j]);
- $this->log("Deleting " . $f->getAbsolutePath(), $this->verbosity);
- try {
- $f->delete();
- } catch (Exception $e) {
- $message = "Unable to delete file " . $f->__toString() . ": " . $e->getMessage();
- if($this->failonerror) {
- throw new BuildException($message);
- } else {
- $this->log($message, $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
- }
- }
- }
- }
- if (count($dirs) > 0 && $this->includeEmpty) {
- $dirCount = 0;
- for ($j=count($dirs)-1; $j>=0; --$j) {
- $dir = new PhingFile($d, $dirs[$j]);
- $dirFiles = $dir->listDir();
- if ($dirFiles === null || count($dirFiles) === 0) {
- $this->log("Deleting " . $dir->__toString(), $this->verbosity);
- try {
- $dir->delete();
- $dirCount++;
- } catch (Exception $e) {
- $message="Unable to delete directory " . $dir->__toString();
- if($this->failonerror) {
- throw new BuildException($message);
- } else {
- $this->log($message, $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
- }
- }
- }
- }
- if ($dirCount > 0) {
- $this->log("Deleted $dirCount director" . ($dirCount==1 ? "y" : "ies") . " from " . $d->__toString());
- }
- }
- }
- }
|