Skip to content

Commit ef9a45d

Browse files
committed
Added runtime sync monitor.
1 parent b07589e commit ef9a45d

2 files changed

Lines changed: 81 additions & 26 deletions

File tree

_scripts/sh/sync_monitor.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
# Files sync monitoer
4+
#
5+
# © 2019 Cotes Chung
6+
# MIT Licensed
7+
8+
# $1 -> the origin filen with absolute path.
9+
# $2 -> the origin sync directory
10+
# $3 -> the destination sync direcotry
11+
12+
# Pass the system temp file
13+
if [[ ! -f $1 ]]; then
14+
exit 0
15+
fi
16+
17+
src_dir=`dirname $(realpath $1)`
18+
19+
dir_prefix="$(realpath $2)/"
20+
21+
related_dir="${src_dir:${#dir_prefix}}"
22+
23+
24+
dest="$(realpath $3)/${related_dir}"
25+
26+
if [[ ! -d "$dest" ]]; then
27+
mkdir -p "$dest"
28+
fi
29+
30+
if [[ -f "$1" ]]; then
31+
cp $1 $dest
32+
fi
33+
34+
if [[ $related_dir == "_posts" ]]; then
35+
python $3/_scripts/py/pages_generator.py
36+
fi

run.sh

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,77 @@
11
#!/bin/bash
2-
#
2+
33
# Run jekyll site at http://127.0.0.1:4000
4+
#
5+
# Requirement:
6+
# fswatch › http://emcrisostomo.github.io/fswatch/
7+
#
48
# © 2019 Cotes Chung
59
# Published under MIT License
610

711

12+
WORK_DIR=$PWD
13+
CONTAINER=.container
14+
SYNC_TOOL=_scripts/sh/sync_monitor.sh
15+
16+
cmd="bundle exec jekyll s"
17+
818
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+
1830
}
1931

2032

2133
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
2437
}
2538

2639

2740
init() {
28-
2941
set -eu
3042

31-
if [[ -d .container ]]; then
32-
rm -rf .container
43+
if [[ -d $CONTAINER ]]; then
44+
rm -rf $CONTAINER
3345
fi
3446

3547
temp=$(mktemp -d)
3648
cp -r * $temp
3749
cp -r .git $temp
38-
mv $temp .container
50+
mv $temp $CONTAINER
3951

4052
trap cleanup INT
4153
}
4254

4355

4456
check_unset() {
45-
if [[ -z ${1:+unset} ]]
46-
then
57+
if [[ -z ${1:+unset} ]]; then
4758
help
4859
exit 1
4960
fi
5061
}
5162

5263

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+
5475

5576
while (( $# ))
5677
do
@@ -80,6 +101,10 @@ do
80101
shift
81102
shift
82103
;;
104+
-t|--trace)
105+
cmd+=" -t"
106+
shift
107+
;;
83108
-h|--help)
84109
help
85110
exit 0
@@ -92,10 +117,4 @@ do
92117
esac
93118
done
94119

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

Comments
 (0)