web/run.sh

139 lines
2.3 KiB
Bash
Raw Normal View History

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:
# Option '-r, --realtime' needs fswatch http://emcrisostomo.github.io/fswatch/
2019-11-05 12:18:31 +03:00
#
2019-09-30 15:38:41 +03:00
# © 2019 Cotes Chung
# Published under MIT License
2019-11-05 12:18:31 +03:00
WORK_DIR=$PWD
CONTAINER=.container
SYNC_TOOL=_scripts/sh/sync_monitor.sh
cmd="bundle exec jekyll s"
realtime=false
2019-11-05 12:18:31 +03:00
2019-09-30 15:38:41 +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"
echo " -r, --realtime Make the modified content updated in real time"
2019-09-30 15:38:41 +03:00
}
cleanup() {
2019-11-05 12:18:31 +03:00
cd $WORK_DIR
rm -rf $CONTAINER
ps aux | grep fswatch | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1
2019-09-30 15:38:41 +03:00
}
init() {
set -eu
2019-11-05 12:18:31 +03:00
if [[ -d $CONTAINER ]]; then
rm -rf $CONTAINER
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
2019-11-05 12:18:31 +03:00
mv $temp $CONTAINER
2019-09-30 15:38:41 +03:00
trap cleanup INT
}
check_unset() {
2019-11-05 12:18:31 +03:00
if [[ -z ${1:+unset} ]]; then
2019-09-30 15:38:41 +03:00
help
exit 1
fi
}
check_command() {
if [[ -z $(command -v $1) ]]; then
echo "Error: command '$1' not found !"
echo "Hint: Get '$1' on <$2>"
exit 1
fi
}
2019-11-05 12:18:31 +03:00
main() {
init
cd $CONTAINER
python _scripts/py/init_all.py
if [[ $realtime = true ]]; then
fswatch -0 -e "\\$CONTAINER" -e "\.git" ${WORK_DIR} | xargs -0 -I {} bash ./${SYNC_TOOL} {} $WORK_DIR . &
fi
2019-11-05 12:18:31 +03:00
echo "\$ $cmd"
eval $cmd
}
2019-09-30 15:38:41 +03:00
while (( $# ))
do
opt="$1"
case $opt in
-H|--host)
check_unset $2
cmd+=" -H $2"
shift # past argument
shift # past value
;;
-P|--port)
check_unset $2
cmd+=" -P $2"
shift
shift
;;
-b|--baseurl)
check_unset $2
if [[ $2 == \/* ]]
then
cmd+=" -b $2"
else
help
exit 1
fi
shift
shift
;;
2019-11-05 12:18:31 +03:00
-t|--trace)
cmd+=" -t"
shift
;;
-r|--realtime)
check_command fswatch 'http://emcrisostomo.github.io/fswatch/'
realtime=true
shift
;;
2019-09-30 15:38:41 +03:00
-h|--help)
help
exit 0
;;
*)
# unknown option
help
exit 1
;;
esac
done
2019-11-05 12:18:31 +03:00
main