-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHandler.php
More file actions
108 lines (90 loc) · 2.46 KB
/
Handler.php
File metadata and controls
108 lines (90 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace TopFloor\ComposerCleanupVcsDirs;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class Handler {
/**
* @var Composer
*/
protected $composer;
/**
* @var IOInterface
*/
protected $io;
/**
* @var Filesystem
*/
protected $fs;
/**
* Handler constructor.
* @param \Composer\Composer $composer
* @param \Composer\IO\IOInterface $io
*/
public function __construct(Composer $composer, IOInterface $io) {
$this->composer = $composer;
$this->io = $io;
$this->fs = new Filesystem();
}
/**
* @param $parentDir
* @param bool $excludeRoot
* @return \Symfony\Component\Finder\Finder|\Symfony\Component\Finder\SplFileInfo[]
*/
public function getVcsDirs($parentDir, $excludeRoot = false) {
$finder = new Finder();
$iterator = $finder
->directories()
->in($excludeRoot ? $parentDir : './')
->ignoreVCS(false)
->ignoreDotFiles(false)
->exclude(['node_modules', '.git/*'])
->name('.git');
if ($excludeRoot) {
$iterator->depth('> 0');
}
else {
$iterator->path($parentDir);
}
$extra = $this->composer->getPackage()->getExtra();
if (isset($extra['cleanup-vcs-dirs']) && !empty($extra['cleanup-vcs-dirs']['exclude'])) {
foreach ((array) $extra['cleanup-vcs-dirs']['exclude'] as $pattern) {
$iterator->notPath($pattern);
}
}
return $iterator;
}
/**
* @param $parentDir
* @param bool $excludeRoot
*/
public function cleanupVcsDirs($parentDir, $excludeRoot = false) {
$dirs = [];
foreach ($this->getVcsDirs($parentDir, $excludeRoot) as $dir) {
$this->io->write(sprintf("<info>Deleting %s directory from %s</info>", $dir->getBasename(), $dir->getRealPath()));
$dirs[] = $dir;
}
$this->deleteVcsDirs($dirs);
}
/**
* @param array $dirs
*/
public function deleteVcsDirs(array $dirs) {
/** @var SplFileInfo $dir */
foreach ($dirs as $dir) {
$this->fs->removeDirectory($dir->getRealPath());
}
}
/**
* @param \Composer\Package\PackageInterface $package
*/
public function onPostPackageEvent(PackageInterface $package) {
$packagePath = $this->composer->getInstallationManager()->getInstallPath($package);
if (!empty($packagePath)) {
$this->cleanupVcsDirs($packagePath);
}
}
}