2019-09-30 15:38:41 +03:00
|
|
|
|
#!/bin/bash
|
2019-11-05 12:18:31 +03:00
|
|
|
|
|
2019-09-30 15:38:41 +03:00
|
|
|
|
# Run jekyll site at http://127.0.0.1:4000
|
2019-11-05 12:18:31 +03:00
|
|
|
|
#
|
|
|
|
|
# Requirement:
|
2019-11-18 17:01:59 +03:00
|
|
|
|
# Option '-r, --realtime' needs fswatch › http://emcrisostomo.github.io/fswatch/
|
2019-11-05 12:18:31 +03:00
|
|
|
|
#
|
2020-01-02 16:17:49 +03:00
|
|
|
|
# v2.0
|
|
|
|
|
# https://github.com/cotes2020/jekyll-theme-chirpy
|
2019-09-30 15:38:41 +03:00
|
|
|
|
# © 2019 Cotes Chung
|
|
|
|
|
# Published under MIT License
|
|
|
|
|
|
2020-01-01 20:21:43 +03:00
|
|
|
|
set -eu
|
|
|
|
|
|
2020-08-30 09:49:36 +03:00
|
|
|
|
WORK_DIR="$(dirname "$(dirname "$(realpath "$0")")")"
|
2019-09-30 15:38:41 +03:00
|
|
|
|
|
2020-10-09 18:46:28 +03:00
|
|
|
|
CONTAINER="${WORK_DIR}/.container"
|
2019-11-05 12:18:31 +03:00
|
|
|
|
SYNC_TOOL=_scripts/sh/sync_monitor.sh
|
|
|
|
|
|
2020-10-09 18:46:28 +03:00
|
|
|
|
cmd="bundle exec jekyll s"
|
2020-10-10 09:06:16 +03:00
|
|
|
|
JEKYLL_DOCKER_HOME="/srv/jekyll"
|
2020-10-09 18:46:28 +03:00
|
|
|
|
|
2019-11-18 17:01:59 +03:00
|
|
|
|
realtime=false
|
2020-10-08 11:00:08 +03:00
|
|
|
|
docker=false
|
2019-11-05 12:18:31 +03:00
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
|
_help() {
|
2019-11-05 12:18:31 +03:00
|
|
|
|
echo "Usage:"
|
|
|
|
|
echo
|
|
|
|
|
echo " bash run.sh [options]"
|
|
|
|
|
echo
|
|
|
|
|
echo "Options:"
|
|
|
|
|
echo " -H, --host <HOST> Host to bind to"
|
|
|
|
|
echo " -P, --port <PORT> Port to listen on"
|
|
|
|
|
echo " -b, --baseurl <URL> The site relative url that start with slash, e.g. '/project'"
|
|
|
|
|
echo " -h, --help Print the help information"
|
|
|
|
|
echo " -t, --trace Show the full backtrace when an error occurs"
|
2019-11-18 17:01:59 +03:00
|
|
|
|
echo " -r, --realtime Make the modified content updated in real time"
|
2020-10-08 11:00:08 +03:00
|
|
|
|
echo " --docker Run within docker"
|
2019-09-30 15:38:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
|
_cleanup() {
|
2020-10-09 18:46:28 +03:00
|
|
|
|
rm -rf "$CONTAINER"
|
2019-11-05 12:18:31 +03:00
|
|
|
|
ps aux | grep fswatch | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1
|
2019-09-30 15:38:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-09 18:46:28 +03:00
|
|
|
|
_setup_docker() {
|
|
|
|
|
# docker image `jekyll/jekyll` based on Alpine Linux
|
|
|
|
|
echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
|
2020-10-10 09:06:16 +03:00
|
|
|
|
## CN Apline sources mirror
|
|
|
|
|
# sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
|
2020-10-09 18:46:28 +03:00
|
|
|
|
apk update
|
|
|
|
|
apk add yq
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
|
_init() {
|
2020-10-10 09:06:16 +03:00
|
|
|
|
cd "$WORK_DIR"
|
|
|
|
|
|
2020-10-08 11:00:08 +03:00
|
|
|
|
if [[ -f Gemfile.lock ]]; then
|
|
|
|
|
rm -f Gemfile.lock
|
|
|
|
|
fi
|
2019-09-30 15:38:41 +03:00
|
|
|
|
|
2020-10-09 18:46:28 +03:00
|
|
|
|
if [[ -d $CONTAINER ]]; then
|
|
|
|
|
rm -rf "$CONTAINER"
|
2019-09-30 15:38:41 +03:00
|
|
|
|
fi
|
|
|
|
|
|
2020-10-10 09:06:16 +03:00
|
|
|
|
mkdir "$CONTAINER"
|
|
|
|
|
cp -r ./* "$CONTAINER"
|
|
|
|
|
cp -r ./.git "$CONTAINER"
|
|
|
|
|
|
|
|
|
|
if $docker; then
|
|
|
|
|
local _image_user=$(stat -c "%U" "$JEKYLL_DOCKER_HOME"/.)
|
|
|
|
|
|
|
|
|
|
if [[ $_image_user != $(whoami) ]]; then
|
|
|
|
|
# under Docker for Linux
|
|
|
|
|
chown -R "$(stat -c "%U:%G" "$JEKYLL_DOCKER_HOME"/.)" "$CONTAINER"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
fi
|
2019-09-30 15:38:41 +03:00
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
|
trap _cleanup INT
|
2019-09-30 15:38:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
|
_check_unset() {
|
2019-11-05 12:18:31 +03:00
|
|
|
|
if [[ -z ${1:+unset} ]]; then
|
2020-04-09 22:15:51 +03:00
|
|
|
|
_help
|
2019-09-30 15:38:41 +03:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
|
_check_command() {
|
2020-08-19 12:05:58 +03:00
|
|
|
|
if [[ -z $(command -v "$1") ]]; then
|
2019-11-18 17:01:59 +03:00
|
|
|
|
echo "Error: command '$1' not found !"
|
|
|
|
|
echo "Hint: Get '$1' on <$2>"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-09 18:46:28 +03:00
|
|
|
|
_run() {
|
|
|
|
|
cd "$CONTAINER"
|
2020-04-09 22:15:51 +03:00
|
|
|
|
bash _scripts/sh/create_pages.sh
|
|
|
|
|
bash _scripts/sh/dump_lastmod.sh
|
2019-11-18 17:01:59 +03:00
|
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
|
if $realtime; then
|
2020-09-14 21:06:55 +03:00
|
|
|
|
|
|
|
|
|
exclude_regex="\/\..*"
|
|
|
|
|
|
|
|
|
|
if [[ $OSTYPE == "darwin"* ]]; then
|
|
|
|
|
exclude_regex="/\..*" # darwin gcc treat regex '/' as character '/'
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
fswatch -e "$exclude_regex" -0 -r \
|
|
|
|
|
--event Created --event Removed \
|
|
|
|
|
--event Updated --event Renamed \
|
|
|
|
|
--event MovedFrom --event MovedTo \
|
|
|
|
|
"$WORK_DIR" | xargs -0 -I {} bash "./${SYNC_TOOL}" {} "$WORK_DIR" . &
|
2019-11-18 17:01:59 +03:00
|
|
|
|
fi
|
2019-11-05 12:18:31 +03:00
|
|
|
|
|
2020-10-09 18:46:28 +03:00
|
|
|
|
if $docker; then
|
2020-10-08 11:00:08 +03:00
|
|
|
|
cmd+=" -H 0.0.0.0"
|
2020-10-09 18:46:28 +03:00
|
|
|
|
else
|
|
|
|
|
cmd+=" -l -o"
|
2020-10-08 11:00:08 +03:00
|
|
|
|
fi
|
|
|
|
|
|
2019-11-05 12:18:31 +03:00
|
|
|
|
echo "\$ $cmd"
|
2020-08-19 12:05:58 +03:00
|
|
|
|
eval "$cmd"
|
2019-11-05 12:18:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-09 18:46:28 +03:00
|
|
|
|
main() {
|
|
|
|
|
if $docker; then
|
|
|
|
|
_setup_docker
|
|
|
|
|
fi
|
|
|
|
|
|
2020-10-10 09:06:16 +03:00
|
|
|
|
_init
|
2020-10-09 18:46:28 +03:00
|
|
|
|
_run
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
|
while (($#)); do
|
2019-09-30 15:38:41 +03:00
|
|
|
|
opt="$1"
|
|
|
|
|
case $opt in
|
2020-09-01 09:54:29 +03:00
|
|
|
|
-H | --host)
|
2020-08-19 12:05:58 +03:00
|
|
|
|
_check_unset "$2"
|
2019-09-30 15:38:41 +03:00
|
|
|
|
cmd+=" -H $2"
|
|
|
|
|
shift # past argument
|
|
|
|
|
shift # past value
|
|
|
|
|
;;
|
2020-09-01 09:54:29 +03:00
|
|
|
|
-P | --port)
|
2020-08-19 12:05:58 +03:00
|
|
|
|
_check_unset "$2"
|
2019-09-30 15:38:41 +03:00
|
|
|
|
cmd+=" -P $2"
|
|
|
|
|
shift
|
|
|
|
|
shift
|
|
|
|
|
;;
|
2020-09-01 09:54:29 +03:00
|
|
|
|
-b | --baseurl)
|
2020-08-19 12:05:58 +03:00
|
|
|
|
_check_unset "$2"
|
2020-09-14 21:06:55 +03:00
|
|
|
|
if [[ $2 == \/* ]]; then
|
2019-09-30 15:38:41 +03:00
|
|
|
|
cmd+=" -b $2"
|
|
|
|
|
else
|
2020-04-09 22:15:51 +03:00
|
|
|
|
_help
|
2019-09-30 15:38:41 +03:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
shift
|
|
|
|
|
shift
|
|
|
|
|
;;
|
2020-09-01 09:54:29 +03:00
|
|
|
|
-t | --trace)
|
2019-11-05 12:18:31 +03:00
|
|
|
|
cmd+=" -t"
|
|
|
|
|
shift
|
|
|
|
|
;;
|
2020-09-01 09:54:29 +03:00
|
|
|
|
-r | --realtime)
|
2020-08-19 12:05:58 +03:00
|
|
|
|
_check_command fswatch "http://emcrisostomo.github.io/fswatch/"
|
2019-11-18 17:01:59 +03:00
|
|
|
|
realtime=true
|
|
|
|
|
shift
|
|
|
|
|
;;
|
2020-10-08 11:00:08 +03:00
|
|
|
|
--docker)
|
|
|
|
|
docker=true
|
|
|
|
|
shift
|
|
|
|
|
;;
|
2020-09-01 09:54:29 +03:00
|
|
|
|
-h | --help)
|
2020-04-09 22:15:51 +03:00
|
|
|
|
_help
|
2019-09-30 15:38:41 +03:00
|
|
|
|
exit 0
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
# unknown option
|
2020-04-09 22:15:51 +03:00
|
|
|
|
_help
|
2019-09-30 15:38:41 +03:00
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
2019-11-05 12:18:31 +03:00
|
|
|
|
main
|