-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.php
104 lines (86 loc) · 3.19 KB
/
build.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
<?php
declare(strict_types=1);
//exec("vendor\bin\php-cs-fixer.bat");
/**
* Build script
*/
// For example C:/pmmp-server/plugins/BuilderTools.phar
const CUSTOM_OUTPUT_PATH = "C:\Users\stehl\Desktop\pmmp\plugins\ImageOnMap.phar";
const COMPRESS_FILES = true;
const COMPRESSION = Phar::GZ;
$startTime = microtime(true);
// Input & Output directory...
$from = getcwd() . DIRECTORY_SEPARATOR;
$to = getcwd() . DIRECTORY_SEPARATOR . "out" . DIRECTORY_SEPARATOR . "ImageOnMap" . DIRECTORY_SEPARATOR;
@mkdir($to, 0777, true);
// Clean output directory...
cleanDirectory($to);
// Copying new files...
copyDirectory($from . "src", $to . "src");
//copyDirectory($from . "resources", $to . "resources");
$description = yaml_parse_file($from . "plugin.yml");
yaml_emit_file($to . "plugin.yml", $description);
// Defining output path...
$outputPath = CUSTOM_OUTPUT_PATH == "" ? getcwd() . DIRECTORY_SEPARATOR . "out" . DIRECTORY_SEPARATOR . "ImageOnMap_{$description["version"]}_dev.phar" : CUSTOM_OUTPUT_PATH;
@unlink($outputPath);
// Generate phar
$phar = new Phar($outputPath);
$phar->buildFromDirectory($to);
if(COMPRESS_FILES) {
$phar->compressFiles(COMPRESSION);
}
printf("Plugin built in %s seconds! Output path: %s\n", round(microtime(true) - $startTime, 3), $outputPath);
function copyDirectory(string $from, string $to): void {
mkdir($to, 0777, true);
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($from, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
/** @var SplFileInfo $fileInfo */
foreach ($files as $fileInfo) {
$target = str_replace($from, $to, $fileInfo->getPathname());
if($fileInfo->isDir()) {
mkdir($target, 0777, true);
} else {
$contents = file_get_contents($fileInfo->getPathname());
preProcess($contents);
file_put_contents($target, $contents);
}
}
}
function cleanDirectory(string $directory): void {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
/** @var SplFileInfo $fileInfo */
foreach ($files as $fileInfo) {
if($fileInfo->isDir()) {
rmdir($fileInfo->getPathname());
} else {
unlink($fileInfo->getPathname());
}
}
}
function preProcess(string &$file): void {
// if(!defined("replacements")) {
// $patterns = [
// 'Math::lengthSquared2d({%1}, {%2})' => '({%1} ** 2) + ({%2} ** 2)',
// 'Math::lengthSquared3d({%1}, {%2}, {%3})' => '({%1} ** 2) + ({%2} ** 2) + ({%3} ** 2)',
// ];
//
// $replacements = [];
//
// foreach ($patterns as $key => $value) {
// $key = "#" . str_replace(["(", ")"], ["\(", "\)"], $key) . "#";
//
// /** @noinspection PhpStatementHasEmptyBodyInspection */
// for($i = 0; strpos($key, "{%" . (++$i) ."}") !== false;);
//
// for($j = 1; $j < $i; ++$j) {
// $key = str_replace("{%$j}", "(.*?)", $key);
// $value = str_replace("{%$j}", "\$$j", $value);
// }
//
// $replacements[$key] = $value;
// }
//
// define("replacements", $replacements);
// }
//
// $file = preg_replace(array_keys(replacements), array_values(replacements), $file);
}