-
Notifications
You must be signed in to change notification settings - Fork 4
/
tpb.sh
executable file
·123 lines (103 loc) · 2.39 KB
/
tpb.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
#!/usr/bin/env bash
# ---------------------------
# helper functions
# ---------------------------
## detect if it is on which system, because inplace sed on MacOS requires backup extension
isMac() {
if [[ "$OSTYPE" == "darwin"* ]]; then
return 0
fi
return 1
}
## sed inplace for cross platform
xsed() {
# $1 : pattern
# $2 : filename
if isMac; then
sed -Ei '' "$1" "$2"
else
sed -Ei "$1" "$2"
fi
}
## check if imageMagick is installed
checkRequirement() {
if ! type ruby > /dev/null; then
echo "ruby not found, please install.. (rbenv recommended)"
return 1
fi
if ! type gem > /dev/null; then
echo "gem not found, please install.. (rbenv recommended)"
return 1
fi
return 0
}
## display help
displayHelp(){
echo "Usage: $0 [-i] [-d]"
echo
echo " Options:"
echo " -i, --install install dependencies with gem (gem install bundler) and bundler (bundle install)."
echo " -d, --debug serve jekyll locally using bundler."
echo
exit 1
}
# ---------------------------
# functions
# ---------------------------
## install dependencies
installDeps() {
if ! type bundle > /dev/null; then
echo -e ">> bundle not found, installing..\n"
gem install bundler
fi
echo -e ">> installing bundles..\n"
bundle install
}
## debug locally
debugLocally() {
local config_file
local fwp_pid
config_file="./_config.yml"
if type gp > /dev/null; then
echo -e ">> Running on Gitpod.. Forwarding port 4000 to 4001 to expose publicly..\n"
gp forward-port 4000 4001 &
# collect pid of forwarding port to kill later
fwp_pid="$!"
fi
# comment url line on start
xsed 's/^(url:.*)/\# \1/' $config_file
# serve jekyll locally
JEKYLL_ENV=production bundle exec jekyll serve
# uncomment url line on stop
xsed 's/^#\s*(url: https\:\/\/tupleblog.*)/\1/' $config_file
# if there is forward port, kill it
if [ ! -z "$fwp_pid" ]; then
echo -e "\n>> Killing pid: $fwp_pid.."
kill -9 "$fwp_pid"
fi
}
# ---------------------------
# main
# ---------------------------
main() {
# check requirement first
if ! checkRequirement; then exit 1; fi
case $1 in
'-i' | '--install' )
installDeps
exit
;;
'-d' | '--debug' )
debugLocally
exit
;;
'-t' | '--test' )
debugLocally
# displayHelp
;;
* )
displayHelp
;;
esac
}
main $@