forked from ubccr/xdmod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
composer.scripts.php
95 lines (85 loc) · 2.74 KB
/
composer.scripts.php
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
<?php
namespace Xdmod;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Composer\Script\Event;
class ComposerScripts
{
/**
* A mapping of package names to lists of files to keep post-install.
*
* @var array
*/
private static $packageWhitelists = array(
'apache/xalan-j-2jars' => array(
'xalan.jar',
),
'apache/poi' => array(
'poi-3.6-20091214.jar',
),
'apache/commons-beanutils' => array(
'commons-beanutils-1.8.0.jar',
),
'apache/commons-collections' => array(
'commons-collections-2.1.1.jar',
),
'apache/commons-digester' => array(
'commons-digester-1.7.jar',
),
'apache/commons-logging' => array(
'commons-logging.jar',
),
);
public static function postInstall(Event $event)
{
$composer = $event->getComposer();
$repositoryManager = $composer->getRepositoryManager();
$installationManager = $composer->getInstallationManager();
foreach (static::$packageWhitelists as $packageName => $packageWhitelist) {
$package = $repositoryManager->findPackage($packageName, '*');
if ($package === null) {
echo "Could not find package '$packageName' during cleanup.\n";
continue;
}
$packagePath = $installationManager->getInstallPath($package);
static::deleteFilesInDirExcept($packagePath, array_map(
function ($packageWhitelistPath) use ($packagePath) {
return "$packagePath/$packageWhitelistPath";
},
$packageWhitelist
));
}
}
public static function postUpdate(Event $event)
{
return static::postInstall($event);
}
/**
* Delete all files in a directory except some given exceptions.
*
* @param string $dir The directory to delete the contents of.
* @param array $exceptions A set of paths to not delete.
*/
private static function deleteFilesInDirExcept($dir, array $exceptions = array())
{
$fileIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$dir,
FilesystemIterator::SKIP_DOTS
),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($fileIterator as $file) {
$filePath = $file->getPathname();
if (in_array($filePath, $exceptions)) {
continue;
}
if ($file->isDir()) {
@rmdir($filePath);
} else {
unlink($filePath);
}
}
}
}