Repository navigation
Expand file tree
/
Copy pathbytecode-package-sign
More file actions
executable file
·77 lines (73 loc) · 2.87 KB
/
Copy pathbytecode-package-sign
File metadata and controls
executable file
·77 lines (73 loc) · 2.87 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env php
<?php
declare(strict_types=1);
// --- Rust-first dispatch -------------------------------------------------
// If a compiled Rust `bytecode-package-sign` is available, hand off to it (it
// is a byte-compatible reimplementation). Set BYTECODE_NO_RUST=1 to force PHP.
(static function (): void {
if (getenv('BYTECODE_NO_RUST') || PHP_SAPI !== 'cli' || !function_exists('proc_open')) {
return;
}
$root = getenv('BYTECODE_ROOT') ?: dirname(__DIR__, 2);
$candidates = [];
if ($explicit = getenv('BYTECODE_PACKAGE_SIGN_RS')) {
$candidates[] = $explicit;
}
$candidates[] = $root . '/rust/target/release/bytecode-package-sign';
$candidates[] = __DIR__ . '/bytecode-package-sign-rs';
$bin = null;
foreach ($candidates as $cand) {
if ($cand !== '' && is_file($cand) && is_executable($cand)) {
$bin = $cand;
break;
}
}
if ($bin === null) {
return;
}
// Repo root so the VERSION file (tool_version) resolves the same way.
if (getenv('BYTECODE_ROOT') === false) {
putenv('BYTECODE_ROOT=' . dirname(__DIR__, 2));
}
global $argv;
$proc = @proc_open(
array_merge([$bin], array_slice($argv, 1)),
[0 => STDIN, 1 => STDOUT, 2 => STDERR],
$pipes
);
if (!is_resource($proc)) {
return; // could not launch -> fall back to PHP
}
exit(proc_close($proc));
})();
// --- end Rust-first dispatch ---------------------------------------------
require_once __DIR__ . '/bytecode-common.php';
if ($argc < 2 || in_array($argv[1] ?? '', ['-h', '--help'], true)) {
fwrite(STDERR, "Usage: bytecode-package-sign <encoded-output-dir>\n");
exit(($argc === 2 && in_array($argv[1], ['-h', '--help'], true)) ? 0 : 2);
}
$dir = rtrim($argv[1], DIRECTORY_SEPARATOR);
if (!is_dir($dir)) {
fwrite(STDERR, "not a directory: {$dir}\n");
exit(2);
}
$files = [];
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS));
foreach ($it as $file) {
if (!$file->isFile() || basename($file->getPathname()) === 'bytecode.package.json') {
continue;
}
$rel = str_replace('\\', '/', substr($file->getPathname(), strlen($dir) + 1));
$files[] = ['path' => $rel, 'sha256' => hash_file('sha256', $file->getPathname()), 'size' => $file->getSize()];
}
usort($files, fn (array $a, array $b): int => $a['path'] <=> $b['path']);
$package = [
'format' => 'bytecode-package-v1',
'tool_version' => bytecode_project_version(),
'created_at' => gmdate('c'),
'file_count' => count($files),
'files' => $files,
];
$package['sha256'] = hash('sha256', json_encode($files, JSON_UNESCAPED_SLASHES));
file_put_contents($dir . DIRECTORY_SEPARATOR . 'bytecode.package.json', json_encode($package, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n");
echo "wrote {$dir}/bytecode.package.json\n";