Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignorelist to pack command. #964

Merged
merged 3 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/tasks/Archive.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ $this->taskPack(
->add('README') // Puts file 'README' in archive at the root
->add('project') // Puts entire contents of directory 'project' in archinve inside 'project'
->addFile('dir/file.txt', 'file.txt') // Takes 'file.txt' from cwd and puts it in archive inside 'dir'.
->exclude(['dir\/.*.zip', '.*.md']) // Add regex (or array of regex) to the excluded patterns list.
->run();
?>
```
Expand All @@ -47,4 +48,5 @@ $this->taskPack(
* `addFile($placementLocation, $filesystemLocation)` Add an item to the archive. Like file_exists(), the parameter
* `addDir($placementLocation, $filesystemLocation)` Alias for addFile, in case anyone has angst about using
* `add($item)` Add a file or directory, or list of same to the archive.
* `exclude($ignoreList)` Allow files or folder to be excluded from the archive. Use regex, without enclosing slashes.

27 changes: 26 additions & 1 deletion src/Task/Archive/Pack.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* ->add('README') // Puts file 'README' in archive at the root
* ->add('project') // Puts entire contents of directory 'project' in archinve inside 'project'
* ->addFile('dir/file.txt', 'file.txt') // Takes 'file.txt' from cwd and puts it in archive inside 'dir'.
* ->exclude(['dir\/.*.zip', '.*.md']) // Add regex (or array of regex) to the excluded patterns list.
* ->run();
* ?>
* ```
Expand All @@ -37,6 +38,12 @@ class Pack extends BaseTask implements PrintedInterface
*/
private $archiveFile;

/**
* A list of regex patterns to exclude from the archive.
*
* @var array
*/
private $ignoreList;
/**
* Construct the class.
*
Expand All @@ -48,6 +55,7 @@ class Pack extends BaseTask implements PrintedInterface
public function __construct($archiveFile)
{
$this->archiveFile = $archiveFile;
$this->ignoreList = [];
}

/**
Expand Down Expand Up @@ -134,6 +142,20 @@ public function add($item)
return $this;
}

/**
* Allow files or folder to be excluded from the archive. Use regex, without enclosing slashes.
*
* @param string|string[]
* A regex (or array of) to be excluded.
*
* @return $this
*/
public function exclude($ignoreList)
{
$this->ignoreList = array_merge($this->ignoreList, (array) $ignoreList);
return $this;
}

/**
* Create a zip archive for distribution.
*
Expand Down Expand Up @@ -184,6 +206,7 @@ protected function archiveTar($archiveFile, $items)
}

$tar_object = new \Archive_Tar($archiveFile);
$tar_object->setIgnoreList($this->ignoreList);
foreach ($items as $placementLocation => $filesystemLocation) {
$p_remove_dir = $filesystemLocation;
$p_add_dir = $placementLocation;
Expand Down Expand Up @@ -236,7 +259,9 @@ protected function addItemsToZip($zip, $items)
foreach ($items as $placementLocation => $filesystemLocation) {
if (is_dir($filesystemLocation)) {
$finder = new Finder();
$finder->files()->in($filesystemLocation)->ignoreDotFiles(false);
// Add slashes so Symfony Finder patterns work like Archive_Tar ones.
$zipIgnoreList = preg_filter('/^|$/', '/', $this->ignoreList);
$finder->files()->in($filesystemLocation)->ignoreDotFiles(false)->notName($zipIgnoreList)->notPath($zipIgnoreList);

foreach ($finder as $file) {
// Replace Windows slashes or resulting zip will have issues on *nixes.
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/PackExtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public function testPackExtract($archiveType)
// an archive for it located in 'deep'
$result = $this->taskPack("deeply.$archiveType")
->add(['deep' => 'some/deeply'])
->exclude(['structu3.*.re'])
->exclude('nested4')
->run();
$this->assertTrue($result->wasSuccessful(), $result->getMessage());
$this->assertFileExists("deeply.$archiveType");
Expand All @@ -72,6 +74,9 @@ public function testPackExtract($archiveType)
$this->assertDirectoryExists("extracted-$archiveType");
$this->assertDirectoryExists("extracted-$archiveType/nested");
$this->assertFileExists("extracted-$archiveType/nested/structu.re");
$this->assertFileNotExists("extracted-$archiveType/nested3/structu31.re");
$this->assertFileNotExists("extracted-$archiveType/nested3/structu32.re");
$this->assertDirectoryNotExists("extracted-$archiveType/nested4");
// Next, we'll extract the same archive again, this time preserving
// the top-level folder.
$this->taskExtract("deeply.$archiveType")
Expand All @@ -81,10 +86,15 @@ public function testPackExtract($archiveType)
$this->assertDirectoryExists("preserved-$archiveType");
$this->assertDirectoryExists("preserved-$archiveType/deep/nested");
$this->assertFileExists("preserved-$archiveType/deep/nested/structu.re");
$this->assertFileNotExists("preserved-$archiveType/deep/nested3/structu31.re");
$this->assertFileNotExists("preserved-$archiveType/deep/nested3/structu32.re");
$this->assertDirectoryNotExists("preserved-$archiveType/deep/nested4");
// Make another archive, this time composed of fanciful locations
$result = $this->taskPack("composed.$archiveType")
->add(['a/b/existing_file' => 'some/deeply/existing_file'])
->add(['x/y/z/structu.re' => 'some/deeply/nested/structu.re'])
->add(['missing_files' => 'some/deeply/nested3'])
->exclude(['structu3.*.re'])
->run();
$this->assertTrue($result->wasSuccessful(), $result->getMessage());
$this->assertFileExists("composed.$archiveType");
Expand All @@ -98,6 +108,8 @@ public function testPackExtract($archiveType)
$this->assertDirectoryExists("decomposed-$archiveType");
$this->assertDirectoryExists("decomposed-$archiveType/x/y/z");
$this->assertFileExists("decomposed-$archiveType/x/y/z/structu.re");
$this->assertFileNotExists("decomposed-$archiveType/missing_files/structu31.re");
$this->assertFileNotExists("decomposed-$archiveType/missing_files/structu32.re");
$this->assertDirectoryExists("decomposed-$archiveType/a/b");
$this->assertFileExists("decomposed-$archiveType/a/b/existing_file");

Expand Down