-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.php
More file actions
59 lines (55 loc) · 3.57 KB
/
Copy pathbuild.php
File metadata and controls
59 lines (55 loc) · 3.57 KB
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
<?php
/**
* Build dist/tiger-install.php: the wizard (src/installer.php) with the tiger-headless engine of the
* tag in ENGINE_VERSION inlined at the @@TIGER_HEADLESS@@ marker. One file is the product — a person
* uploads it to a document root with no shell — so the engine travels INSIDE it, byte-for-byte from
* its tagged release, never a working copy.
*
* php build.php # fetch the tag from GitHub
* php build.php --from=../TigerHeadless # a local checkout (development only; the build says so)
*/
$root = __DIR__;
$tag = trim((string) file_get_contents($root . '/ENGINE_VERSION'));
$from = null;
foreach (array_slice($_SERVER['argv'], 1) as $a) { if (preg_match('/^--from=(.+)$/', $a, $m)) { $from = rtrim($m[1], '/'); } }
$srcDir = null; $tmp = null;
if ($from !== null) {
$srcDir = $from . '/src/Tiger/Headless';
is_dir($srcDir) || exit("no engine at {$srcDir}\n");
} else {
$tmp = sys_get_temp_dir() . '/tiger-install-build-' . bin2hex(random_bytes(4));
mkdir($tmp, 0700, true);
$url = "https://github.com/WebTigers/TigerHeadless/archive/refs/tags/{$tag}.zip";
$ctx = stream_context_create(['http' => ['user_agent' => 'tiger-install-build', 'follow_location' => 1, 'timeout' => 60]]);
$zip = @file_get_contents($url, false, $ctx);
($zip !== false && strlen($zip) > 1000) || exit("could not download {$url}\n");
file_put_contents($tmp . '/engine.zip', $zip);
$za = new ZipArchive();
$za->open($tmp . '/engine.zip') === true || exit("bad zip from {$url}\n");
$za->extractTo($tmp); $za->close();
$srcDir = $tmp . '/TigerHeadless-' . ltrim($tag, 'v') . '/src/Tiger/Headless';
is_dir($srcDir) || exit("unexpected archive layout under {$tmp}\n");
}
$files = glob($srcDir . '/*.php'); sort($files);
$files || exit("no engine classes in {$srcDir}\n");
$engine = "/* ==== vendored tiger-headless {$tag} (https://github.com/WebTigers/TigerHeadless) — generated by build.php, do not edit ==== */\n";
$engineVersion = '';
foreach ($files as $f) {
$code = (string) file_get_contents($f);
$code = preg_replace('/^<\?php\s*/', '', $code, 1);
$code = preg_replace('/^\s*declare\s*\([^)]*\)\s*;\s*/m', '', $code);
if (preg_match("/VERSION\s*=\s*'([^']+)'/", $code, $m) && basename($f) === 'Version.php') { $engineVersion = $m[1]; }
$engine .= "\n/* ---- " . basename($f) . " ---- */\n" . trim($code) . "\n";
}
$engine .= "/* ==== end tiger-headless {$tag} ==== */\n";
if ($from === null && 'v' . $engineVersion !== $tag) { exit("engine says {$engineVersion} but the tag is {$tag}\n"); }
$wizard = (string) file_get_contents($root . '/src/installer.php');
strpos($wizard, '/* @@TIGER_HEADLESS@@ */') !== false || exit("src/installer.php has no @@TIGER_HEADLESS@@ marker\n");
$out = str_replace('/* @@TIGER_HEADLESS@@ */', $engine, $wizard);
$out = str_replace('@@ENGINE_VERSION@@', $engineVersion ?: ltrim($tag, 'v'), $out);
@mkdir($root . '/dist', 0755, true);
file_put_contents($root . '/dist/tiger-install.php', $out);
if ($tmp) { foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tmp, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $p) { $p->isDir() ? rmdir($p) : unlink($p); } rmdir($tmp); }
exec(escapeshellarg(PHP_BINARY) . ' -l ' . escapeshellarg($root . '/dist/tiger-install.php'), $o, $rc);
$rc === 0 || exit("dist/tiger-install.php does not lint:\n" . implode("\n", $o) . "\n");
echo "built dist/tiger-install.php (engine {$tag}" . ($from !== null ? ", from {$from} — NOT a release build" : '') . ', ' . count($files) . " classes, " . number_format(strlen($out)) . " bytes)\n";