aboutsummaryrefslogtreecommitdiff
path: root/build_css.sh
blob: 9334ce0ddb3def5af36b9d03648048600d5431fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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