4in1_ws_web/deploy/gen_runtime_config.php
2025-05-02 22:41:18 +03:00

80 lines
2.3 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
require __DIR__.'/../src/init.php';
$htdocs_dir = null;
$commit_hash = null;
for ($i = 1; $i < $argc; $i++) {
switch ($argv[$i]) {
case '--htdocs-dir':
$htdocs_dir = $argv[++$i] ?? usage('missing value for --htdocs-dir');
break;
case '--commit-hash':
$commit_hash = $argv[++$i] ?? usage('missing value for --commit-hash');
break;
default:
usage("unknown option {$argv[$i]}");
}
}
if (is_null($htdocs_dir) || is_null($commit_hash))
usage();
$hashes = [
'commit_hash' => $commit_hash,
'assets' => []
];
foreach (['css', 'js'] as $type) {
$entries = glob_recursive($htdocs_dir.'/dist-'.$type.'/*.'.$type);
if (empty($entries)) {
fwrite(STDERR, "warning: no files found in $htdocs_dir/dist-$type\n");
continue;
}
foreach ($entries as $file) {
$hashes['assets'][$type.'/'.basename($file)] = [
'integrity' => []
];
foreach (\engine\skin\FeaturedSkin::RESOURCE_INTEGRITY_HASHES as $hash_type)
$hashes['assets'][$type.'/'.basename($file)]['integrity'][$hash_type] = base64_encode(hash_file($hash_type, $file, true));
}
}
echo "<?php\n\n";
echo "return ".var_export($hashes, true).";\n";
function usage(string $msg = ''): never {
if ($msg !== '')
fwrite(STDERR, "error: {$msg}\n");
$script = $GLOBALS['argv'][0];
fwrite(STDERR, "usage: {$script} --htdocs-dir DIR --commit-hash HASH\n");
exit(1);
}
function glob_escape(string $pattern): string {
if (str_contains($pattern, '[') || str_contains($pattern, ']')) {
$placeholder = uniqid();
$replaces = [$placeholder.'[', $placeholder.']', ];
$pattern = str_replace( ['[', ']'], $replaces, $pattern);
$pattern = str_replace( $replaces, ['[[]', '[]]'], $pattern);
}
return $pattern;
}
/**
* Does not support flag GLOB_BRACE
*
* @param string $pattern
* @param int $flags
* @return array
*/
function glob_recursive(string $pattern, int $flags = 0): array {
$files = glob(glob_escape($pattern), $flags);
foreach (glob(glob_escape(dirname($pattern)).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
}
return $files;
}