Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Avoid adding the tar we're creating to the tar.
  More performance improvements.
  Skip . and .. folders.
  Skip . and .. folders.
  Fix undefined variable $iterator.
  Filter out tmpDir when creating the mirror dir.
  Group addModify calls to Archive_Tar to improve performance.
  Better check for broken link.
  Fix useTmpDir.
  Fix for broken links.
  Do not remove tmpDir if we didn't create one
  Fix undefined variable target.
  Fix undefined variable.
  Make the use of a temp dir optional.
  Fix for symlinks.
  • Loading branch information
Jelle-S committed Feb 17, 2017
2 parents d048e22 + c7959ab commit d70d526
Showing 1 changed file with 68 additions and 11 deletions.
79 changes: 68 additions & 11 deletions src/PackageProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DigipolisGent\Robo\Task\Package;

use Robo\Result;
use Robo\Task\Archive\Pack;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -39,6 +40,13 @@ class PackageProject extends Pack
*/
protected $ignoreFileNames = [];

/**
* Whether or not to use a temporary directory.
*
* @var bool
*/
protected $useTmpDir = false;


/**
* Create a new PackageProject task.
Expand All @@ -60,7 +68,6 @@ public function __construct($archiveFile, $dir = null, FileSystem $fs = null)
$this->fs = is_null($fs)
? new Filesystem()
: $fs;
$this->tmpDir = md5(time());
}

/**
Expand All @@ -80,6 +87,27 @@ public function ignoreFileNames($fileNames)
return $this;
}

/**
* Whether or not to use a temporary directory. Files that should not be
* packaged will be deleted when creating the package. When we use a
* temporary directory, we copy all the files to that directory and remove
* the files that should not be packaged from that temporary directory, so
* that your original directory stays the same. If not the files will be
* deleted from the original directory.
*
* @param bool $use
* Whether or not to use a temporary directory.
*
* @return $this
*/
public function useTmpDir($use = null)
{
$this->useTmpDir = is_null($use)
? true
: $use;
return $this;
}

/**
* Get the files and directories to package.
*
Expand All @@ -93,29 +121,56 @@ protected function getFiles()
$mirrorFinder = new Finder();
$mirrorFinder->ignoreDotFiles(false);
$add = [];
foreach ($mirrorFinder->in($this->tmpDir)->depth(1) as $file) {
$mirrorFinder
->in($this->tmpDir)
->depth(0);
foreach ($mirrorFinder as $file) {
$add[substr($file->getRealPath(), strlen(realpath($this->tmpDir)) + 1)] = $file->getRealPath();
}
return $add;
}

/**
* Mirror the directory to a temp directory.
*/
protected function mirrorDir()
{
if (!$this->useTmpDir) {
$this->tmpDir = $this->dir;
return;
}
$this->tmpDir = md5(time());
if (file_exists($this->tmpDir)) {
$this->fs->remove($this->tmpDir);
}

$this->fs->mkdir($this->tmpDir);

$directoryIterator = new \RecursiveDirectoryIterator($this->dir);
$iterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $item) {
if ($item->isDir()) {
$this->fs->mkdir($this->tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
$tmpRealPath = realpath($this->tmpDir);

$directoryIterator = new \RecursiveDirectoryIterator($this->dir, \RecursiveDirectoryIterator::SKIP_DOTS);
$recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
$filterIterator = new \CallbackFilterIterator(
$recursiveIterator,
function ($current) use ($tmpRealPath)
{
return strpos($current->getRealPath(), $tmpRealPath) !== 0;
}
);
foreach ($filterIterator as $item) {
if (strpos($item->getRealPath(), $tmpRealPath) === 0) {
continue;
}
if (is_link($item)) {
if ($item->getRealPath() !== false) {
$this->fs->symlink($item->getLinkTarget(), $this->tmpDir . DIRECTORY_SEPARATOR . $filterIterator->getSubPathName());
}
continue;
}
else {
$this->fs->copy($item, $this->tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
if ($item->isDir()) {
$this->fs->mkdir($this->tmpDir . DIRECTORY_SEPARATOR . $filterIterator->getSubPathName());
continue;
}
$this->fs->copy($item, $this->tmpDir . DIRECTORY_SEPARATOR . $filterIterator->getSubPathName());
}
}

Expand Down Expand Up @@ -155,7 +210,9 @@ public function run()
}
$this->add($this->getFiles());
$result = parent::run();
$this->fs->remove($this->tmpDir);
if ($this->useTmpDir) {
$this->fs->remove($this->tmpDir);
}
return $result;
}
}

0 comments on commit d70d526

Please sign in to comment.