aboutsummaryrefslogtreecommitdiff
path: root/build_css.sh
diff options
context:
space:
mode:
Diffstat (limited to 'build_css.sh')
-rwxr-xr-xbuild_css.sh137
1 files changed, 137 insertions, 0 deletions
diff --git a/build_css.sh b/build_css.sh
new file mode 100755
index 0000000..9334ce0
--- /dev/null
+++ b/build_css.sh
@@ -0,0 +1,137 @@
+#!/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
+LESSC="$ROOT"/node_modules/.bin/lessc
+THEMES="light dark"
+TARGETS="app"
+INDIR=
+OUTDIR=
+TYPE=
+
+error() {
+ >&2 echo "error: $@"
+}
+
+die() {
+ error "$@"
+ exit 1
+}
+
+installed() {
+ command -v "$1" > /dev/null
+ return $?
+}
+
+usage() {
+ cat <<EOF
+usage: $PROGNAME [OPTIONS]
+
+Options:
+ -o output directory
+ -i input directory
+ -t 'less' or 'scss'
+ -h show this help
+EOF
+ exit
+}
+
+build() {
+ local entry_name="$1"
+ local theme="$2"
+
+ local input="$INDIR/entries/$entry_name/$theme.$TYPE"
+ local output="$OUTDIR/$entry_name"
+ [ "$theme" = "dark" ] && output="${output}_dark"
+ output="${output}.css"
+
+ case "$TYPE" in
+ less) "$LESSC" -x "$input" "$output" ;;
+ scss) sassc -t compressed "$input" "$output" ;;
+ esac
+}
+
+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"
+}
+
+[ -z "$1" ] && usage
+
+while [[ $# -gt 0 ]]; do
+ case $1 in
+ -o)
+ OUTDIR="$2"
+ shift
+ ;;
+ -i)
+ INDIR="$2"
+ shift
+ ;;
+
+ -t)
+ case $2 in
+ less|scss) TYPE="$2" ;;
+ *) die "invalid type: $2" ;;
+ esac
+ shift
+ ;;
+
+ -h)
+ usage
+ ;;
+ *)
+ die "unexpected argument: $1"
+ ;;
+ esac
+ shift
+done
+
+[ -z "$OUTDIR" ] && die "output directory is not specified"
+[ -z "$INDIR" ] && die "input directory is not specified"
+[ -z "$TYPE" ] && die "type is not specified"
+
+if [ ! -d "$OUTDIR" ]; then
+ mkdir "$OUTDIR"
+else
+ rm "$OUTDIR"/*
+fi
+
+[ -x "$CLEANCSS" ] || die "$CLEANCSS: cleancss not found"
+case "$TYPE" in
+ less) [ -x "$LESSC" ] || die "$LESSC: lessc not found" ;;
+ scss) installed sassc || die "sassc not found" ;;
+esac
+
+for theme in $THEMES; do
+ for target in $TARGETS; do
+ build "$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