|
1 | 1 | #!/bin/bash |
2 | | -# |
| 2 | + |
3 | 3 | # Run jekyll site at http://127.0.0.1:4000 |
| 4 | +# |
| 5 | +# Requirement: |
| 6 | +# fswatch › http://emcrisostomo.github.io/fswatch/ |
| 7 | +# |
4 | 8 | # © 2019 Cotes Chung |
5 | 9 | # Published under MIT License |
6 | 10 |
|
7 | 11 |
|
| 12 | +WORK_DIR=$PWD |
| 13 | +CONTAINER=.container |
| 14 | +SYNC_TOOL=_scripts/sh/sync_monitor.sh |
| 15 | + |
| 16 | +cmd="bundle exec jekyll s" |
| 17 | + |
8 | 18 | help() { |
9 | | - echo "Usage:" |
10 | | - echo |
11 | | - echo " bash run.sh [options]" |
12 | | - echo |
13 | | - echo "Options:" |
14 | | - echo " -H, --host <HOST> Host to bind to" |
15 | | - echo " -P, --port <PORT> Port to listen on" |
16 | | - echo " -b, --baseurl <URL> The site relative url that start with slash, e.g. '/project'" |
17 | | - echo " -h, --help Print the help information" |
| 19 | + echo "Usage:" |
| 20 | + echo |
| 21 | + echo " bash run.sh [options]" |
| 22 | + echo |
| 23 | + echo "Options:" |
| 24 | + echo " -H, --host <HOST> Host to bind to" |
| 25 | + echo " -P, --port <PORT> Port to listen on" |
| 26 | + echo " -b, --baseurl <URL> The site relative url that start with slash, e.g. '/project'" |
| 27 | + echo " -h, --help Print the help information" |
| 28 | + echo " -t, --trace Show the full backtrace when an error occurs" |
| 29 | + |
18 | 30 | } |
19 | 31 |
|
20 | 32 |
|
21 | 33 | cleanup() { |
22 | | - cd ../ |
23 | | - rm -rf .container |
| 34 | + cd $WORK_DIR |
| 35 | + rm -rf $CONTAINER |
| 36 | + ps aux | grep fswatch | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1 |
24 | 37 | } |
25 | 38 |
|
26 | 39 |
|
27 | 40 | init() { |
28 | | - |
29 | 41 | set -eu |
30 | 42 |
|
31 | | - if [[ -d .container ]]; then |
32 | | - rm -rf .container |
| 43 | + if [[ -d $CONTAINER ]]; then |
| 44 | + rm -rf $CONTAINER |
33 | 45 | fi |
34 | 46 |
|
35 | 47 | temp=$(mktemp -d) |
36 | 48 | cp -r * $temp |
37 | 49 | cp -r .git $temp |
38 | | - mv $temp .container |
| 50 | + mv $temp $CONTAINER |
39 | 51 |
|
40 | 52 | trap cleanup INT |
41 | 53 | } |
42 | 54 |
|
43 | 55 |
|
44 | 56 | check_unset() { |
45 | | - if [[ -z ${1:+unset} ]] |
46 | | - then |
| 57 | + if [[ -z ${1:+unset} ]]; then |
47 | 58 | help |
48 | 59 | exit 1 |
49 | 60 | fi |
50 | 61 | } |
51 | 62 |
|
52 | 63 |
|
53 | | -cmd="bundle exec jekyll s" |
| 64 | +main() { |
| 65 | + init |
| 66 | + |
| 67 | + cd $CONTAINER |
| 68 | + python _scripts/py/init_all.py |
| 69 | + fswatch -0 -e "\\$CONTAINER" -e "\.git" ${WORK_DIR} | xargs -0 -I {} bash ./${SYNC_TOOL} {} $WORK_DIR . & |
| 70 | + |
| 71 | + echo "\$ $cmd" |
| 72 | + eval $cmd |
| 73 | +} |
| 74 | + |
54 | 75 |
|
55 | 76 | while (( $# )) |
56 | 77 | do |
|
80 | 101 | shift |
81 | 102 | shift |
82 | 103 | ;; |
| 104 | + -t|--trace) |
| 105 | + cmd+=" -t" |
| 106 | + shift |
| 107 | + ;; |
83 | 108 | -h|--help) |
84 | 109 | help |
85 | 110 | exit 0 |
|
92 | 117 | esac |
93 | 118 | done |
94 | 119 |
|
95 | | -init |
96 | | - |
97 | | -cd .container |
98 | | -python _scripts/py/init_all.py |
99 | | - |
100 | | -echo "\$ $cmd" |
101 | | -eval $cmd |
| 120 | +main |
0 commit comments