Changeset 110

Show
Ignore:
Timestamp:
08/17/06 16:23:36 (2 years ago)
Author:
hans
Message:

ticket:50 - Adding patch submitted by Joakim against UnzipTask and UntarTask.

Files:

Legend:

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

    r107 r110  
    3030 */ 
    3131abstract class ExtractBaseTask extends MatchingTask { 
     32    /** 
     33     * @var PhingFile $file 
     34     */ 
    3235    protected $file; 
    33     protected $destDir; 
     36    /** 
     37     * @var PhingFile $todir 
     38     */ 
     39    protected $todir; 
     40    protected $removepath; 
    3441    protected $filesets = array(); // all fileset objects assigned to this task 
    3542 
     
    5663     * @param PhingFile $baseDir 
    5764     */ 
    58     public function setDestDir(PhingFile $destDir) { 
    59         $this->destDir = $destDir; 
     65    public function setToDir(PhingFile $todir) { 
     66        $this->todir = $todir; 
     67    } 
     68     
     69    public function setRemovePath($removepath) 
     70    { 
     71        $this->removepath = $removepath; 
    6072    } 
    6173 
     
    6880        $this->validateAttributes(); 
    6981         
    70         $destinationFileSet = new FileSet(); 
    71         $destinationFileSet->setDir($this->destDir); 
    72         $destinationDirScanner = $destinationFileSet->getDirectoryScanner($this->project); 
    73         $destinationFiles = $destinationDirScanner->getIncludedFiles(); 
    74          
    7582        $filesToExtract = array(); 
    7683        if ($this->file !== null) { 
     
    7885                $filesToExtract[] = $this->file; 
    7986            } else { 
    80                 $this->log('Nothing to do: ' . $this->destDir->getAbsolutePath() . ' is up to date for ' .  $this->file->getCanonicalPath(), PROJECT_MSG_INFO); 
     87                $this->log('Nothing to do: ' . $this->todir->getAbsolutePath() . ' is up to date for ' .  $this->file->getCanonicalPath(), PROJECT_MSG_INFO); 
    8188            } 
    8289        } 
     
    97104                   $filesToExtract[] = $compressedArchiveFile; 
    98105                } else { 
    99                         $this->log('Nothing to do: ' . $this->destDir->getAbsolutePath() . ' is up to date for ' .  $compressedArchiveFile->getCanonicalPath(), PROJECT_MSG_INFO); 
     106                        $this->log('Nothing to do: ' . $this->todir->getAbsolutePath() . ' is up to date for ' .  $compressedArchiveFile->getCanonicalPath(), PROJECT_MSG_INFO); 
    100107                } 
    101108            } 
     
    109116    abstract protected function extractArchive(PhingFile $compressedArchiveFile); 
    110117     
    111     abstract protected function isDestinationUpToDate(PhingFile $compressedArchiveFile); 
     118    /** 
     119     * @param array $files array of filenames 
     120     * @param PhingFile $dir 
     121     * @return boolean 
     122     */ 
     123    protected function isDestinationUpToDate(PhingFile $compressedArchiveFile) { 
     124        if (!$compressedArchiveFile->exists()) { 
     125                throw new BuildException("Could not find file " . $compressedArchiveFile->__toString() . " to extract."); 
     126        } 
     127         
     128        $compressedArchiveContent = $this->listArchiveContent($compressedArchiveFile); 
     129        if(is_array($compressedArchiveContent)) { 
     130             
     131            $fileSystem = FileSystem::getFileSystem(); 
     132            $compressArchiveFilename = $compressArchivePathInfo['filename']; 
     133            foreach ($compressedArchiveContent as $compressArchivePathInfo) { 
     134                if(!empty($this->removepath) && strlen($compressArchiveFilename) >= strlen($this->removepath)) 
     135                { 
     136                    $compressArchiveFilename = preg_replace('/^' . $this->removepath . '/','', $compressArchiveFilename); 
     137                } 
     138                $compressArchivePath = new PhingFile($this->todir, $compressArchiveFilename); 
     139                 
     140                if(!$compressArchivePath->exists() || 
     141                    $fileSystem->compareMTimes($compressedArchiveFile->getCanonicalPath(), $compressArchivePath->getCanonicalPath()) == 1) { 
     142                    return false; 
     143                } 
     144            } 
     145             
     146        } 
     147         
     148        return true; 
     149    } 
     150     
     151    abstract protected function listArchiveContent(PhingFile $compressedArchiveFile); 
    112152     
    113153    /** 
     
    124164        } 
    125165 
    126         if ($this->destDir === null) { 
    127             throw new BuildException("Destdir must be set."); 
     166        if ($this->todir === null) { 
     167            throw new BuildException("todir must be set."); 
    128168        } 
    129169         
    130         if ($this->destDir !== null && $this->destDir->exists() && !$this->destDir->isDirectory()) { 
    131             throw new BuildException("Destdir must be a directory."); 
     170        if ($this->todir !== null && $this->todir->exists() && !$this->todir->isDirectory()) { 
     171            throw new BuildException("todir must be a directory."); 
    132172        } 
    133173 
  • branches/2.2/classes/phing/tasks/ext/UntarTask.php

    r107 r110  
    4343    protected function extractArchive(PhingFile $tarfile) 
    4444    { 
    45         $this->log("Extracting tar file: " . $tarfile->__toString() . ' to ' . $this->destDir->__toString(), PROJECT_MSG_INFO); 
     45        $this->log("Extracting tar file: " . $tarfile->__toString() . ' to ' . $this->todir->__toString(), PROJECT_MSG_INFO); 
    4646         
    4747        try { 
    4848                $tar = $this->initTar($tarfile); 
    49                 if(!$tar->extract($this->destDir->getAbsolutePath())) { 
    50                    throw new BuildException('Failed to extract tar file: ' . $tar->errorInfo(true)); 
     49                if(!$tar->extractModify($this->todir->getAbsolutePath(), $this->removepath)) { 
     50                    throw new BuildException('Failed to extract tar file: ' . $tarfile->getAbsolutePath()); 
    5151                } 
    5252        } catch (IOException $ioe) { 
     
    5656    } 
    5757     
    58     /** 
    59      * @param array $files array of filenames 
    60      * @param PhingFile $dir 
    61      * @return boolean 
    62      */ 
    63     protected function isDestinationUpToDate(PhingFile $tarfile) { 
    64         if (!$tarfile->exists()) { 
    65                 throw new BuildException("Could not find file " . $tarfile->__toString() . " to untar."); 
    66         } 
    67          
     58    protected function listArchiveContent(PhingFile $tarfile) 
     59    { 
    6860        $tar = $this->initTar($tarfile); 
    69         $tarContents = $tar->listContent(); 
    70         if(is_array($tarContents)) { 
    71             /* Get first file/dir to match against destination directory path to find 
    72                when the file was last unziped */ 
    73             $firstPathInfo = current($tarContents); 
    74             $firstPath = new PhingFile($this->destDir, $firstPathInfo['filename']); 
    75              
    76             $fileSystem = FileSystem::getFileSystem(); 
    77             if(!$firstPath->exists() || $fileSystem->compareMTimes($tarfile->getCanonicalPath(), $firstPath->getCanonicalPath()) == 1) { 
    78                 return false; 
    79             } 
    80         } 
    81          
    82         return true; 
     61        return $tar->listContent(); 
    8362    } 
    8463     
  • branches/2.2/classes/phing/tasks/ext/UnzipTask.php

    r107 r110  
    3636    protected function extractArchive(PhingFile $zipfile) 
    3737    { 
    38         $extractParams = array('add_path' => $this->destDir->getAbsolutePath()); 
    39         $this->log("Extracting zip: " . $zipfile->__toString() . ' to ' . $this->destDir->__toString(), PROJECT_MSG_INFO); 
     38        $extractParams = array('add_path' => $this->todir->getAbsolutePath()); 
     39        if(!empty($this->removepath)) 
     40        { 
     41            $extractParams['remove_path'] = $this->removepath; 
     42        } 
     43         
     44        $this->log("Extracting zip: " . $zipfile->__toString() . ' to ' . $this->todir->__toString(), PROJECT_MSG_INFO); 
    4045         
    4146        try { 
     
    4550                if(is_array($extractResponse)) { 
    4651                    foreach ($extractResponse as $extractedPath) { 
    47                         $this->log('Extracted' . $extractedPath['stored_filename'] . ' to ' . $this->destDir->__toString(), PROJECT_MSG_VERBOSE); 
     52                        $this->log('Extracted' . $extractedPath['stored_filename'] . ' to ' . $this->todir->__toString(), PROJECT_MSG_VERBOSE); 
    4853                    } 
    4954                } else if ($extractResponse === 0) { 
     
    5661    } 
    5762     
    58     /** 
    59      * @param array $files array of filenames 
    60      * @param PhingFile $dir 
    61      * @return boolean 
    62      */ 
    63     protected function isDestinationUpToDate(PhingFile $zipfile) { 
    64         if (!$zipfile->exists()) { 
    65                 throw new BuildException("Could not find file " . $zipfile->__toString() . " to unzip."); 
    66         } 
    67          
     63    protected function listArchiveContent(PhingFile $zipfile) 
     64    { 
    6865        $zip = new Archive_Zip($zipfile->getAbsolutePath()); 
    69         $zipContents = $zip->listContent(); 
    70         if(is_array($zipContents)) { 
    71             /* Get first file/dir to match against destination directory path to find 
    72                when the file was last unziped */ 
    73             $firstPathInfo = current($zipContents); 
    74             $firstPath = new PhingFile($this->destDir, $firstPathInfo['filename']); 
    75              
    76             $fileSystem = FileSystem::getFileSystem(); 
    77             if(!$firstPath->exists() || $fileSystem->compareMTimes($zipfile->getCanonicalPath(), $firstPath->getCanonicalPath()) == 1) { 
    78                 return false; 
    79             } 
    80         } 
    81          
    82         return true; 
     66        return $zip->listContent(); 
    8367    } 
    8468}