|
| 1 | +#!/bin/bash |
| 2 | +trap "kill 0" EXIT |
| 3 | + |
| 4 | +# Set this variable to the name of the output directory |
| 5 | +BUILD_DIR=site/public |
| 6 | + |
| 7 | +WATCH=0 |
| 8 | + |
| 9 | +# Parsing parameters |
| 10 | +while [[ $# -gt 0 ]]; do |
| 11 | + key="$1" |
| 12 | + |
| 13 | + case $key in |
| 14 | + -o | --output-dir) |
| 15 | + CUSTOM_BUILD_DIR=$2 |
| 16 | + shift # past argument |
| 17 | + shift # past value |
| 18 | + ;; |
| 19 | + -v | --verbose) |
| 20 | + VERBOSE=1 |
| 21 | + shift |
| 22 | + ;; |
| 23 | + -w | --watch) |
| 24 | + WATCH=1 |
| 25 | + shift |
| 26 | + ;; |
| 27 | + -h | --help) |
| 28 | + echo "Usage : $0 [options]" |
| 29 | + echo |
| 30 | + echo "Options:" |
| 31 | + echo -e " -o, --output-dir BUILD_DIR\tOutput directory" |
| 32 | + echo -e " -v, --verbose\t\tVerbose Output!" |
| 33 | + echo -e " -w, --watch\t\tWatch Mode" |
| 34 | + echo -e " -h, --help\t\tDisplay this help and exit" |
| 35 | + exit 0 |
| 36 | + ;; |
| 37 | + *) # unknown option |
| 38 | + POSITIONAL+=("$1") # save it in an array for later |
| 39 | + shift # past argument |
| 40 | + ;; |
| 41 | + esac |
| 42 | +done |
| 43 | +echo $POSITIONAL |
| 44 | +set -- "${POSITIONAL[@]}" # restore positional parameters |
| 45 | + |
| 46 | +if [[ ! -z "${CUSTOM_BUILD_DIR}" ]]; then |
| 47 | + BUILD_DIR=${CUSTOM_BUILD_DIR} |
| 48 | +fi |
| 49 | + |
| 50 | +if [[ "${VERBOSE}" == "1" ]]; then |
| 51 | + echo "Creating build directories" |
| 52 | +fi |
| 53 | +mkdir -p ${BUILD_DIR} |
| 54 | +mkdir -p ${BUILD_DIR}/styles |
| 55 | +mkdir -p ${BUILD_DIR}/scripts |
| 56 | +mkdir -p ${BUILD_DIR}/resources |
| 57 | + |
| 58 | +if [[ "${VERBOSE}" == "1" ]]; then |
| 59 | + echo "Copying resources and scripts" |
| 60 | +fi |
| 61 | +cp src/scripts/* ${BUILD_DIR}/scripts/ --update |
| 62 | +cp -r src/resources/* ${BUILD_DIR}/resources/ --update |
| 63 | +cp src/root/* ${BUILD_DIR}/ --update |
| 64 | + |
| 65 | +if [[ "${VERBOSE}" == "1" && ! -z "${CUSTOM_BUILD_DIR}" && "${WATCH}" == "1" ]]; then |
| 66 | + echo "Updating Less Watch Compiler config" |
| 67 | +fi |
| 68 | +if [[ ! -z ${CUSTOM_BUILD_DIR} && "${WATCH}" == "1" ]]; then |
| 69 | + change_less_watch_json ${BUILD_DIR} |
| 70 | +fi |
| 71 | + |
| 72 | +if [[ "${WATCH}" == "1" ]]; then |
| 73 | + if [[ "${VERBOSE}" == "1" ]]; then |
| 74 | + echo "Compiling pug -> html" |
| 75 | + pug -w src/pug/pages/ --basedir ./ --out ${BUILD_DIR}/ & |
| 76 | + else |
| 77 | + pug -s -w src/pug/pages/ --basedir ./ --out ${BUILD_DIR}/ & |
| 78 | + fi |
| 79 | + if [[ "${VERBOSE}" == "1" ]]; then |
| 80 | + echo "Starting less watch compiler" |
| 81 | + fi |
| 82 | + less-watch-compiler & |
| 83 | + if [[ "${VERBOSE}" == "1" ]]; then |
| 84 | + echo "Starting live server" |
| 85 | + live-server --verbose --port=9600 ./${BUILD_DIR} & |
| 86 | + else |
| 87 | + live-server --port=9600 ./${BUILD_DIR} & |
| 88 | + fi |
| 89 | +fi |
| 90 | + |
| 91 | +if [[ "${WATCH}" == "0" ]]; then |
| 92 | + if [[ "${VERBOSE}" == "1" ]]; then |
| 93 | + echo "Compiling pug -> html" |
| 94 | + pug src/pug/pages/ --basedir ./ --out ${BUILD_DIR}/ & |
| 95 | + else |
| 96 | + pug -s src/pug/pages/ --basedir ./ --out ${BUILD_DIR}/ & |
| 97 | + fi |
| 98 | + if [[ "${VERBOSE}" == "1" ]]; then |
| 99 | + echo "Compiling less -> css" |
| 100 | + fi |
| 101 | + lessc ./src/less/index.less ${BUILD_DIR}/styles/index.css |
| 102 | + cleancss --inline all -o ${BUILD_DIR}/styles/index.min.css ${BUILD_DIR}/styles/index.css |
| 103 | + rm ${BUILD_DIR}/styles/index.css |
| 104 | +fi |
| 105 | +wait |
| 106 | + |
| 107 | +# Function to change directory in less-watch-compiler.config.json |
| 108 | +change_less_watch_json() { |
| 109 | + parse_json_script=$(mktemp parse_json.XXXX.py) |
| 110 | + cat > $parse_json_script << SCRIPT |
| 111 | + #!/usr/bin/env python |
| 112 | + import json |
| 113 | + filename = "less-watch-compiler.config.json" |
| 114 | + with open(filename, 'r+') as f: |
| 115 | + jsonObject = json.load(f) |
| 116 | + jsonObject['outputFolder'] = "$1/styles" |
| 117 | + f.seek(0) |
| 118 | + json.dump(jsonObject, f, indent=4, separators=(','," : ")) |
| 119 | +SCRIPT |
| 120 | + python $parse_json_script && rm $parse_json_script |
| 121 | +} |
0 commit comments