-
Notifications
You must be signed in to change notification settings - Fork 21
/
compile.php
137 lines (123 loc) · 3.71 KB
/
compile.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
<?php
/*
* NOWHERE Plugin Workspace Framework
*
* Copyright (C) 2015-2017 PEMapModder
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PEMapModder
*/
if(version_compare(PHP_VERSION, "7.0.0", "<")){
echo "Fatal: This entry script requires PHP >=7.0.0!\n";
exit;
}
chdir(__DIR__);
$i = 0;
function addDir(Phar $phar, $from, $localDir){
global $i;
$from = rtrim(realpath($from), "/") . "/";
$localDir = rtrim($localDir, "/") . "/";
if(!is_dir($from)){
echo "WARNING: $from is not a directory!";
return;
}
/** @type SplFileInfo $file */
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($from)) as $file){
if(!$file->isFile()){
continue;
}
$incl = substr($file, strlen($from));
$target = $localDir . $incl;
$phar->addFile($file, $target);
$i++;
echo "\r[$i] Added file $target";
}
echo "\n";
}
function walkPerms(array $stack, array &$perms){
$prefix = implode(".", $stack) . ".";
foreach(array_keys($perms) as $key){
$perms[$prefix . $key] = $perms[$key];
unset($perms[$key]);
$stack2 = $stack;
$stack2[] = $key;
if(isset($perms[$prefix . $key]["children"])){
walkPerms($stack2, $perms[$prefix . $key]["children"]);
}
}
}
function parsePerms(SimpleXMLElement $element, array $parents){
// var_dump($element);
$prefix = "";
foreach($parents as $parent){
$prefix .= $parent . ".";
}
$description = (string) $element->attributes()->description;
$default = (string) $element->attributes()->default;
$children = [];
foreach($element->children() as $childName => $child){
$copy = $parents;
$copy[] = $childName;
$children[$prefix . $childName] = parsePerms($child, $copy);
}
return [
"description" => $description,
"default" => $default,
"children" => $children,
];
}
$info = json_decode(file_get_contents("nowhere.json"));
$NAME = $info->name;
$CLASS = "Dev";
$BUILD_NUMBER = $info->nextBuild++;
$opts = getopt("", ["rc", "beta"]);
if(isset($opts["beta"])){
$CLASS = "Beta";
}elseif(isset($opts["rc"])){
$CLASS = "RC";
}
$VERSION = $info->version->major . "." . $info->version->minor . "-" . $CLASS . "#" . $BUILD_NUMBER;
file_put_contents("nowhere.json", json_encode($info, JSON_BIGINT_AS_STRING | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$permissions = [];
if(is_file("permissions.xml")){
$permissions = parsePerms((new SimpleXMLElement(file_get_contents("permissions.xml"))), [])["children"];
}
$file = "compile/" . $NAME . "_" . $CLASS . ".phar";
if(is_file($file)){
unlink($file);
}
$phar = new Phar($file);
$phar->setStub('<?php require_once "phar://" . __FILE__ . "/entry/entry.php"; __HALT_COMPILER();');
$phar->setSignatureAlgorithm(Phar::SHA1);
$phar->startBuffering();
$phar->addFromString("plugin.yml", yaml_emit([
"name" => $NAME,
"author" => $info->author,
"authors" => $info->authors ?? [],
"main" => $info->main,
"api" => $info->api,
"depend" => $info->depend ?? [],
"softdepend" => $info->softdepend ?? [],
"loadbefore" => $info->loadbefore ?? [],
"description" => $info->description ?? "",
"website" => $info->website ?? "",
"prefix" => $info->prefix ?? $NAME,
"load" => $info->load ?? "POSTWORLD",
"version" => $VERSION,
"commands" => $info->commands ?? [],
"permissions" => $permissions,
"generated" => date(DATE_ISO8601)
]));
addDir($phar, "src", "src");
addDir($phar, "entry", "entry");
addDir($phar, "resources", "resources");
$phar->stopBuffering();
echo "Created $NAME $CLASS #$BUILD_NUMBER", PHP_EOL;
exec("git add " . escapeshellarg($file));
if(is_file("priv/postCompile.php")){
include_once "priv/postCompile.php";
}