misuzu/build.php

227 lines
5.2 KiB
PHP
Raw Normal View History

<?php
/**
* Misuzu Asset Build Script.
*/
/**
* NPM module files to be imported into /js/libraries.js
*/
define('NODE_IMPORT_JS', [
2018-05-26 22:26:27 +00:00
'highlightjs/highlight.pack.min.js',
'timeago.js/dist/timeago.min.js',
'timeago.js/dist/timeago.locales.min.js',
]);
/**
* NPM module files to be imported into /css/libraries.css
*/
define('NODE_IMPORT_CSS', [
2018-05-26 22:26:27 +00:00
'highlightjs/styles/default.css',
'highlightjs/styles/tomorrow-night.css',
2018-09-28 16:40:41 +00:00
'@fortawesome/fontawesome-free/css/all.min.css',
]);
/**
* Directories to copy to the public folder
*/
define('NODE_COPY_DIRECTORY', [
'@fortawesome/fontawesome-free/webfonts' => 'webfonts',
]);
/**
* BEYOND THIS POINT YOU WON'T HAVE TO EDIT THE CONFIG PRETTY MUCH EVER
*/
define('LESS_CMD', 'lessc --verbose %s %s');
define('ASSETS_DIR', __DIR__ . '/assets');
define('LESS_DIR', ASSETS_DIR . '/less');
define('TS_DIR', ASSETS_DIR . '/typescript');
define('LESS_ENTRY_POINT', '/main.less');
define('PUBLIC_DIR', __DIR__ . '/public');
define('CSS_DIR', PUBLIC_DIR . '/css');
define('JS_DIR', PUBLIC_DIR . '/js');
define('LESS_DEST', CSS_DIR . '/style.css');
define('TS_DEST', JS_DIR . '/misuzu.js');
define('TS_SRC', JS_DIR . '/misuzu');
define('NODE_MODULES_DIR', __DIR__ . '/node_modules');
define('NODE_DEST_CSS', CSS_DIR . '/libraries.css');
define('NODE_DEST_JS', JS_DIR . '/libraries.js');
2018-09-17 19:07:10 +00:00
define('TWIG_DIRECTORY', sys_get_temp_dir() . '/msz-tpl-cache-' . md5(__DIR__));
/**
* FUNCTIONS
*/
function build_log(string $text = ''): void
{
echo strlen($text) > 0 ? date('<H:i:s> ') . $text . PHP_EOL : PHP_EOL;
}
function create_dir(string $dir): void
{
if (!file_exists($dir) || !is_dir($dir)) {
mkdir($dir);
build_log("Created '{$dir}'!");
}
}
function glob_dir(string $dir, string $pattern, int $flags = 0): array
{
return glob($dir . '/' . $pattern, $flags);
}
function purge_dir(string $dir, string $pattern): void
{
$files = glob_dir($dir, $pattern);
foreach ($files as $file) {
2018-09-17 19:07:10 +00:00
if (is_dir($file)) {
build_log("'{$file}' is a directory, entering...");
purge_dir($file, $pattern);
2018-09-17 19:07:10 +00:00
rmdir($file);
} else {
unlink($file);
build_log("Deleted '{$file}'");
2018-09-17 19:07:10 +00:00
}
}
}
function recursive_copy(string $source, string $dest): bool
2018-09-28 16:40:41 +00:00
{
if (is_link($source)) {
return symlink(readlink($source), $dest);
}
if (is_file($source)) {
return copy($source, $dest);
}
if (!is_dir($dest)) {
mkdir($dest);
}
$dir = dir($source);
while (($entry = $dir->read()) !== false) {
if ($entry === '.' || $entry === '..') {
continue;
}
recursive_copy($source . DIRECTORY_SEPARATOR . $entry, $dest . DIRECTORY_SEPARATOR . $entry);
2018-09-28 16:40:41 +00:00
}
$dir->close();
return true;
}
2018-11-17 20:37:18 +00:00
$doAll = empty($argv[1]) || $argv[1] === 'all';
$doCss = $doAll || $argv[1] === 'css';
$doJs = $doAll || $argv[1] === 'js';
2018-11-05 16:45:22 +00:00
// Make sure we're running from the misuzu root directory.
chdir(__DIR__);
build_log('Cleanup');
2018-11-17 20:37:18 +00:00
if ($doCss) {
create_dir(CSS_DIR);
purge_dir(CSS_DIR, '*.css');
2018-11-17 20:37:18 +00:00
}
2018-11-17 20:37:18 +00:00
if ($doJs) {
create_dir(JS_DIR);
purge_dir(JS_DIR, '*.js');
purge_dir(TS_DIR, '*.d.ts');
2018-11-17 20:37:18 +00:00
}
if ($doCss) {
build_log();
build_log('Compiling LESS');
2018-11-17 20:37:18 +00:00
if (!is_file(LESS_DIR . LESS_ENTRY_POINT)) {
build_log('==> ERR: Entry point for this style does not exist (' . basename(LESS_ENTRY_POINT) . ')');
2018-11-17 20:37:18 +00:00
} else {
system(sprintf(LESS_CMD, escapeshellarg(LESS_DIR . LESS_ENTRY_POINT), LESS_DEST));
}
}
build_log();
build_log('Importing libraries');
define('IMPORT_SEQ', [
[
'name' => 'CSS',
'files' => NODE_IMPORT_CSS,
'destination' => NODE_DEST_CSS,
'insert-semicolon' => false,
2018-11-17 20:37:18 +00:00
'do' => $doCss,
],
[
'name' => 'JavaScript',
'files' => NODE_IMPORT_JS,
'destination' => NODE_DEST_JS,
'insert-semicolon' => true,
2018-11-17 20:37:18 +00:00
'do' => $doJs,
],
]);
foreach (IMPORT_SEQ as $sequence) {
2018-11-17 20:37:18 +00:00
if (!$sequence['do']) {
continue;
}
build_log("=> {$sequence['name']}");
$contents = '';
foreach ($sequence['files'] as $file) {
$realpath = realpath(NODE_MODULES_DIR . '/' . $file);
build_log("==> '{$file}'");
if (!file_exists($realpath)) {
build_log('===> File does not exist.');
continue;
}
$contents .= file_get_contents($realpath);
if ($sequence['insert-semicolon']) {
$contents .= ';';
}
}
file_put_contents($sequence['destination'], $contents);
}
2018-09-17 19:07:10 +00:00
2018-11-17 20:37:18 +00:00
if ($doJs) {
build_log();
build_log('Compiling TypeScript');
build_log(shell_exec('tsc --extendedDiagnostics -p tsconfig.json'));
2018-11-17 20:37:18 +00:00
}
build_log();
build_log('Copying data...');
2018-09-28 16:40:41 +00:00
foreach (NODE_COPY_DIRECTORY as $source => $dest) {
build_log("=> " . basename($dest));
2018-09-28 16:40:41 +00:00
$source = realpath(NODE_MODULES_DIR . DIRECTORY_SEPARATOR . $source);
$dest = PUBLIC_DIR . DIRECTORY_SEPARATOR . $dest;
purge_dir($dest, '*');
recursive_copy($source, $dest);
2018-09-28 16:40:41 +00:00
}
2018-09-17 19:07:10 +00:00
// no need to do this in debug mode, auto reload is enabled and cache is disabled
2018-11-17 20:37:18 +00:00
if ($doAll && !file_exists(__DIR__ . '/.debug')) {
2018-09-17 19:07:10 +00:00
// Clear Twig cache
build_log();
build_log('Deleting template cache');
purge_dir(TWIG_DIRECTORY, '*');
2018-09-17 19:07:10 +00:00
}