forked from CzechPMDevs/MultiWorld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.php
73 lines (57 loc) · 2.3 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
<?php
declare(strict_types=1);
/**
* Build script
*/
// For example C:/pmmp-server/plugins/MultiWorld.phar
const CUSTOM_OUTPUT_PATH = "";
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 . "MultiWorld" . DIRECTORY_SEPARATOR;
@mkdir($to, 0777, true);
// Clean output directory...
cleanDirectory($to);
// Copying new files...
copyDirectory($from . "src", $to . "src");
copyDirectory($from . "resources", $to . "resources");
copyDirectory($from . "vendor/czechpmdevs/libpmform/src", $to . "src/czechpmdevs/libpmform");
$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 . "MultiWorld_{$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, RecursiveDirectoryIterator::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());
file_put_contents($target, $contents);
}
}
}
function cleanDirectory(string $directory): void {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
/** @var SplFileInfo $fileInfo */
foreach ($files as $fileInfo) {
if($fileInfo->isDir()) {
rmdir($fileInfo->getPathname());
} else {
unlink($fileInfo->getPathname());
}
}
}