-
Notifications
You must be signed in to change notification settings - Fork 103
/
pack-plugin.php
92 lines (74 loc) · 2.45 KB
/
pack-plugin.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
<?php
if (! class_exists(ZipArchive::class)) {
echo 'This script requires zip PHP extension.' . PHP_EOL;
exit(1);
}
$plugin = rtrim($argv[1] ?? '', '/');
if (! $plugin || ! preg_match('~^(?:[a-z-_]++)$~', $plugin)) {
echo 'Plugin name was not specified or is invalid.' . PHP_EOL;
exit(1);
}
$directory = __DIR__ . '/plugins/' . $plugin . '/src';
if (! is_dir($directory)) {
$directory = __DIR__ . '/examples/' . $plugin . '/src';
if (is_dir($directory)) {
echo 'Specified plugin was not found in the "plugins" directory, using "examples" directory instead.' . PHP_EOL;
} else {
echo 'Specified plugin was not found in "plugins" nor "examples" directories.' . PHP_EOL;
exit(1);
}
}
chdir($directory);
if (file_exists($directory . '/composer.json')) {
exec('composer validate --no-check-publish --no-interaction', $result, $exitCode);
echo implode(PHP_EOL, $result);
if ($exitCode !== 0) {
exit($exitCode);
}
exec('composer install --classmap-authoritative --no-dev --no-interaction --no-suggest', $result, $exitCode);
echo implode(PHP_EOL, $result);
if ($exitCode !== 0) {
exit($exitCode);
}
}
$zipFile = $directory . '/../' . $plugin . '.zip';
if (file_exists($zipFile)) {
unlink($zipFile);
}
$zip = new ZipArchive();
if ($zip->open($zipFile, ZipArchive::CREATE) !== true) {
echo 'Can\'t open zip file.' . PHP_EOL;
exit(1);
}
// add README, if present
$readmeFilename = $directory . '/../README.md';
$readme = new SplFileInfo($readmeFilename);
if ($readme->isReadable()) {
@$zip->addFile($readme->getPathname(), $readme->getBasename());
}
$files = new CallbackFilterIterator(
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($directory)
),
static function (SplFileInfo $fileInfo) {
return ! $fileInfo->isDir();
}
);
$reservedFiles = [
'/ucrm.json',
'/.ucrm-plugin-running',
'/.ucrm-plugin-execution-requested',
];
/** @var SplFileInfo $fileInfo */
foreach ($files as $fileInfo) {
$filename = substr(str_replace('\\', '/', $fileInfo->getPathname()), strlen($directory));
if (in_array($filename, $reservedFiles, true)) {
echo sprintf('Skipping reserved file "%s".', $filename) . PHP_EOL;
continue;
}
if (! $zip->addFile($fileInfo->getPathname(), ltrim($filename, '/'))) {
echo sprintf('Unable to add file "%s".', $filename) . PHP_EOL;
exit(1);
}
}
$zip->close();