Replace build bash script with a PHP version.
This commit is contained in:
parent
aa977fc338
commit
44d86d523b
2 changed files with 149 additions and 87 deletions
149
build.php
Normal file
149
build.php
Normal file
|
@ -0,0 +1,149 @@
|
|||
<?php
|
||||
/**
|
||||
* Misuzu Asset Build Script.
|
||||
*/
|
||||
|
||||
/**
|
||||
* NPM module files to be imported into /js/libraries.js
|
||||
*/
|
||||
define('NODE_IMPORT_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', [
|
||||
]);
|
||||
|
||||
|
||||
/**
|
||||
* 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 . '/%s.css');
|
||||
define('TS_DEST', JS_DIR . '/%s.js');
|
||||
define('DTS_DEST', TS_DIR . '/%s.d.ts');
|
||||
|
||||
define('NODE_MODULES_DIR', __DIR__ . '/node_modules');
|
||||
define('NODE_DEST_CSS', CSS_DIR . '/libraries.css');
|
||||
define('NODE_DEST_JS', JS_DIR . '/libraries.js');
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
unlink($file);
|
||||
misuzu_log("Deleted '{$file}'");
|
||||
}
|
||||
}
|
||||
|
||||
misuzu_log('Cleanup');
|
||||
createDirIfNotExist(CSS_DIR);
|
||||
createDirIfNotExist(JS_DIR);
|
||||
deleteAllFilesInDir(CSS_DIR, '*.css');
|
||||
deleteAllFilesInDir(JS_DIR, '*.js');
|
||||
deleteAllFilesInDir(TS_DIR, '*.d.ts');
|
||||
|
||||
misuzu_log();
|
||||
misuzu_log('Compiling LESS');
|
||||
|
||||
$styles = globDir(LESS_DIR, '*', GLOB_ONLYDIR);
|
||||
|
||||
foreach ($styles as $style) {
|
||||
$basename = basename($style);
|
||||
$destination = sprintf(LESS_DEST, $basename);
|
||||
$entryPoint = $style . LESS_ENTRY_POINT;
|
||||
misuzu_log("=> {$basename}");
|
||||
|
||||
if (!file_exists($entryPoint)) {
|
||||
misuzu_log('==> ERR: Entry point for this style does not exist (' . basename(LESS_ENTRY_POINT) . ')');
|
||||
continue;
|
||||
}
|
||||
|
||||
$compileCmd = sprintf(LESS_CMD, escapeshellarg($entryPoint), escapeshellarg($destination));
|
||||
system($compileCmd);
|
||||
}
|
||||
|
||||
// figure this out
|
||||
//misuzu_log();
|
||||
//misuzu_log('Compiling TypeScript');
|
||||
|
||||
misuzu_log();
|
||||
misuzu_log('Importing libraries');
|
||||
|
||||
define('IMPORT_SEQ', [
|
||||
[
|
||||
'name' => 'CSS',
|
||||
'files' => NODE_IMPORT_CSS,
|
||||
'destination' => NODE_DEST_CSS,
|
||||
'insert-semicolon' => false,
|
||||
],
|
||||
[
|
||||
'name' => 'JavaScript',
|
||||
'files' => NODE_IMPORT_JS,
|
||||
'destination' => NODE_DEST_JS,
|
||||
'insert-semicolon' => true,
|
||||
],
|
||||
]);
|
||||
|
||||
foreach (IMPORT_SEQ as $sequence) {
|
||||
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);
|
||||
}
|
87
build.sh
87
build.sh
|
@ -1,87 +0,0 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
ASSETS_PATH='./assets'
|
||||
ASSETS_LESS="$ASSETS_PATH/less"
|
||||
ASSETS_TS="$ASSETS_PATH/typescript"
|
||||
|
||||
LESS_ENTRY_FILE='main.less'
|
||||
|
||||
PUBLIC_DIR='./public'
|
||||
PUBLIC_CSS="$PUBLIC_DIR/css"
|
||||
PUBLIC_JS="$PUBLIC_DIR/js"
|
||||
|
||||
NODE_PATH='./node_modules'
|
||||
|
||||
NODE_DEST_JS="$PUBLIC_JS/libraries.js"
|
||||
NODE_IMPORT_JS=(
|
||||
#'turbolinks/dist/turbolinks.js'
|
||||
#'highlightjs/highlight.pack.min.js'
|
||||
'timeago.js/dist/timeago.min.js'
|
||||
'timeago.js/dist/timeago.locales.min.js'
|
||||
)
|
||||
|
||||
NODE_DEST_CSS="$PUBLIC_CSS/libraries.css"
|
||||
NODE_IMPORT_CSS=(
|
||||
#'highlightjs/styles/default.css'
|
||||
)
|
||||
|
||||
# delete old files, using find to avoid errors
|
||||
echo "=> Cleanup"
|
||||
mkdir -p $PUBLIC_CSS
|
||||
mkdir -p $PUBLIC_JS
|
||||
find $ASSETS_TS -type f -name "*.d.ts" -delete -print
|
||||
find $PUBLIC_CSS -type f -name "*.css" -delete -print
|
||||
find $PUBLIC_JS -type f -name "*.js" -delete -print
|
||||
echo
|
||||
|
||||
# styles
|
||||
echo
|
||||
echo "=> LESS"
|
||||
for STYLE_DIR in $ASSETS_LESS/*/; do
|
||||
STYLE_NAME=`basename $STYLE_DIR | tr '[A-Z]' '[a-z]'`
|
||||
echo "==> $STYLE_NAME"
|
||||
lessc --verbose $STYLE_DIR/$LESS_ENTRY_FILE $PUBLIC_CSS/$STYLE_NAME.css
|
||||
echo
|
||||
done
|
||||
|
||||
# scripts (don't need yet, build script gets stuck here bc no files exists)
|
||||
# echo
|
||||
# echo "=> TypeScript"
|
||||
# for SCRIPT_DIR in $ASSETS_TS/*/; do
|
||||
# SCRIPT_NAME=`basename $SCRIPT_DIR`
|
||||
# SCRIPT_NAME_LOWER=`echo $SCRIPT_NAME | tr '[A-Z]' '[a-z]'`
|
||||
# echo "==> $SCRIPT_NAME"
|
||||
# find $SCRIPT_DIR -name "*.ts" | xargs tsc \
|
||||
# -d \
|
||||
# -t es5 \
|
||||
# --listFiles \
|
||||
# --listEmittedFiles \
|
||||
# --noImplicitAny \
|
||||
# --removeComments \
|
||||
# --outFile $PUBLIC_JS/$SCRIPT_NAME_LOWER.js
|
||||
# mv -v $PUBLIC_JS/$SCRIPT_NAME_LOWER.d.ts $ASSETS_TS/$SCRIPT_NAME.d.ts
|
||||
# echo
|
||||
# done
|
||||
|
||||
# node imports
|
||||
echo
|
||||
echo "=> NPM imports"
|
||||
|
||||
echo "==> JavaScript"
|
||||
echo "Creating $NODE_DEST_JS"
|
||||
touch $NODE_DEST_JS
|
||||
for FILE in "${NODE_IMPORT_JS[@]}"; do
|
||||
echo "===> $FILE"
|
||||
cat "$NODE_PATH/$FILE" >> $NODE_DEST_JS
|
||||
done
|
||||
echo
|
||||
|
||||
echo "==> CSS"
|
||||
echo "Creating $NODE_DEST_CSS"
|
||||
touch $NODE_DEST_CSS
|
||||
for FILE in "${NODE_IMPORT_CSS[@]}"; do
|
||||
echo "===> $FILE"
|
||||
cat "$NODE_PATH/$FILE" >> $NODE_DEST_CSS
|
||||
done
|
||||
|
||||
|
Loading…
Add table
Reference in a new issue