-
Notifications
You must be signed in to change notification settings - Fork 3
/
AssetInstaller.php
executable file
·140 lines (111 loc) · 4.38 KB
/
AssetInstaller.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace asseteer;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use Composer\Script\Event;
use Composer\Repository\PackageRepository;
use Composer\Package\Package;
use Composer\Util\Filesystem;
use Composer\IO\IOInterface;
class AssetInstaller
{
const extraKey = 'post-install-asseteer';
const packageExtraKey = 'asseteer';
const vendorKey = 'vendor';
const includeFitersKey = 'filters';
const targetKey = 'target';
private static $vendorOfAssetsToCopy = '';
private static $vendorDir = '';
public static function postUpdate(Event $event)
{
$repositoryManager = $event->getComposer()->getRepositoryManager();
$installationManager = $event->getComposer()->getInstallationManager();
$fs = new Filesystem();
$io = $event->getIO();
$repositories = $repositoryManager->getRepositories();
foreach ($repositories as $repository) {
if ($repository instanceof PackageRepository) {
$package = $repository->getPackages()[0];
$extra = $package->getExtra();
if (array_key_exists(self::packageExtraKey, $extra) && is_array($extra[self::packageExtraKey])) {
foreach ($extra[self::packageExtraKey] as $url) {
$newPackage = new Package($package->getName(), $package->getVersion(), $package->getPrettyVersion());
$newPackage->setType($package->getType());
$newPackage->setDistUrl($url);
$newPackage->setDistReference(sha1($url));
$installationPath = $installationManager->getInstallPath($package);
$filename = basename($url);
$targetFile = $installationPath.'/'.$filename;
if (! file_exists($targetFile)) {
$io->write(' [Asseteer] Fetching URL: ' . $url);
$tmpDir = sys_get_temp_dir() . '/asseteer-' . md5($url);
$fs->ensureDirectoryExists($tmpDir);
$event->getComposer()->getDownloadManager()->getDownloader('file')->download($newPackage, $tmpDir);
$fs->rename($tmpDir . '/' . $filename, $targetFile);
$fs->remove($tmpDir);
}
}
}
}
}
}
public static function postInstall(Event $event)
{
$extra = $event->getComposer()->getPackage()->getExtra();
if ( (! is_array($extra)) || (! array_key_exists(self::extraKey, $extra)) ) {
return;
}
$postInstallConfig = $extra[self::extraKey];
if ( ! is_array($postInstallConfig)) {
return;
}
self::$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
$io = $event->getIO();
foreach ($postInstallConfig as $entry) {
self::performPostInstallEntry($entry, $io);
}
}
private static function performPostInstallEntry(array $entry, IOInterface $io) {
$includeFilters = [];
if (array_key_exists(self::includeFitersKey, $entry)) {
if (is_array($entry[self::includeFitersKey])) {
$includeFilters = $entry[self::includeFitersKey];
}
else {
$includeFilters = [ $entry[self::includeFitersKey] ];
}
}
$vendorAssetsRootFolder = $entry[self::vendorKey];
self::$vendorOfAssetsToCopy = self::$vendorDir .'/' . $vendorAssetsRootFolder;
$targetPath = $entry[self::targetKey];
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(self::$vendorOfAssetsToCopy),
RecursiveIteratorIterator::SELF_FIRST);
$matches = null;
foreach($objects as $name => $object){
if (is_dir($name)) {
continue;
}
foreach ($includeFilters as $filter) {
if (preg_match('#' . $filter . '#', $name)) {
self::copy($name, $targetPath, $io);
break;
}
}
}
}
private static function copy($filename, $targetPath, IOInterface $io)
{
$relativeFilename = preg_replace('#^'.self::$vendorOfAssetsToCopy.'/#', '', $filename);
$directory = $targetPath . '/' . dirname($relativeFilename);
if (! is_dir($directory)) {
mkdir($directory, 0766, true);
}
$targetfile = $directory . '/' . basename($filename);
if ( (! file_exists($targetfile)) || (md5_file($targetfile) != md5_file($filename)) ) {
$io->write(" [copy] $targetfile");
chmod($filename, 0766);
copy($filename, $targetfile);
chmod($targetfile, 0766);
}
}
}