-
Notifications
You must be signed in to change notification settings - Fork 20
/
compile.php
138 lines (123 loc) · 3.22 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
138
<?php
$statusPadding = 0;
preg_match_all("/Columns:[ \t]+([0-9]+)/", `mode`, $matches);
$MAX_LENGTH = ((int) $matches[1][0]) - 1;
$MAX_LENGTH = 180;
$input = trim(readLine("Plugin dir name: "), "/\\");
if(!is_dir($input)){
fail("Directory doesn't exist!");
}
$dir = rtrim(realpath($input), "/\\") . "/";
if(!is_file($pluginYml = $dir . "plugin.yml")){
fail("Plugin manifest not found!");
}
if(!is_dir($srcDir = $dir . "src/")){
fail("Source folder not found!");
}
if(!is_dir($dir . "bin")){
console("Creating bin/ directory and its README file...");
mkdir($dir . "bin");
file_put_contents($readme = $dir . "bin/README.md", <<<EOF
$input/bin
===
This directory stores a .phar build of the latest built version, but not necessarily the latest commit.
EOF
);
}
if($hasRes = is_dir($resDir = $dir . "resources/")){
console("Resources directory detected.");
}
$manifest = yaml_parse_file($pluginYml);
$version = $manifest["version"];
$last3 = substr($version, -3);
if(!is_numeric($last3)){
$last3 = 0;
$version .= "_#";
}else{
$version = substr($version, 0, -3);
}
$last3 = ((int) $last3) + 1;
$version .= str_pad((string) $last3, 3, "0", STR_PAD_LEFT);
$manifest["version"] = $version;
yaml_emit_file($pluginYml, $manifest);
console("New $input version: $version");
$path = $dir . "bin/" . $input . ".phar";
if(is_file($path)){
console("Older phar detected, deleting...");
unlink($path);
}
console("Creating phar at $path...");
$phar = new Phar($path);
$phar->setStub('<?php __HALT_COMPILER();');
$phar->setSignatureAlgorithm(Phar::SHA1);
$phar->startBuffering();
console("Adding files into phar...");
console("Adding plugin.yml...");
$phar->addFile($pluginYml, "plugin.yml");
clearLine();
console("Adding sources...");
$cnt = 0;
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($srcDir)) as $absPath){
if(!is_file($absPath)){
continue;
}
$cnt++;
$absPath = realpath($absPath);
status("[$cnt] $absPath");
$relPath = ltrim(substr($absPath, strlen($dir)), "/\\");
$phar->addFile($absPath, $relPath);
}
if($hasRes){
clearLine();
console("Adding resources...");
$cnt = 0;
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($resDir)) as $absPath){
if(!is_file($absPath)){
continue;
}
$cnt++;
$absPath = realpath($absPath);
status("[$cnt] $absPath");
$relPath = ltrim(substr($absPath, strlen($dir)), "/\\");
$phar->addFile($absPath, $relPath);
}
}
console("Compressing files...");
$phar->stopBuffering();
console("Done! Phar created at \x1b[33;1m$path\x1b[0m.");
console("Staging generated/modified files into Git..");
echo `git add -A :/`;
fail("Everything finished! Press enter to close this window \x1b[32;1m\x1b[4m:)\x1b[0m", 1);
function readLine($prompt = ""){
echo $prompt;
while(!($line = trim(fgets(fopen("php://stdin", "rt"))))){
;
}
return $line;
}
function fail($reason, $code = 2){
echo $reason . PHP_EOL;
exit($code);
}
function console($msg){
// clearLine();
echo $msg;
echo PHP_EOL;
}
function status($msg){
if(true){
console(trim($msg));
return;
}
global $statusPadding;
clearLine();
echo $msg;
echo "\r";
$statusPadding = strlen($msg);
}
function clearLine(){
global $statusPadding, $MAX_LENGTH;
echo str_repeat(" ", $MAX_LENGTH);
echo "\r";
$statusPadding = 0;
}