From 864e73cdc75a2fb0e4fad500f649dae2343c10a8 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Mon, 11 Jul 2022 02:59:35 +0300 Subject: rewrite css and js assets building --- deploy/build_common.sh | 74 ++++++++++++++++++++++++++++++++++++++++++++ deploy/build_css.sh | 62 +++++++++++++++++++++++++++++++++++++ deploy/build_js.sh | 31 +++++++++++++++++++ deploy/deploy.sh | 52 +++++++++++++++++++++++++++++++ deploy/gen_css_diff.js | 14 +++++++++ deploy/gen_static_config.php | 57 ++++++++++++++++++++++++++++++++++ 6 files changed, 290 insertions(+) create mode 100644 deploy/build_common.sh create mode 100755 deploy/build_css.sh create mode 100755 deploy/build_js.sh create mode 100755 deploy/deploy.sh create mode 100755 deploy/gen_css_diff.js create mode 100755 deploy/gen_static_config.php (limited to 'deploy') diff --git a/deploy/build_common.sh b/deploy/build_common.sh new file mode 100644 index 0000000..acac5da --- /dev/null +++ b/deploy/build_common.sh @@ -0,0 +1,74 @@ +#!/bin/bash + +set -e + +INDIR= +OUTDIR= + +error() { + >&2 echo "error: $@" +} + +warning() { + >&2 echo "warning: $@" +} + +die() { + error "$@" + exit 1 +} + +usage() { + local code="$1" + cat < "$file.tmp" + rm "$file" + mv "$file.tmp" "$file" +} + +create_dark_patch() { + local entry_name="$1" + local light_file="$OUTDIR/$entry_name.css" + local dark_file="$OUTDIR/${entry_name}_dark.css" + + "$DIR"/gen_css_diff.js "$light_file" "$dark_file" > "$dark_file.diff" + rm "$dark_file" + mv "$dark_file.diff" "$dark_file" +} + +THEMES="light dark" +TARGETS="common admin" + +input_args "$@" +check_args + +[ -x "$CLEANCSS" ] || die "cleancss is not found" + +for theme in $THEMES; do + for target in $TARGETS; do + build_scss "$target" "$theme" + done +done + +for target in $TARGETS; do + create_dark_patch "$target" + for theme in $THEMES; do cleancss "$target" "$theme"; done +done \ No newline at end of file diff --git a/deploy/build_js.sh b/deploy/build_js.sh new file mode 100755 index 0000000..b1019f7 --- /dev/null +++ b/deploy/build_js.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +PROGNAME="$0" +DIR="$( cd "$( dirname "$(readlink -f "${BASH_SOURCE[0]}")" )" && pwd )" + +. $DIR/build_common.sh + +# suckless version of webpack +# watch and learn, bitches! +build_chunk() { + local name="$1" + local output="$OUTDIR/$name.js" + local not_first=0 + for file in "$INDIR/$name"/*.js; do + # insert newline before out comment + [ "$not_first" = "1" ] && echo "" >> "$output" + echo "/* $(basename "$file") */" >> "$output" + + cat "$file" >> "$output" + not_first=1 + done +} + +TARGETS="common admin" + +input_args "$@" +check_args + +for f in $TARGETS; do + build_chunk "$f" +done \ No newline at end of file diff --git a/deploy/deploy.sh b/deploy/deploy.sh new file mode 100755 index 0000000..7ef28a3 --- /dev/null +++ b/deploy/deploy.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +set -e + +DIR="$( cd "$( dirname "$(readlink -f "${BASH_SOURCE[0]}")" )" && pwd )" + +DEV_DIR="$(realpath "$DIR/../")" +STAGING_DIR="$HOME/staging" +PROD_DIR="$HOME/prod" +PHP=/usr/bin/php8.1 + +git push origin master + +[ -d "$STAGING_DIR" ] || mkdir "$STAGING_DIR" +pushd "$STAGING_DIR" + +if [ ! -d .git ]; then + git init + git remote add origin git@ch1p.io:ch1p_io_web.git + git fetch + git checkout master +fi + +git reset --hard +git pull origin master + +composer8.1 install --no-dev --optimize-autoloader --ignore-platform-reqs + +if [ ! -d node_modules ]; then + npm i +fi + +cp "$DEV_DIR/config-local.php" . +sed -i '/is_dev/d' ./config-local.php + +"$DIR"/build_js.sh -i "$DEV_DIR/htdocs/js" -o "$STAGING_DIR/htdocs/dist-js" || die "build_js failed" +"$DIR"/build_css.sh -i "$DEV_DIR/htdocs/scss" -o "$STAGING_DIR/htdocs/dist-css" || die "build_css failed" +$PHP "$DIR"/gen_static_config.php > "$STAGING_DIR/config-static.php" || die "gen_static_config failed" + +popd + +# copy staging to prod +rsync -a --delete --delete-excluded --info=progress2 "$STAGING_DIR/" "$PROD_DIR/" \ + --exclude .git \ + --exclude debug.log \ + --exclude='/composer.*' \ + --exclude='/htdocs/scss' \ + --exclude='/htdocs/js' \ + --exclude='/htdocs/sass.php' \ + --exclude='/htdocs/js.php' \ + --exclude='*.sh' \ + --exclude='*.sql' diff --git a/deploy/gen_css_diff.js b/deploy/gen_css_diff.js new file mode 100755 index 0000000..5ca1945 --- /dev/null +++ b/deploy/gen_css_diff.js @@ -0,0 +1,14 @@ +#!/usr/bin/env node +const {generateCSSPatch} = require('css-patch') +const fs = require('fs') + +const files = process.argv.slice(2) +if (files.length !== 2) { + console.log(`usage: ${process.argv[0]} file1 file2`) + process.exit() +} + +const css1 = fs.readFileSync(files[0], 'utf-8') +const css2 = fs.readFileSync(files[1], 'utf-8') + +console.log(generateCSSPatch(css1, css2)) \ No newline at end of file diff --git a/deploy/gen_static_config.php b/deploy/gen_static_config.php new file mode 100755 index 0000000..09058ad --- /dev/null +++ b/deploy/gen_static_config.php @@ -0,0 +1,57 @@ +#!/usr/bin/env php8.1 + 0) { + switch ($argv[0]) { + case '-i': + array_shift($argv); + $input_dir = array_shift($argv); + break; + + default: + cli::die('unsupported argument: '.$argv[0]); + } +} + +if (is_null($input_dir)) + cli::die("input directory has not been specified"); + +$hashes = []; +foreach (['css', 'js'] as $type) { + $entries = glob_recursive($input_dir.'/dist-'.$type.'/*.'.$type); + if (empty($entries)) { + cli::error("warning: no files found in $input_dir/dist-$type"); + continue; + } + + foreach ($entries as $file) + $hashes[$type.'/'.basename($file)] = get_hash($file); +} + +echo "