Skip to content

Commit 38a013d

Browse files
committed
Merge pull request #156 from AydinHassan/log-deployed
Record added/removed files during installation/uninstallation of packages
2 parents c11a564 + 2fa71ed commit 38a013d

File tree

8 files changed

+200
-36
lines changed

8 files changed

+200
-36
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public function createDelegate($source, $dest)
105105
}
106106
} else {
107107
copy($item, $subDestPath);
108+
$this->addDeployedFile($subDestPath);
108109
}
109110
if (!is_readable($subDestPath)) {
110111
throw new \ErrorException("Could not create $subDestPath");

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

Lines changed: 75 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
*/
@@ -53,6 +55,27 @@ abstract class DeploystrategyAbstract
5355
*/
5456
protected $isForced = false;
5557

58+
/**
59+
* List of files/folders which were
60+
* created via any of the deploy methods
61+
*
62+
* @var array
63+
*/
64+
protected $deployedFiles = array();
65+
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+
5679
/**
5780
* Constructor
5881
*
@@ -61,8 +84,9 @@ abstract class DeploystrategyAbstract
6184
*/
6285
public function __construct($sourceDir, $destDir)
6386
{
64-
$this->destDir = $destDir;
65-
$this->sourceDir = $sourceDir;
87+
$this->destDir = $destDir;
88+
$this->sourceDir = $sourceDir;
89+
$this->filesystem = new Filesystem;
6690
}
6791

6892
/**
@@ -349,8 +373,8 @@ public function create($source, $dest)
349373
*/
350374
public function remove($source, $dest)
351375
{
352-
$sourcePath = $this->getSourceDir() . '/' . $this->removeTrailingSlash($source);
353-
$destPath = $this->getDestDir() . '/' . $dest;
376+
$sourcePath = $this->getSourceDir() . '/' . ltrim($this->removeTrailingSlash($source), '\\/');
377+
$destPath = $this->getDestDir() . '/' . ltrim($dest, '\\/');
354378

355379
// If source doesn't exist, check if it's a glob expression, otherwise we have nothing we can do
356380
if (!file_exists($sourcePath)) {
@@ -374,7 +398,8 @@ public function remove($source, $dest)
374398
if (basename($sourcePath) !== basename($destPath)) {
375399
$destPath .= '/' . basename($source);
376400
}
377-
self::rmdirRecursive($destPath);
401+
$this->filesystem->remove($destPath);
402+
$this->addRemovedFile($destPath);
378403
}
379404

380405
/**
@@ -387,20 +412,17 @@ public function rmEmptyDirsRecursive($dir, $stopDir = null)
387412
{
388413
$absoluteDir = $this->getDestDir() . '/' . $dir;
389414
if (is_dir($absoluteDir)) {
415+
390416
$iterator = new \RecursiveIteratorIterator(
391-
new \RecursiveDirectoryIterator($absoluteDir),
417+
new \RecursiveDirectoryIterator($absoluteDir, \RecursiveDirectoryIterator::SKIP_DOTS),
392418
\RecursiveIteratorIterator::CHILD_FIRST
393419
);
394420

395-
foreach ($iterator as $item) {
396-
/** @var SplFileInfo $item */
397-
$path = (string)$item;
398-
if (!strcmp($item->getFilename(), '.') || !strcmp($item->getFilename(), '..')) {
399-
continue;
400-
}
421+
if (iterator_count($iterator) > 0) {
401422
// The directory contains something, do not remove
402423
return;
403424
}
425+
404426
// RecursiveIteratorIterator have opened handle on $absoluteDir
405427
// that cause Windows to block the directory and not remove it until
406428
// the iterator will be destroyed.
@@ -419,24 +441,6 @@ public function rmEmptyDirsRecursive($dir, $stopDir = null)
419441
}
420442
}
421443

422-
/**
423-
* Recursively removes the specified directory or file
424-
*
425-
* @param $dir
426-
*/
427-
public static function rmdirRecursive($dir)
428-
{
429-
$fs = new \Composer\Util\Filesystem();
430-
if(is_dir($dir)){
431-
$result = $fs->removeDirectory($dir);
432-
}else{
433-
@unlink($dir);
434-
}
435-
436-
return;
437-
}
438-
439-
440444
/**
441445
* Create the module's files in the given destination.
442446
*
@@ -449,4 +453,45 @@ public static function rmdirRecursive($dir)
449453
*/
450454
abstract protected function createDelegate($source, $dest);
451455

456+
/**
457+
* Add a file/folder to the list of deployed files
458+
* @param string $file
459+
*/
460+
public function addDeployedFile($file)
461+
{
462+
//strip of destination deploy location
463+
$file = preg_replace(sprintf('/^%s/', preg_quote($this->getDestDir(), '/')), '', $file);
464+
$this->deployedFiles[] = $file;
465+
}
466+
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+
478+
/**
479+
* Get all the deployed files
480+
*
481+
* @return array
482+
*/
483+
public function getDeployedFiles()
484+
{
485+
return $this->deployedFiles;
486+
}
487+
488+
/**
489+
* Get all the removed files
490+
*
491+
* @return array
492+
*/
493+
public function getRemovedFiles()
494+
{
495+
return $this->removedFiles;
496+
}
452497
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function createDelegate($source, $dest)
8787
}
8888
} else {
8989
link($item, $subDestPath);
90+
$this->addDeployedFile($subDestPath);
9091
}
9192
if (!is_readable($subDestPath)) {
9293
throw new \ErrorException("Could not create $subDestPath");

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

Lines changed: 4 additions & 4 deletions
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
}
@@ -102,10 +102,10 @@ public function createDelegate($source, $dest)
102102
}
103103

104104
// Check we where able to create the symlink
105-
if (false === $destPath = @readlink($destPath)) {
106-
throw new \ErrorException("Symlink $destPath points to target $destPath");
105+
if (false === $newDestPath = @readlink($destPath)) {
106+
throw new \ErrorException("Symlink $newDestPath points to target $newDestPath");
107107
}
108-
108+
$this->addDeployedFile($destPath);
109109
return true;
110110
}
111111

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
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,33 @@ public function testWildcardCopyToExistingDir()
7373
$this->assertFileNotExists($this->destDir . DS . 'app' . DS . 'app' . DS . 'code' . DS . 'test.php');
7474

7575
}
76+
77+
public function testDeployedFilesAreStored()
78+
{
79+
$sourceRoot = 'root';
80+
$sourceContents = "subdir/subdir/test.xml";
81+
82+
$this->mkdir($this->sourceDir . DS . $sourceRoot . DS . dirname($sourceContents));
83+
touch($this->sourceDir . DS . $sourceRoot . DS . $sourceContents);
84+
85+
// intentionally using a differnt name to verify solution doesn't rely on identical src/dest paths
86+
$dest = "dest/root";
87+
$this->mkdir($this->destDir . DS . $dest);
88+
89+
$testTarget = $this->destDir . DS . $dest . DS . $sourceContents;
90+
$this->strategy->setCurrentMapping(array($sourceRoot, $dest));
91+
92+
$this->strategy->create($sourceRoot, $dest);
93+
$this->assertFileExists($testTarget);
94+
95+
$this->strategy->setIsForced(true);
96+
$this->strategy->create($sourceRoot, $dest);
97+
98+
$this->assertFileNotExists(dirname(dirname($testTarget)) . DS . basename($testTarget));
99+
100+
$this->assertSame(
101+
array('/dest/root/subdir/subdir/test.xml'),
102+
$this->strategy->getDeployedFiles()
103+
);
104+
}
76105
}

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,35 @@
22
namespace MagentoHackathon\Composer\Magento\Deploystrategy;
33

44
use org\bovigo\vfs\vfsStream;
5-
use org\bovigo\vfs\vfsStreamWrapper;
5+
use PHPUnit_Framework_TestCase;
66

77
if (!defined('DS')) {
88
define('DS', DIRECTORY_SEPARATOR);
99
}
1010

11-
class NoneTest extends \PHPUnit_Framework_TestCase
11+
class NoneTest extends PHPUnit_Framework_TestCase
1212
{
1313
const URL_VFS_ROOT = 'vfsroot';
1414

15+
/**
16+
* @var string
17+
*/
18+
protected $sourceDir;
19+
20+
/**
21+
* @var string
22+
*/
23+
protected $destDir;
24+
25+
/**
26+
* @var \MagentoHackathon\Composer\Magento\Deploystrategy\None
27+
*/
28+
protected $strategy;
29+
30+
/**
31+
* @param string $input
32+
* @return string
33+
*/
1534
protected function _getVfsUrl($input)
1635
{
1736
return vfsStream::url(self::URL_VFS_ROOT . DS . $input);
@@ -44,4 +63,27 @@ public function testCreate()
4463
$this->assertFalse(is_dir($this->_getVfsUrl('destDir' . DS . $dest)));
4564
}
4665

66+
public function testDeployedFilesIsEmpty()
67+
{
68+
$src = 'test1';
69+
$dest = 'test2';
70+
71+
//create the source directory
72+
mkdir($this->_getVfsUrl('sourceDir' . DS . $src), null, true);
73+
74+
$this->assertTrue(is_dir($this->_getVfsUrl('sourceDir' . DS . $src)));
75+
$this->assertFalse(is_dir($this->_getVfsUrl('destDir' . DS . $dest)));
76+
77+
//run the none deploy strategy
78+
$this->strategy->create($src, $dest);
79+
80+
//check that everything is still the same
81+
$this->assertTrue(is_dir($this->_getVfsUrl('sourceDir' . DS . $src)));
82+
$this->assertFalse(is_dir($this->_getVfsUrl('destDir' . DS . $dest)));
83+
84+
$this->assertSame(
85+
array(),
86+
$this->strategy->getDeployedFiles()
87+
);
88+
}
4789
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,23 @@ public function testEmptyDirectoryCleanup()
116116
$this->assertFileNotExists($this->destDir . $file);
117117
$this->assertFileNotExists($this->destDir . $directory);
118118
}
119+
120+
public function testDeployedFilesAreStored()
121+
{
122+
$src = 'local1.xml';
123+
$dest = 'local2.xml';
124+
touch($this->sourceDir . DIRECTORY_SEPARATOR . $src);
125+
$this->assertTrue(is_readable($this->sourceDir . DIRECTORY_SEPARATOR . $src));
126+
$this->assertFalse(is_readable($this->destDir . DIRECTORY_SEPARATOR . $dest));
127+
$this->strategy->create($src, $dest);
128+
$this->assertTrue(is_readable($this->destDir . DIRECTORY_SEPARATOR . $dest));
129+
unlink($this->destDir . DIRECTORY_SEPARATOR . $dest);
130+
$this->strategy->clean($this->destDir . DIRECTORY_SEPARATOR . $dest);
131+
$this->assertFalse(is_readable($this->destDir . DIRECTORY_SEPARATOR . $dest));
132+
133+
$this->assertSame(
134+
array('/local2.xml'),
135+
$this->strategy->getDeployedFiles()
136+
);
137+
}
119138
}

0 commit comments

Comments
 (0)