misuzu/build.php

77 lines
1.8 KiB
PHP
Raw Normal View History

<?php
/**
* Misuzu Asset Build Script.
*/
/**
* BEYOND THIS POINT YOU WON'T HAVE TO EDIT THE CONFIG PRETTY MUCH EVER
*/
define('ASSETS_DIR', __DIR__ . '/assets');
define('TS_DIR', ASSETS_DIR . '/typescript');
define('PUBLIC_DIR', __DIR__ . '/public');
define('JS_DIR', PUBLIC_DIR . '/js');
2018-09-17 21:07:10 +02:00
define('TWIG_DIRECTORY', sys_get_temp_dir() . '/msz-tpl-cache-' . md5(__DIR__));
/**
* FUNCTIONS
*/
2019-06-10 19:04:53 +02:00
function build_log(string $text = ''): void {
echo strlen($text) > 0 ? date('<H:i:s> ') . $text . PHP_EOL : PHP_EOL;
}
2019-06-10 19:04:53 +02:00
function create_dir(string $dir): void {
if(!file_exists($dir) || !is_dir($dir)) {
mkdir($dir);
build_log("Created '{$dir}'!");
}
}
2019-06-10 19:04:53 +02:00
function glob_dir(string $dir, string $pattern, int $flags = 0): array {
return glob($dir . '/' . $pattern, $flags);
}
2019-06-10 19:04:53 +02:00
function purge_dir(string $dir, string $pattern): void {
$files = glob_dir($dir, $pattern);
2019-06-10 19:04:53 +02:00
foreach($files as $file) {
if(is_dir($file)) {
build_log("'{$file}' is a directory, entering...");
purge_dir($file, $pattern);
2018-09-17 21:07:10 +02:00
rmdir($file);
} else {
unlink($file);
build_log("Deleted '{$file}'");
2018-09-17 21:07:10 +02:00
}
}
}
2018-11-17 21:37:18 +01:00
$doAll = empty($argv[1]) || $argv[1] === 'all';
$doJs = $doAll || $argv[1] === 'js';
2018-11-05 17:45:22 +01:00
// Make sure we're running from the misuzu root directory.
chdir(__DIR__);
build_log('Cleanup');
2019-06-10 19:04:53 +02:00
if($doJs) {
create_dir(JS_DIR);
purge_dir(JS_DIR, '*.js');
purge_dir(TS_DIR, '*.d.ts');
2018-11-17 21:37:18 +01:00
build_log();
build_log('Compiling TypeScript');
build_log(shell_exec('tsc --extendedDiagnostics -p tsconfig.json'));
2018-11-17 21:37:18 +01:00
}
2018-09-17 21:07:10 +02:00
// no need to do this in debug mode, auto reload is enabled and cache is disabled
2019-06-10 19:04:53 +02:00
if($doAll && !file_exists(__DIR__ . '/.debug')) {
2018-09-17 21:07:10 +02:00
// Clear Twig cache
build_log();
build_log('Deleting template cache');
purge_dir(TWIG_DIRECTORY, '*');
2018-09-17 21:07:10 +02:00
}