Skip to content

Commit 2fa71ed

Browse files
committed
Also track removed files
1 parent 6803c9e commit 2fa71ed

File tree

3 files changed

+75
-31
lines changed

3 files changed

+75
-31
lines changed

src/MagentoHackathon/Composer/Magento/Deploystrategy/DeploystrategyAbstract.php

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace MagentoHackathon\Composer\Magento\Deploystrategy;
77

8+
use Composer\Util\Filesystem;
9+
810
/**
911
* Abstract deploy strategy
1012
*/
@@ -61,6 +63,19 @@ abstract class DeploystrategyAbstract
6163
*/
6264
protected $deployedFiles = array();
6365

66+
/**
67+
* List of files/folders which were
68+
* remove via this remove operation
69+
*
70+
* @var array
71+
*/
72+
protected $removedFiles = array();
73+
74+
/**
75+
* @var Filesystem
76+
*/
77+
protected $filesystem;
78+
6479
/**
6580
* Constructor
6681
*
@@ -69,8 +84,9 @@ abstract class DeploystrategyAbstract
6984
*/
7085
public function __construct($sourceDir, $destDir)
7186
{
72-
$this->destDir = $destDir;
73-
$this->sourceDir = $sourceDir;
87+
$this->destDir = $destDir;
88+
$this->sourceDir = $sourceDir;
89+
$this->filesystem = new Filesystem;
7490
}
7591

7692
/**
@@ -357,8 +373,8 @@ public function create($source, $dest)
357373
*/
358374
public function remove($source, $dest)
359375
{
360-
$sourcePath = $this->getSourceDir() . '/' . $this->removeTrailingSlash($source);
361-
$destPath = $this->getDestDir() . '/' . $dest;
376+
$sourcePath = $this->getSourceDir() . '/' . ltrim($this->removeTrailingSlash($source), '\\/');
377+
$destPath = $this->getDestDir() . '/' . ltrim($dest, '\\/');
362378

363379
// If source doesn't exist, check if it's a glob expression, otherwise we have nothing we can do
364380
if (!file_exists($sourcePath)) {
@@ -382,7 +398,8 @@ public function remove($source, $dest)
382398
if (basename($sourcePath) !== basename($destPath)) {
383399
$destPath .= '/' . basename($source);
384400
}
385-
self::rmdirRecursive($destPath);
401+
$this->filesystem->remove($destPath);
402+
$this->addRemovedFile($destPath);
386403
}
387404

388405
/**
@@ -395,20 +412,17 @@ public function rmEmptyDirsRecursive($dir, $stopDir = null)
395412
{
396413
$absoluteDir = $this->getDestDir() . '/' . $dir;
397414
if (is_dir($absoluteDir)) {
415+
398416
$iterator = new \RecursiveIteratorIterator(
399-
new \RecursiveDirectoryIterator($absoluteDir),
417+
new \RecursiveDirectoryIterator($absoluteDir, \RecursiveDirectoryIterator::SKIP_DOTS),
400418
\RecursiveIteratorIterator::CHILD_FIRST
401419
);
402420

403-
foreach ($iterator as $item) {
404-
/** @var SplFileInfo $item */
405-
$path = (string)$item;
406-
if (!strcmp($item->getFilename(), '.') || !strcmp($item->getFilename(), '..')) {
407-
continue;
408-
}
421+
if (iterator_count($iterator) > 0) {
409422
// The directory contains something, do not remove
410423
return;
411424
}
425+
412426
// RecursiveIteratorIterator have opened handle on $absoluteDir
413427
// that cause Windows to block the directory and not remove it until
414428
// the iterator will be destroyed.
@@ -427,24 +441,6 @@ public function rmEmptyDirsRecursive($dir, $stopDir = null)
427441
}
428442
}
429443

430-
/**
431-
* Recursively removes the specified directory or file
432-
*
433-
* @param $dir
434-
*/
435-
public static function rmdirRecursive($dir)
436-
{
437-
$fs = new \Composer\Util\Filesystem();
438-
if(is_dir($dir)){
439-
$result = $fs->removeDirectory($dir);
440-
}else{
441-
@unlink($dir);
442-
}
443-
444-
return;
445-
}
446-
447-
448444
/**
449445
* Create the module's files in the given destination.
450446
*
@@ -468,6 +464,17 @@ public function addDeployedFile($file)
468464
$this->deployedFiles[] = $file;
469465
}
470466

467+
/**
468+
* Add a file/folder to the list of removed files
469+
* @param string $file
470+
*/
471+
public function addRemovedFile($file)
472+
{
473+
//strip of destination deploy location
474+
$file = preg_replace(sprintf('/^%s/', preg_quote($this->getDestDir(), '/')), '', $file);
475+
$this->removedFiles[] = $file;
476+
}
477+
471478
/**
472479
* Get all the deployed files
473480
*
@@ -477,4 +484,14 @@ public function getDeployedFiles()
477484
{
478485
return $this->deployedFiles;
479486
}
487+
488+
/**
489+
* Get all the removed files
490+
*
491+
* @return array
492+
*/
493+
public function getRemovedFiles()
494+
{
495+
return $this->removedFiles;
496+
}
480497
}

src/MagentoHackathon/Composer/Magento/Deploystrategy/Symlink.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function createDelegate($source, $dest)
7272
if (file_exists($destPath) && is_dir($destPath)) {
7373
if (basename($sourcePath) === basename($destPath)) {
7474
if ($this->isForced()) {
75-
$this->rmdirRecursive($destPath);
75+
$this->filesystem->remove($destPath);
7676
} else {
7777
throw new \ErrorException("Target $dest already exists (set extra.magento-force to override)");
7878
}

tests/MagentoHackathon/Composer/Magento/Deploystrategy/AbstractTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,31 @@ public function testSourceAndTargetAreDirsDoExist()
409409
$this->assertFileType($testTargetContent, self::TEST_FILETYPE_FILE);
410410
}
411411
}
412+
413+
public function testCleanStoresAllRemovedFiles()
414+
{
415+
$directory = '/app/code/Vendor/Module';
416+
$file1 = $directory . '/file1.txt';
417+
$file2 = $directory . '/file2.txt';
418+
$this->mkdir($this->sourceDir . $directory);
419+
touch($this->sourceDir . $file1);
420+
touch($this->sourceDir . $file2);
421+
$this->strategy->setMappings(array(array($file1, $file1), array($file2, $file2)));
422+
423+
$this->strategy->deploy();
424+
425+
$this->assertFileExists($this->destDir . $file1);
426+
$this->assertFileExists($this->destDir . $file2);
427+
428+
$this->strategy->clean();
429+
430+
$this->assertFileNotExists($this->destDir . $file1);
431+
$this->assertFileNotExists($this->destDir . $file2);
432+
$this->assertFileNotExists($this->destDir . $directory);
433+
434+
$this->assertEquals(
435+
array($file1, $file2),
436+
$this->strategy->getRemovedFiles()
437+
);
438+
}
412439
}

0 commit comments

Comments
 (0)