-
Notifications
You must be signed in to change notification settings - Fork 0
/
mansite
executable file
·184 lines (141 loc) · 3.04 KB
/
mansite
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/sh
# mansite
# shellcheck disable=SC2317
VERSION='2.0.5'
mansite_main() {
trap _exit INT TERM
E_ARGS=16
E_FAILED_CD=17
E_NO_MIDDLEMAN=18
E_EXISTS=19
SCRIPT_INV="$( basename "$0" )"
SCRIPT_PATH="$( readlink -f "$0" )"
SCRIPT_NAME="$( basename "$SCRIPT_PATH" )"
SCRIPT_DIR="$( dirname "$SCRIPT_PATH" )"
BUILD=0
while getopts bhv opt
do
case $opt in
b) BUILD=1 ;;
h) CMD=help ;;
v) CMD=version ;;
*) _invalid_opt ;;
esac
done
shift "$(( OPTIND - 1 ))"
[ $# -gt 0 ] && CMD="$1" && shift
[ -z "$CMD" ] && CMD="serve"
case "$CMD" in
mm) CMD=middleman ;;
esac
case "$CMD" in
build|deploy|help|middleman|new|serve|version) "mansite_$CMD" "$@" ;;
*) _invalid_cmd ;;
esac
_exit
}
mansite_help() {
cat <<USAGE
$SCRIPT_NAME $VERSION
USAGE: $SCRIPT_INV [<options>] <command> [<args>...]
Serve or build site using Middleman.
COMMANDS:
build [<env>]
Build site using Middleman. Defaults to production environment.
deploy [<env>]
Deploy site using Middleman. Defaults to production environment.
help
Show this help message.
middleman [<args>]
Run Middleman. Pass all arguments to it.
mm [<args>]
Alias for 'middleman'.
new <dir>
Create new mansite in <dir>.
serve [<args>]
Serve this site using Middleman. Pass arguments to it.
version
Show the version.
OPTIONS:
-b Build before deploy.
-h Show this help screen.
-v Show script name and version number.
USAGE
}
mansite_build() {
env_name="$1"
[ -z "$env_name" ] && env_name=production
rake "build:$env_name"
}
mansite_deploy() {
env_name="$1"
[ -z "$env_name" ] && env_name=production
[ "$BUILD" -ne 0 ] && ( mansite_build "$env_name" || return $? )
rake "deploy:$env_name"
}
mansite_middleman() {
_find_middleman_dir
[ "$( pwd )" != "$middleman_dir" ] && _echo "$middleman_dir"
cd "$middleman_dir" || exit "$E_FAILED_CD"
[ $# -gt 0 ] && bundle exec middleman "$@" && return
bundle exec middleman "$@"
}
mansite_new() {
[ $# -lt 1 ] && _fatal "$E_ARGS" "Must specify directory."
dir="$1"
[ -d "$dir" ] && _fatal "$E_EXISTS" "Directory already exists: $dir"
git clone https://github.com/remino/mansite "$dir"
cd "$dir" || _fatal "$E_FAILED_CD" "Failed to cd to: $dir"
[ -d .git ] && rm -rf .git
npm install
bundle install
git init
git add .
git commit -m "Initial commit."
}
mansite_serve() {
mansite_middleman "$@"
}
mansite_version() {
_echo "$SCRIPT_NAME $VERSION"
}
_echo() {
echo "$@"
}
_error() {
_echo "$@" >&2
}
_exit() {
exit_code=$?
exit $exit_code
}
_fatal() {
exit_code="$1"
shift
_error "$@"
exit "$exit_code"
}
_find_middleman_dir() {
for middleman_dir in "$( pwd )" "$SCRIPT_DIR"
do
while [ "$middleman_dir" != '/' ]
do
[ -f "$middleman_dir/config.rb" ] && return
middleman_dir="$( dirname "$middleman_dir" )"
done
done
_fatal "$E_NO_MIDDLEMAN" "Failed to find Middleman directory"
}
_invalid_cmd() {
_error "Invalid command: $CMD"
_echo
mansite_help
exit "$E_ARGS"
}
_invalid_opt() {
_error "Invalid option: $opt"
_echo
mansite_help
exit "$E_ARGS"
}
mansite_main "$@"