Changeset 94

Show
Ignore:
Timestamp:
07/11/06 13:33:29 (2 years ago)
Author:
hans
Message:

ticket:8 - Fixed two inter-related issues in TarTask:

  1. Added support for includeEmptyDirs attribute, which is now ON by default.
  2. The test for whether archive is up-to-date was happening even when archive did not exist.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/2.2/classes/phing/tasks/ext/TarTask.php

    r59 r94  
    4545    private $tarFile; 
    4646    private $baseDir; 
    47  
     47        private $includeEmpty = true; // Whether to include empty dirs in the TAR 
     48         
    4849    private $longFileMode = "warn"; 
    4950 
     
    106107    public function setBasedir(PhingFile $baseDir) { 
    107108        $this->baseDir = $baseDir; 
     109    } 
     110 
     111    /** 
     112     * Set the include empty dirs flag. 
     113     * @param  boolean  Flag if empty dirs should be tarred too 
     114     * @return void 
     115     * @access public 
     116     */ 
     117    function setIncludeEmptyDirs($bool) { 
     118        $this->includeEmpty = (boolean) $bool; 
    108119    } 
    109120 
     
    191202            }                         
    192203             
    193             // check if tar is out of date with respect to each 
    194             // fileset 
    195             $upToDate = true; 
    196             foreach($this->filesets as $fs) { 
    197                 $files = $fs->getFiles($this->project); 
    198                 if (!$this->archiveIsUpToDate($files, $fs->getDir($this->project))) { 
    199                     $upToDate = false; 
    200                 } 
    201                 for ($i=0, $fcount=count($files); $i < $fcount; $i++) { 
    202                     if ($this->tarFile->equals(new PhingFile($fs->getDir($this->project), $files[$i]))) { 
    203                         throw new BuildException("A tar file cannot include itself", $this->getLocation()); 
    204                     } 
    205                 } 
    206             } 
    207              
    208             if ($upToDate) { 
    209                 $this->log("Nothing to do: " . $this->tarFile->__toString() . " is up to date.", PROJECT_MSG_INFO)
    210                 return; 
    211            
    212  
     204            // check if tar is out of date with respect to each fileset 
     205            if($this->tarFile->exists()) { 
     206                   $upToDate = true; 
     207                   foreach($this->filesets as $fs) { 
     208                       $files = $fs->getFiles($this->project, $this->includeEmpty); 
     209                       if (!$this->archiveIsUpToDate($files, $fs->getDir($this->project))) { 
     210                           $upToDate = false; 
     211                       } 
     212                       for ($i=0, $fcount=count($files); $i < $fcount; $i++) { 
     213                           if ($this->tarFile->equals(new PhingFile($fs->getDir($this->project), $files[$i]))) { 
     214                               throw new BuildException("A tar file cannot include itself", $this->getLocation()); 
     215                           } 
     216                       } 
     217                   } 
     218                    if ($upToDate) { 
     219                        $this->log("Nothing to do: " . $this->tarFile->__toString() . " is up to date.", PROJECT_MSG_INFO); 
     220                       return
     221                    } 
     222                       
     223                         
    213224            $this->log("Building tar: " . $this->tarFile->__toString(), PROJECT_MSG_INFO); 
    214225             
     
    219230             
    220231            foreach($this->filesets as $fs) {                                 
    221                     $files = $fs->getFiles($this->project); 
     232                    $files = $fs->getFiles($this->project, $this->includeEmpty); 
    222233                    if (count($files) > 1 && strlen($fs->getFullpath()) > 0) { 
    223234                        throw new BuildException("fullpath attribute may only " 
     
    226237                                                 . "single file."); 
    227238                    } 
    228                     // FIXME  
    229                     // Current model is only adding directories implicitly.  This 
    230                     // won't add any empty directories.  Perhaps modify TarFileSet::getFiles() 
    231                     // to also include empty directories.  Not high priority, since non-inclusion 
    232                     // of empty dirs is probably not unexpected behavior for TarTask. 
    233239                    $fsBasedir = $fs->getDir($this->project); 
    234240                    $filesToTar = array(); 
     
    289295     *    the baseDir for the project. 
    290296     */ 
    291     public function getFiles(Project $p) { 
     297    public function getFiles(Project $p, $includeEmpty = true) { 
     298     
    292299        if ($this->files === null) { 
     300         
    293301            $ds = $this->getDirectoryScanner($p); 
    294302            $this->files = $ds->getIncludedFiles(); 
    295         } 
     303             
     304            if ($includeEmpty) { 
     305             
     306                    // first any empty directories that will not be implicitly added by any of the files 
     307                                $implicitDirs = array(); 
     308                                foreach($this->files as $file) { 
     309                                        $implicitDirs[] = dirname($file); 
     310                                }  
     311                                 
     312                                $incDirs = $ds->getIncludedDirectories(); 
     313                                 
     314                                // we'll need to add to that list of implicit dirs any directories 
     315                                // that contain other *directories* (and not files), since otherwise 
     316                                // we get duplicate directories in the resulting tar 
     317                                foreach($incDirs as $dir) { 
     318                                        foreach($incDirs as $dircheck) { 
     319                                                if (!empty($dir) && $dir == dirname($dircheck)) { 
     320                                                        $implicitDirs[] = $dir; 
     321                                                } 
     322                                        } 
     323                                } 
     324                                 
     325                                $implicitDirs = array_unique($implicitDirs); 
     326                                 
     327                                // Now add any empty dirs (dirs not covered by the implicit dirs) 
     328                                // to the files array.  
     329                                 
     330                                foreach($incDirs as $dir) { // we cannot simply use array_diff() since we want to disregard empty/. dirs 
     331                                        if ($dir != "" && $dir != "." && !in_array($dir, $implicitDirs)) { 
     332                                                // it's an empty dir, so we'll add it. 
     333                                                $this->files[] = $dir; 
     334                                        } 
     335                                } 
     336                        } // if $includeEmpty 
     337                         
     338        } // if ($this->files===null) 
     339         
    296340        return $this->files; 
    297341    }