2020-01-01 20:21:43 +03:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Build jekyll site and store site files in ./_site
|
2020-01-02 16:17:49 +03:00
|
|
|
# v2.0
|
|
|
|
# https://github.com/cotes2020/jekyll-theme-chirpy
|
2020-01-01 20:21:43 +03:00
|
|
|
# © 2019 Cotes Chung
|
|
|
|
# Published under MIT License
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
2020-08-30 09:49:36 +03:00
|
|
|
WORK_DIR="$(dirname "$(dirname "$(realpath "$0")")")"
|
2020-01-01 20:21:43 +03:00
|
|
|
|
2020-08-19 12:05:58 +03:00
|
|
|
CONTAINER="${WORK_DIR}/.container"
|
2020-01-01 20:21:43 +03:00
|
|
|
|
2020-10-12 09:19:01 +03:00
|
|
|
dest="${WORK_DIR}/_site"
|
|
|
|
|
|
|
|
cmd="JEKYLL_ENV=production bundle exec jekyll b"
|
2020-01-01 20:21:43 +03:00
|
|
|
|
2020-10-08 11:00:08 +03:00
|
|
|
docker=false
|
|
|
|
|
2020-10-12 09:19:01 +03:00
|
|
|
config=""
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
_help() {
|
2020-01-01 20:21:43 +03:00
|
|
|
echo "Usage:"
|
|
|
|
echo
|
|
|
|
echo " bash build.sh [options]"
|
|
|
|
echo
|
|
|
|
echo "Options:"
|
2020-10-12 09:19:01 +03:00
|
|
|
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)"
|
|
|
|
echo " --docker Build site within docker"
|
|
|
|
echo " --config <CONFIG_a[,CONFIG_b]> Specify config files"
|
2020-10-08 11:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_install_tools() {
|
2020-10-09 18:46:28 +03:00
|
|
|
# docker image `jekyll/jekyll` based on Alpine Linux
|
2020-10-08 11:00:08 +03:00
|
|
|
echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
|
|
|
|
apk update
|
|
|
|
apk add yq
|
2020-01-01 20:21:43 +03:00
|
|
|
}
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
_init() {
|
2020-08-19 12:05:58 +03:00
|
|
|
cd "$WORK_DIR"
|
2020-01-01 20:21:43 +03:00
|
|
|
|
2020-10-08 11:00:08 +03:00
|
|
|
if [[ -f Gemfile.lock ]]; then
|
|
|
|
rm -f Gemfile.lock
|
|
|
|
fi
|
|
|
|
|
2020-09-16 20:28:44 +03:00
|
|
|
if [[ -d $CONTAINER ]]; then
|
2020-08-19 12:05:58 +03:00
|
|
|
rm -rf "$CONTAINER"
|
2020-01-01 20:21:43 +03:00
|
|
|
fi
|
|
|
|
|
2020-10-12 09:19:01 +03:00
|
|
|
if [[ -d $dest ]]; then
|
2020-09-16 20:28:44 +03:00
|
|
|
bundle exec jekyll clean
|
2020-01-01 20:21:43 +03:00
|
|
|
fi
|
|
|
|
|
2020-08-19 12:05:58 +03:00
|
|
|
local _temp="$(mktemp -d)"
|
|
|
|
cp -r ./* "$_temp"
|
|
|
|
cp -r ./.git "$_temp"
|
|
|
|
mv "$_temp" "$CONTAINER"
|
2020-01-01 20:21:43 +03:00
|
|
|
}
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
_build() {
|
2020-08-19 12:05:58 +03:00
|
|
|
cd "$CONTAINER"
|
2020-01-01 20:21:43 +03:00
|
|
|
echo "$ cd $(pwd)"
|
2020-04-09 22:15:51 +03:00
|
|
|
|
2020-08-19 12:05:58 +03:00
|
|
|
bash "_scripts/sh/create_pages.sh"
|
|
|
|
bash "_scripts/sh/dump_lastmod.sh"
|
2020-01-01 20:21:43 +03:00
|
|
|
|
2020-10-12 09:19:01 +03:00
|
|
|
cmd+=" -d $dest"
|
|
|
|
|
|
|
|
if [[ -n $config ]]; then
|
|
|
|
cmd+=" --config $config"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "\$ $cmd"
|
|
|
|
eval "$cmd"
|
|
|
|
echo -e "\nBuild success, the site files have been placed in '${dest}'."
|
2020-01-01 20:21:43 +03:00
|
|
|
|
2020-10-12 09:19:01 +03:00
|
|
|
if [[ -d "${dest}/.git" ]]; then
|
|
|
|
if [[ -n $(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 $dest to remote master branch.\n"
|
2020-01-01 20:21:43 +03:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-08-19 12:05:58 +03:00
|
|
|
cd .. && rm -rf "$CONTAINER"
|
2020-01-01 20:21:43 +03:00
|
|
|
}
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
_check_unset() {
|
2020-09-16 20:28:44 +03:00
|
|
|
if [[ -z ${1:+unset} ]]; then
|
2020-04-09 22:15:51 +03:00
|
|
|
_help
|
2020-01-01 20:21:43 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2020-09-16 20:28:44 +03:00
|
|
|
while [[ $# -gt 0 ]]; do
|
2020-01-01 20:21:43 +03:00
|
|
|
opt="$1"
|
|
|
|
case $opt in
|
2020-09-16 20:28:44 +03:00
|
|
|
-b | --baseurl)
|
2020-07-28 21:15:53 +03:00
|
|
|
local _baseurl="$2"
|
2020-09-16 20:28:44 +03:00
|
|
|
if [[ -z $_baseurl ]]; then
|
2020-07-28 21:15:53 +03:00
|
|
|
_baseurl='""'
|
2020-01-01 20:21:43 +03:00
|
|
|
fi
|
2020-10-12 09:19:01 +03:00
|
|
|
cmd+=" -b $_baseurl"
|
2020-01-01 20:21:43 +03:00
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
2020-09-16 20:28:44 +03:00
|
|
|
-d | --destination)
|
2020-08-19 12:05:58 +03:00
|
|
|
_check_unset "$2"
|
2020-10-12 09:19:01 +03:00
|
|
|
dest="$(realpath "$2")"
|
2020-09-16 20:28:44 +03:00
|
|
|
shift
|
|
|
|
shift
|
2020-01-01 20:21:43 +03:00
|
|
|
;;
|
2020-10-08 11:00:08 +03:00
|
|
|
--docker)
|
|
|
|
docker=true
|
|
|
|
shift
|
|
|
|
;;
|
2020-10-12 09:19:01 +03:00
|
|
|
--config)
|
|
|
|
_check_unset "$2"
|
|
|
|
config="$(realpath "$2")"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
2020-09-16 20:28:44 +03:00
|
|
|
-h | --help)
|
2020-04-09 22:15:51 +03:00
|
|
|
_help
|
2020-01-01 20:21:43 +03:00
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*) # unknown option
|
2020-04-09 22:15:51 +03:00
|
|
|
_help
|
2020-01-01 20:21:43 +03:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-10-08 11:00:08 +03:00
|
|
|
if $docker; then
|
|
|
|
_install_tools
|
|
|
|
fi
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
_init
|
|
|
|
_build
|
2020-01-01 20:21:43 +03:00
|
|
|
}
|
|
|
|
|
2020-01-08 23:11:59 +03:00
|
|
|
main "$@"
|