From 3ca90d7caa0ae4711abc2decf7ec32a32c55663e Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Thu, 16 Feb 2017 13:01:23 +0100 Subject: [PATCH 01/15] Fix for symlinks. --- src/PackageProject.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index 67576e7..5034d31 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -99,6 +99,9 @@ protected function getFiles() return $add; } + /** + * Mirror the directory to a temp directory. + */ protected function mirrorDir() { if (file_exists($this->tmpDir)) { @@ -110,12 +113,15 @@ protected function mirrorDir() $directoryIterator = new \RecursiveDirectoryIterator($this->dir); $iterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $item) { + if (is_link($file) && file_exists($file)) { + $this->fs->symlink($file->getLinkTarget(), $target); + continue; + } if ($item->isDir()) { $this->fs->mkdir($this->tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); + continue; } - else { - $this->fs->copy($item, $this->tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); - } + $this->fs->copy($item, $this->tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); } } From be03ce4c50da49545bcc2186e311f32a69c181ec Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Thu, 16 Feb 2017 13:22:53 +0100 Subject: [PATCH 02/15] Make the use of a temp dir optional. --- src/PackageProject.php | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index 5034d31..937c87a 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -39,6 +39,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. @@ -60,7 +67,6 @@ public function __construct($archiveFile, $dir = null, FileSystem $fs = null) $this->fs = is_null($fs) ? new Filesystem() : $fs; - $this->tmpDir = md5(time()); } /** @@ -80,6 +86,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. * @@ -93,7 +120,10 @@ protected function getFiles() $mirrorFinder = new Finder(); $mirrorFinder->ignoreDotFiles(false); $add = []; - foreach ($mirrorFinder->in($this->tmpDir)->depth(1) as $file) { + $mirrorFinder + ->in($this->tmpDir) + ->depth(1); + foreach ($mirrorFinder as $file) { $add[substr($file->getRealPath(), strlen(realpath($this->tmpDir)) + 1)] = $file->getRealPath(); } return $add; @@ -104,6 +134,11 @@ protected function getFiles() */ 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); } From 0d102d4f057b0c274e39154adf94c89eb0ff98bf Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Thu, 16 Feb 2017 13:27:58 +0100 Subject: [PATCH 03/15] Fix undefined variable. --- src/PackageProject.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index 937c87a..29cbc21 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -148,8 +148,8 @@ protected function mirrorDir() $directoryIterator = new \RecursiveDirectoryIterator($this->dir); $iterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $item) { - if (is_link($file) && file_exists($file)) { - $this->fs->symlink($file->getLinkTarget(), $target); + if (is_link($item) && file_exists($item)) { + $this->fs->symlink($item->getLinkTarget(), $target); continue; } if ($item->isDir()) { From efdfba373fad54ad1fc547b34b225590c67f8766 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Thu, 16 Feb 2017 14:14:34 +0100 Subject: [PATCH 04/15] Fix undefined variable target. --- src/PackageProject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index 29cbc21..6501082 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -149,7 +149,7 @@ protected function mirrorDir() $iterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $item) { if (is_link($item) && file_exists($item)) { - $this->fs->symlink($item->getLinkTarget(), $target); + $this->fs->symlink($item->getLinkTarget(), $this->tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); continue; } if ($item->isDir()) { From 7e9f61c2c8c99f4edd2367fccb1e2a220c9813f7 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Thu, 16 Feb 2017 14:20:37 +0100 Subject: [PATCH 05/15] Do not remove tmpDir if we didn't create one --- src/PackageProject.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index 6501082..7ab67f8 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -196,7 +196,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; } } From 9294e78cd593b08e137d5e825a737da50da2ef55 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Thu, 16 Feb 2017 14:26:11 +0100 Subject: [PATCH 06/15] Fix for broken links. --- src/PackageProject.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index 7ab67f8..b6bd9ee 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -148,8 +148,10 @@ protected function mirrorDir() $directoryIterator = new \RecursiveDirectoryIterator($this->dir); $iterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $item) { - if (is_link($item) && file_exists($item)) { - $this->fs->symlink($item->getLinkTarget(), $this->tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); + if (is_link($item)) { + if (file_exists($item)) { + $this->fs->symlink($item->getLinkTarget(), $this->tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); + } continue; } if ($item->isDir()) { From 286c1b9d8926759812b5542981e922979a253be5 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Thu, 16 Feb 2017 14:27:01 +0100 Subject: [PATCH 07/15] Fix useTmpDir. --- src/PackageProject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index b6bd9ee..41ace68 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -134,7 +134,7 @@ protected function getFiles() */ protected function mirrorDir() { - if ($this->useTmpDir) { + if (!$this->useTmpDir) { $this->tmpDir = $this->dir; return; } From 2043b99592454b58fa303183f11027bf53694be0 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Thu, 16 Feb 2017 14:37:14 +0100 Subject: [PATCH 08/15] Better check for broken link. --- src/PackageProject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index 41ace68..7201013 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -149,7 +149,7 @@ protected function mirrorDir() $iterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $item) { if (is_link($item)) { - if (file_exists($item)) { + if ($item->getRealPath() !== false) { $this->fs->symlink($item->getLinkTarget(), $this->tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); } continue; From 3af54a096a674eae033e16d1218e260508834212 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Fri, 17 Feb 2017 08:34:09 +0100 Subject: [PATCH 09/15] Group addModify calls to Archive_Tar to improve performance. --- src/PackageProject.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/PackageProject.php b/src/PackageProject.php index 7201013..35d26b3 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -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; @@ -203,4 +204,36 @@ public function run() } return $result; } + + /** + * {@inheritdoc} + */ + protected function archiveTar($archiveFile, $items) + { + if (!class_exists('Archive_Tar')) { + return Result::errorMissingPackage($this, 'Archive_Tar', 'pear/archive_tar'); + } + + $tar_object = new \Archive_Tar($archiveFile); + $addModify = array(); + foreach ($items as $placementLocation => $filesystemLocation) { + $p_remove_dir = $filesystemLocation; + $p_add_dir = $placementLocation; + if (is_file($filesystemLocation)) { + $p_remove_dir = dirname($filesystemLocation); + $p_add_dir = dirname($placementLocation); + if (basename($filesystemLocation) != basename($placementLocation)) { + return Result::error($this, "Tar archiver does not support renaming files during extraction; could not add $filesystemLocation as $placementLocation."); + } + } + // Group the addModify calls for better performance. + $addModify[$p_add_dir . '|' . $p_remove_dir][] = $filesystemLocation; + } + foreach ($addModify as $modifiers => $dirs) { + if (!call_user_func_array(array($tar_object, 'addModify'), array_merge(array($dirs), explode('|', $modifiers)))) { + return Result::error($this, "Could not add $filesystemLocation to the archive."); + } + } + return Result::success($this); + } } From 5133fcaac7a0be0f7f594f97bef1b7f49cb36904 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Fri, 17 Feb 2017 09:46:06 +0100 Subject: [PATCH 10/15] Filter out tmpDir when creating the mirror dir. --- src/PackageProject.php | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index 35d26b3..3ece7dc 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -145,13 +145,24 @@ protected function mirrorDir() } $this->fs->mkdir($this->tmpDir); + $tmpRealPath = realpath($this->tmpDir); $directoryIterator = new \RecursiveDirectoryIterator($this->dir); - $iterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST); - foreach ($iterator as $item) { + $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 . $iterator->getSubPathName()); + $this->fs->symlink($item->getLinkTarget(), $this->tmpDir . DIRECTORY_SEPARATOR . $filterIterator->getSubPathName()); } continue; } @@ -214,23 +225,23 @@ protected function archiveTar($archiveFile, $items) return Result::errorMissingPackage($this, 'Archive_Tar', 'pear/archive_tar'); } - $tar_object = new \Archive_Tar($archiveFile); + $tarObject = new \Archive_Tar($archiveFile); $addModify = array(); foreach ($items as $placementLocation => $filesystemLocation) { - $p_remove_dir = $filesystemLocation; - $p_add_dir = $placementLocation; + $removeDir = $filesystemLocation; + $addDir = $placementLocation; if (is_file($filesystemLocation)) { - $p_remove_dir = dirname($filesystemLocation); - $p_add_dir = dirname($placementLocation); + $removeDir = dirname($filesystemLocation); + $addDir = dirname($placementLocation); if (basename($filesystemLocation) != basename($placementLocation)) { return Result::error($this, "Tar archiver does not support renaming files during extraction; could not add $filesystemLocation as $placementLocation."); } } // Group the addModify calls for better performance. - $addModify[$p_add_dir . '|' . $p_remove_dir][] = $filesystemLocation; + $addModify[$addDir . '|' . $removeDir][] = $filesystemLocation; } foreach ($addModify as $modifiers => $dirs) { - if (!call_user_func_array(array($tar_object, 'addModify'), array_merge(array($dirs), explode('|', $modifiers)))) { + if (!call_user_func_array(array($tarObject, 'addModify'), array_merge(array($dirs), explode('|', $modifiers)))) { return Result::error($this, "Could not add $filesystemLocation to the archive."); } } From 506bb3d528ca13b22c473f8897e28e078ef516ac Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Fri, 17 Feb 2017 09:56:51 +0100 Subject: [PATCH 11/15] Fix undefined variable $iterator. --- src/PackageProject.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index 3ece7dc..37ff75f 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -167,10 +167,10 @@ function ($current) use ($tmpRealPath) continue; } if ($item->isDir()) { - $this->fs->mkdir($this->tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); + $this->fs->mkdir($this->tmpDir . DIRECTORY_SEPARATOR . $filterIterator->getSubPathName()); continue; } - $this->fs->copy($item, $this->tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); + $this->fs->copy($item, $this->tmpDir . DIRECTORY_SEPARATOR . $filterIterator->getSubPathName()); } } From 722461d5d7c7c3ccdee32022c04c4e2439adc1b5 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Fri, 17 Feb 2017 10:08:42 +0100 Subject: [PATCH 12/15] Skip . and .. folders. --- src/PackageProject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index 37ff75f..7c0ebb6 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -153,7 +153,7 @@ protected function mirrorDir() $recursiveIterator, function ($current) use ($tmpRealPath) { - return strpos($current->getRealPath(), $tmpRealPath) !== 0; + return !$current->isDot() && strpos($current->getRealPath(), $tmpRealPath) !== 0; } ); foreach ($filterIterator as $item) { From 00e6e2f48d575b725f8551077e5e63837599633f Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Fri, 17 Feb 2017 10:22:42 +0100 Subject: [PATCH 13/15] Skip . and .. folders. --- src/PackageProject.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index 7c0ebb6..b8ff079 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -147,13 +147,13 @@ protected function mirrorDir() $this->fs->mkdir($this->tmpDir); $tmpRealPath = realpath($this->tmpDir); - $directoryIterator = new \RecursiveDirectoryIterator($this->dir); + $directoryIterator = new \RecursiveDirectoryIterator($this->dir, \RecursiveDirectoryIterator::SKIP_DOTS); $recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST); $filterIterator = new \CallbackFilterIterator( $recursiveIterator, function ($current) use ($tmpRealPath) { - return !$current->isDot() && strpos($current->getRealPath(), $tmpRealPath) !== 0; + return strpos($current->getRealPath(), $tmpRealPath) !== 0; } ); foreach ($filterIterator as $item) { From a43ae946366ac8454e12efaefe886cbb835d81a1 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Fri, 17 Feb 2017 11:03:26 +0100 Subject: [PATCH 14/15] More performance improvements. --- src/PackageProject.php | 43 +----------------------------------------- 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index b8ff079..dc7a20e 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -118,16 +118,7 @@ protected function getFiles() { $this->mirrorDir(); $this->cleanMirrorDir(); - $mirrorFinder = new Finder(); - $mirrorFinder->ignoreDotFiles(false); - $add = []; - $mirrorFinder - ->in($this->tmpDir) - ->depth(1); - foreach ($mirrorFinder as $file) { - $add[substr($file->getRealPath(), strlen(realpath($this->tmpDir)) + 1)] = $file->getRealPath(); - } - return $add; + return array('' => realpath($this->tmpDir)); } /** @@ -215,36 +206,4 @@ public function run() } return $result; } - - /** - * {@inheritdoc} - */ - protected function archiveTar($archiveFile, $items) - { - if (!class_exists('Archive_Tar')) { - return Result::errorMissingPackage($this, 'Archive_Tar', 'pear/archive_tar'); - } - - $tarObject = new \Archive_Tar($archiveFile); - $addModify = array(); - foreach ($items as $placementLocation => $filesystemLocation) { - $removeDir = $filesystemLocation; - $addDir = $placementLocation; - if (is_file($filesystemLocation)) { - $removeDir = dirname($filesystemLocation); - $addDir = dirname($placementLocation); - if (basename($filesystemLocation) != basename($placementLocation)) { - return Result::error($this, "Tar archiver does not support renaming files during extraction; could not add $filesystemLocation as $placementLocation."); - } - } - // Group the addModify calls for better performance. - $addModify[$addDir . '|' . $removeDir][] = $filesystemLocation; - } - foreach ($addModify as $modifiers => $dirs) { - if (!call_user_func_array(array($tarObject, 'addModify'), array_merge(array($dirs), explode('|', $modifiers)))) { - return Result::error($this, "Could not add $filesystemLocation to the archive."); - } - } - return Result::success($this); - } } From c7959ab23ac72741f7d1d4d25439479952b91ab4 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Fri, 17 Feb 2017 11:09:00 +0100 Subject: [PATCH 15/15] Avoid adding the tar we're creating to the tar. --- src/PackageProject.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/PackageProject.php b/src/PackageProject.php index dc7a20e..7295c49 100644 --- a/src/PackageProject.php +++ b/src/PackageProject.php @@ -118,7 +118,16 @@ protected function getFiles() { $this->mirrorDir(); $this->cleanMirrorDir(); - return array('' => realpath($this->tmpDir)); + $mirrorFinder = new Finder(); + $mirrorFinder->ignoreDotFiles(false); + $add = []; + $mirrorFinder + ->in($this->tmpDir) + ->depth(0); + foreach ($mirrorFinder as $file) { + $add[substr($file->getRealPath(), strlen(realpath($this->tmpDir)) + 1)] = $file->getRealPath(); + } + return $add; } /**