From 44d86d523be3fdb69028d539f70bf9b6ce5ee53f Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 27 May 2018 00:14:21 +0200 Subject: [PATCH] Replace build bash script with a PHP version. --- build.php | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ build.sh | 87 ------------------------------- 2 files changed, 149 insertions(+), 87 deletions(-) create mode 100644 build.php delete mode 100644 build.sh diff --git a/build.php b/build.php new file mode 100644 index 00000000..971783fe --- /dev/null +++ b/build.php @@ -0,0 +1,149 @@ + 0 ? date(' ') . $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); +} diff --git a/build.sh b/build.sh deleted file mode 100644 index c00a34f1..00000000 --- a/build.sh +++ /dev/null @@ -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 - -