-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake.php
159 lines (136 loc) · 4.17 KB
/
make.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
require __DIR__."/config.php";
require __DIR__."/src/build.php";
$extDir = __DIR__."/src/ext";
$buildDir = __DIR__."/build";
$releaseMode = false;
$configDir = __DIR__."/config";
$libDir = STORAGE_PATH."/lib";
$cwd = getcwd();
$forceBuild = array_search("-f", $argv) !== false;
if (isset($argv[1])) {
switch ($argv[1]) {
case "release":
$releaseMode = true;
break;
case "clean":
cleanBuiltData();
break;
}
}
build();
/**
* @return void
*/
function build(bool $noExit = false): void
{
global $extDir, $buildDir, $releaseMode, $forceBuild, $libDir, $cwd, $configDir;
// Build webhook endpoint.
sh(escapeshellarg(PHP_BINARY)." ".escapeshellarg(__DIR__."/bin/build_webhook.php"));
// Make lib dir.
is_dir($libDir) or mkdir($libDir);
/**
* Build extension.
*/
$buildExtDir = $buildDir."/ext";
if (!is_dir($buildDir)) {
mmlog("Create build directory: %s", $buildDir);
mkdir($buildDir, 0755);
}
if (!is_dir($buildExtDir)) {
mmlog("Create ext build directory: %s", $buildExtDir);
mkdir($buildExtDir, 0755);
}
/**
* Build languages.
*/
recursiveCallbackScanDir($extDir, function (string $dir, string $file) {
if (preg_match("/^.+\.lang\.php$/S", $file)) {
langFragBuilder($dir."/".$file);
}
});
// Scan ext directory.
recursiveCallbackScanDir($extDir, function (string $dir, string $file) use ($extDir, $buildExtDir) {
$baseD = explode($extDir, $dir, 2);
if (isset($baseD[1])) {
$targetDir = $buildExtDir."/".ltrim($baseD[1], "/");
if (!is_dir($targetDir)) {
mmlog("Create ext build directory: %s", $targetDir);
mkdir($targetDir, 0755);
}
$sourceFile = $dir."/".$file;
$targetFile = rtrim($targetDir, "/")."/".$file;
if ((!file_exists($targetFile)) || (filemtime($sourceFile) > filemtime($targetFile))) {
sh("cp -vf ".escapeshellarg($sourceFile)." ".escapeshellarg($targetFile));
}
}
});
chdir($buildExtDir);
if (!file_exists("configure.lock")) {
sh("cd {$buildExtDir}; phpize");
sh("cd {$buildExtDir}; ./configure");
file_put_contents($buildExtDir."/configure.lock", time());
}
if ($forceBuild) {
sh("cd {$buildExtDir}; make clean");
}
sh("cd {$buildExtDir}; make 2>&1");
sh("ln -svf ".escapeshellarg($buildExtDir."/modules/teabot.so")." ".escapeshellarg($libDir)." 2>&1");
chdir($cwd);
/**
* Build config.
*/
recursiveCallbackScanDir($configDir, function (string $dir, string $file) {
if (preg_match("/^.+\.frag\.php$/S", $file)) {
sh(escapeshellarg(PHP_BINARY)." ".escapeshellarg($dir."/".$file));
}
});
if (!$noExit) {
exit(0);
}
}
/**
* @return void
*/
function cleanBuiltData(): void
{
global $extDir, $buildDir, $releaseMode, $forceBuild, $libDir, $cwd, $configDir;
sh("rm -rfv build");
recursiveCallbackScanDir($configDir, function (string $dir, string $file) {
if (($file !== ".gitignore") && (!preg_match("/^.+\.frag\.php$/S", $file))) {
print "Deleting {$file}...";
if (unlink($dir."/".$file)) {
print "OK!";
} else {
print "Error!";
}
print "\n";
}
});
exit(0);
}
/**
* @param string $sourceFile
* @return void
*/
function langFragBuilder(string $sourceFile): void
{
require $sourceFile;
if (file_exists($targetFile) && (filemtime($sourceFile) < filemtime($targetFile))) {
return;
}
print "Executing {$sourceFile}...\n";
$handle = fopen($targetFile, "w");
fwrite($handle, "
/**
* This file was generated by langFragBuilder() from source file: ".basename($sourceFile)."
*/
");
fwrite($handle, "const lang_entry {$lang}_lang_entry[] = {\n");
foreach ($langData as $k => $data) {
$data = str_replace("\n", "\\n", $data);
fwrite($handle, " ADD_LE(\"{$k}\", \"{$data}\"),\n");
}
fwrite($handle, "};\n");
fclose($handle);
}