aboutsummaryrefslogtreecommitdiff
path: root/deploy
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2022-07-11 02:59:35 +0300
committerEvgeny Zinoviev <me@ch1p.io>2022-07-11 02:59:40 +0300
commit864e73cdc75a2fb0e4fad500f649dae2343c10a8 (patch)
tree6ce6762c6be72c98592a32fe0bed4f2ce751d544 /deploy
parentcb13ea239b9f1ca6aea43125d5694d5a55dcd287 (diff)
rewrite css and js assets building
Diffstat (limited to 'deploy')
-rw-r--r--deploy/build_common.sh74
-rwxr-xr-xdeploy/build_css.sh62
-rwxr-xr-xdeploy/build_js.sh31
-rwxr-xr-xdeploy/deploy.sh52
-rwxr-xr-xdeploy/gen_css_diff.js14
-rwxr-xr-xdeploy/gen_static_config.php57
6 files changed, 290 insertions, 0 deletions
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 <<EOF
+usage: $PROGNAME [OPTIONS]
+
+Options:
+ -o output directory
+ -i input directory
+ -h show this help
+EOF
+ exit $code
+}
+
+input_args() {
+ [ -z "$1" ] && usage
+
+ while [[ $# -gt 0 ]]; do
+ case $1 in
+ -o)
+ OUTDIR="$2"
+ shift
+ ;;
+ -i)
+ INDIR="$2"
+ shift
+ ;;
+ -h)
+ usage
+ ;;
+ *)
+ die "unexpected argument: $1"
+ ;;
+ esac
+ shift
+ done
+}
+
+check_args() {
+ [ -z "$OUTDIR" ] && {
+ error "output directory not specified"
+ usage 1
+ }
+ [ -z "$INDIR" ] && {
+ error "input directory not specified"
+ usage 1
+ }
+
+ if [ ! -d "$OUTDIR" ]; then
+ mkdir "$OUTDIR"
+ else
+ warning "$OUTDIR already exists, erasing it"
+ rm "$OUTDIR"/*
+ fi
+} \ No newline at end of file
diff --git a/deploy/build_css.sh b/deploy/build_css.sh
new file mode 100755
index 0000000..2129ea2
--- /dev/null
+++ b/deploy/build_css.sh
@@ -0,0 +1,62 @@
+#!/bin/bash
+
+PROGNAME="$0"
+DIR="$( cd "$( dirname "$(readlink -f "${BASH_SOURCE[0]}")" )" && pwd )"
+ROOT="$(realpath "$DIR/../")"
+CLEANCSS="$ROOT"/node_modules/clean-css-cli/bin/cleancss
+
+. $DIR/build_common.sh
+
+build_scss() {
+ local entry_name="$1"
+ local theme="$2"
+
+ local input="$INDIR/entries/$entry_name/$theme.scss"
+ local output="$OUTDIR/$entry_name"
+ [ "$theme" = "dark" ] && output="${output}_dark"
+ output="${output}.css"
+
+ sassc -t compressed "$input" "$output"
+}
+
+cleancss() {
+ local entry_name="$1"
+ local theme="$2"
+
+ local file="$OUTDIR/$entry_name"
+ [ "$theme" = "dark" ] && file="${file}_dark"
+ file="${file}.css"
+
+ $CLEANCSS -O2 "all:on;mergeSemantically:on;restructureRules:on" "$file" > "$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
+<?php
+
+require __DIR__.'/../init.php';
+
+if ($argc <= 1) {
+ usage();
+ exit(1);
+}
+
+$input_dir = null;
+
+array_shift($argv);
+while (count($argv) > 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 "<?php\n\n";
+echo "return ".var_export($hashes, true).";\n";
+
+function usage(): void {
+ global $argv;
+ echo <<<EOF
+usage: {$argv[0]} [OPTIONS]
+
+Options:
+ -i input htdocs directory
+
+EOF;
+}
+
+function get_hash(string $path): string {
+ return substr(sha1(file_get_contents($path)), 0, 8);
+} \ No newline at end of file