web/build.sh

110 lines
1.7 KiB
Bash
Raw Normal View History

2019-09-30 15:38:41 +03:00
#!/bin/bash
#
# Build jekyll site and store site files in ./_site
# © 2019 Cotes Chung
# Published under MIT License
2019-09-30 22:38:36 +03:00
CMD="JEKYLL_ENV=production bundle exec jekyll b"
DEST=$(realpath '_site')
2019-09-30 15:38:41 +03:00
help() {
echo "Usage:"
echo
echo " bash build.sh [options]"
echo
echo "Options:"
echo " -b, --baseurl <URL> The site relative url that start with slash, e.g. '/project'"
echo " -h, --help Print the help information"
echo " -d, --destination <DIR> Destination directory (defaults to ./_site)"
}
init() {
set -eu
if [[ -d .container ]]; then
rm -rf .container
fi
if [[ -d _site ]]; then
2019-09-30 22:38:36 +03:00
jekyll clean
2019-09-30 15:38:41 +03:00
fi
2019-09-30 22:38:36 +03:00
temp=$(mktemp -d)
cp -r * $temp
cp -r .git $temp
mv $temp .container
2019-09-30 15:38:41 +03:00
}
check_unset() {
if [[ -z ${1:+unset} ]]
then
help
exit 1
fi
}
while [[ $# -gt 0 ]]
do
opt="$1"
case $opt in
-b|--baseurl)
check_unset $2
if [[ $2 == \/* ]]
then
CMD+=" -b $2"
else
help
exit 1
fi
shift
shift
;;
-d|--destination)
check_unset $2
2019-09-30 22:38:36 +03:00
DEST=$(realpath $2)
2019-09-30 15:38:41 +03:00
shift;
shift;
;;
-h|--help)
help
exit 0
;;
*) # unknown option
help
exit 1
;;
esac
done
2019-09-30 22:38:36 +03:00
2019-09-30 15:38:41 +03:00
init
cd .container
echo "$ cd $(pwd)"
python _scripts/py/init_all.py
2019-09-30 22:38:36 +03:00
CMD+=" -d ${DEST}"
2019-09-30 15:38:41 +03:00
echo "\$ $CMD"
eval $CMD
2019-09-30 22:38:36 +03:00
echo -e "\nBuild success, the site files placed in '${DEST}'."
if [[ -d ${DEST}/.git ]]; then
if [[ ! -z $(git -C $DEST status -s) ]]; then
git -C $DEST add .
git -C $DEST commit -m "[Automation] Update site files." -q
echo -e "\nPlease push the changes of '$(realpath $DEST)' to remote master branch.\n"
fi
2019-09-30 15:38:41 +03:00
2019-09-30 22:38:36 +03:00
fi
2019-09-30 15:38:41 +03:00
cd .. && rm -rf .container