forked from cotes2020/jekyll-theme-chirpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·138 lines (114 loc) · 2.34 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
# Run jekyll site at http://127.0.0.1:4000
#
# Requirement:
# Option '-r, --realtime' needs fswatch › http://emcrisostomo.github.io/fswatch/
#
# © 2019 Cotes Chung
# Published under MIT License
WORK_DIR=$PWD
CONTAINER=.container
SYNC_TOOL=_scripts/sh/sync_monitor.sh
cmd="bundle exec jekyll s"
realtime=false
help() {
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"
}
cleanup() {
cd $WORK_DIR
rm -rf $CONTAINER
ps aux | grep fswatch | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1
}
init() {
set -eu
if [[ -d $CONTAINER ]]; then
rm -rf $CONTAINER
fi
temp=$(mktemp -d)
cp -r * $temp
cp -r .git $temp
mv $temp $CONTAINER
trap cleanup INT
}
check_unset() {
if [[ -z ${1:+unset} ]]; then
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
}
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
echo "\$ $cmd"
eval $cmd
}
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
;;
-t|--trace)
cmd+=" -t"
shift
;;
-r|--realtime)
check_command fswatch 'http://emcrisostomo.github.io/fswatch/'
realtime=true
shift
;;
-h|--help)
help
exit 0
;;
*)
# unknown option
help
exit 1
;;
esac
done
main