2018-05-26 22:14:21 +00:00
|
|
|
<?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',
|
2018-05-26 22:14:21 +00:00
|
|
|
'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',
|
2018-05-26 22:14:21 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
|
2018-09-23 01:32:18 +00:00
|
|
|
define('LESS_DEST', CSS_DIR . '/style.css');
|
2018-11-06 22:55:05 +00:00
|
|
|
define('TS_DEST', JS_DIR . '/misuzu.js');
|
2019-01-30 09:19:35 +00:00
|
|
|
define('TS_SRC', JS_DIR . '/misuzu');
|
2018-05-26 22:14:21 +00:00
|
|
|
|
|
|
|
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__));
|
|
|
|
|
2018-05-26 22:14:21 +00:00
|
|
|
/**
|
|
|
|
* FUNCTIONS
|
|
|
|
*/
|
|
|
|
|
|
|
|
function misuzu_log(string $text = ''): void
|
|
|
|
{
|
|
|
|
echo strlen($text) > 0 ? date('<H:i:s> ') . $text . PHP_EOL : PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDirIfNotExist(string $dir): void
|
|
|
|
{
|
|
|
|
if (!file_exists($dir) || !is_dir($dir)) {
|
|
|
|
mkdir($dir);
|
|
|
|
misuzu_log("Created '{$dir}'!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function globDir(string $dir, string $pattern, int $flags = 0): array
|
|
|
|
{
|
|
|
|
return glob($dir . '/' . $pattern, $flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteAllFilesInDir(string $dir, string $pattern): void
|
|
|
|
{
|
|
|
|
$files = globDir($dir, $pattern);
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
2018-09-17 19:07:10 +00:00
|
|
|
if (is_dir($file)) {
|
|
|
|
misuzu_log("'{$file}' is a directory, entering...");
|
|
|
|
deleteAllFilesInDir($file, $pattern);
|
|
|
|
rmdir($file);
|
|
|
|
} else {
|
|
|
|
unlink($file);
|
|
|
|
misuzu_log("Deleted '{$file}'");
|
|
|
|
}
|
2018-05-26 22:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 16:40:41 +00:00
|
|
|
function recursiveCopy(string $source, string $dest): bool
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
recursiveCopy($source . DIRECTORY_SEPARATOR . $entry, $dest . DIRECTORY_SEPARATOR . $entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dir->close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:55:05 +00:00
|
|
|
function recursiveConcat(string $source, string $existing = ''): string
|
|
|
|
{
|
|
|
|
if (!is_dir($source)) {
|
|
|
|
return $existing . file_get_contents($source);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dir = dir($source);
|
|
|
|
|
|
|
|
while (($entry = $dir->read()) !== false) {
|
|
|
|
if ($entry === '.' || $entry === '..') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$existing = recursiveConcat($source . DIRECTORY_SEPARATOR . $entry, $existing);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dir->close();
|
|
|
|
|
|
|
|
return $existing;
|
|
|
|
}
|
|
|
|
|
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__);
|
|
|
|
|
2018-05-26 22:14:21 +00:00
|
|
|
misuzu_log('Cleanup');
|
|
|
|
|
2018-11-17 20:37:18 +00:00
|
|
|
if ($doCss) {
|
|
|
|
createDirIfNotExist(CSS_DIR);
|
|
|
|
deleteAllFilesInDir(CSS_DIR, '*.css');
|
|
|
|
}
|
2018-05-26 22:14:21 +00:00
|
|
|
|
2018-11-17 20:37:18 +00:00
|
|
|
if ($doJs) {
|
|
|
|
createDirIfNotExist(JS_DIR);
|
|
|
|
deleteAllFilesInDir(JS_DIR, '*.js');
|
|
|
|
deleteAllFilesInDir(TS_DIR, '*.d.ts');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($doCss) {
|
|
|
|
misuzu_log();
|
|
|
|
misuzu_log('Compiling LESS');
|
|
|
|
|
|
|
|
if (!is_file(LESS_DIR . LESS_ENTRY_POINT)) {
|
|
|
|
misuzu_log('==> ERR: Entry point for this style does not exist (' . basename(LESS_ENTRY_POINT) . ')');
|
|
|
|
} else {
|
|
|
|
system(sprintf(LESS_CMD, escapeshellarg(LESS_DIR . LESS_ENTRY_POINT), LESS_DEST));
|
|
|
|
}
|
2018-05-26 22:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
misuzu_log();
|
|
|
|
misuzu_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,
|
2018-05-26 22:14:21 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'JavaScript',
|
|
|
|
'files' => NODE_IMPORT_JS,
|
|
|
|
'destination' => NODE_DEST_JS,
|
|
|
|
'insert-semicolon' => true,
|
2018-11-17 20:37:18 +00:00
|
|
|
'do' => $doJs,
|
2018-05-26 22:14:21 +00:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach (IMPORT_SEQ as $sequence) {
|
2018-11-17 20:37:18 +00:00
|
|
|
if (!$sequence['do']) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-26 22:14:21 +00:00
|
|
|
misuzu_log("=> {$sequence['name']}");
|
|
|
|
|
|
|
|
$contents = '';
|
|
|
|
|
|
|
|
foreach ($sequence['files'] as $file) {
|
|
|
|
$realpath = realpath(NODE_MODULES_DIR . '/' . $file);
|
|
|
|
|
|
|
|
misuzu_log("==> '{$file}'");
|
|
|
|
|
|
|
|
if (!file_exists($realpath)) {
|
|
|
|
misuzu_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) {
|
|
|
|
misuzu_log();
|
|
|
|
misuzu_log('Compiling TypeScript');
|
|
|
|
misuzu_log(shell_exec('tsc --extendedDiagnostics -p tsconfig.json'));
|
|
|
|
file_put_contents(TS_DEST, recursiveConcat(TS_SRC));
|
2019-01-30 09:19:35 +00:00
|
|
|
deleteAllFilesInDir(TS_SRC, '*');
|
2018-11-17 20:37:18 +00:00
|
|
|
rmdir(TS_SRC);
|
|
|
|
}
|
2018-11-06 22:55:05 +00:00
|
|
|
|
2018-09-28 16:40:41 +00:00
|
|
|
misuzu_log();
|
|
|
|
misuzu_log('Copying data...');
|
|
|
|
|
|
|
|
foreach (NODE_COPY_DIRECTORY as $source => $dest) {
|
|
|
|
misuzu_log("=> " . basename($dest));
|
|
|
|
$source = realpath(NODE_MODULES_DIR . DIRECTORY_SEPARATOR . $source);
|
|
|
|
$dest = PUBLIC_DIR . DIRECTORY_SEPARATOR . $dest;
|
|
|
|
deleteAllFilesInDir($dest, '*');
|
|
|
|
recursiveCopy($source, $dest);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
misuzu_log();
|
|
|
|
misuzu_log('Deleting template cache');
|
|
|
|
deleteAllFilesInDir(TWIG_DIRECTORY, '*');
|
|
|
|
}
|