-
Notifications
You must be signed in to change notification settings - Fork 12
/
assemble.php
89 lines (77 loc) · 2.61 KB
/
assemble.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
<?php
/**
* Script for automatic assembling of plugins into zip.
*/
$root = dirname(__FILE__);
$excludeDirs = ['.git', '.github', '.idea'];
$excludeFromZip = ['docs'];
/**
* This filter excludes from the selection zip-archives and the
* folders with the name added to array '$excludeFromZip' and
* contents inside them.
*
* @param SplFileInfo $file
* @param string $key
* @param RecursiveDirectoryIterator $iterator
*
* @return bool
*/
$filter = function (
SplFileInfo $file,
string $key,
RecursiveDirectoryIterator $iterator
) use ($excludeFromZip): bool {
if (
$iterator->hasChildren()
&& in_array($file->getFilename(), $excludeFromZip)
|| $file->getExtension() === 'zip'
) {
return false;
}
return true;
};
foreach (glob($root . '/*', GLOB_ONLYDIR) as $dirPath) {
$dirName = basename($dirPath);
if (!in_array($dirName, $excludeDirs)) {
$zip = new ZipArchive();
/** @noinspection SpellCheckingInspection */
$zipName = sprintf(
'%s/%s.ocmod.zip',
$dirPath,
$dirName
);
$zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true
|| die("Could not create/overwrite archive '$zipName'");
$zip->setArchiveComment("$dirName plugin")
|| die("Could not set comment to archive '$zipName'");
$innerIterator = new RecursiveDirectoryIterator(
$dirPath,
RecursiveDirectoryIterator::SKIP_DOTS
);
$filesToZip = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
$innerIterator,
$filter
),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($filesToZip as $path => $file) {
$relativePath = substr($path, strlen($dirPath) + 1);
$relativePathUnixStyle = str_replace('\\', '/', $relativePath);
switch (is_dir($file)) {
case false:
$zip->addFile($path, $relativePathUnixStyle)
|| die("Could not add file '$path' to archive '$zipName'");
break;
case true:
$zip->addEmptyDir($relativePathUnixStyle)
|| die("Could not create empty folder '$relativePathUnixStyle' inside archive '$zipName'");
break;
}
}
$zip->addFile($root . '/LICENSE', 'LICENSE')
|| die("Could not add plugin license to archive '$zipName'");
$zip->close()
|| die("Failure occurred while trying to write archive '$zipName'");
}
}