-
Notifications
You must be signed in to change notification settings - Fork 3
/
configure
executable file
·336 lines (278 loc) · 8.33 KB
/
configure
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/bash
shopt -s nocasematch
declare OS="$(uname)"
declare CWD="$(pwd)"
declare DEBUG="0"
declare SED_REGEX_FLAG="-r"
## output dot width
declare -i DOTWIDTH=50
## Container version
declare -i VERSION_MAJOR=1
declare -i VERSION_MINOR=0
declare -i VERSION_PATCH=0
declare -i VERSION_REVISION=1
## Node.js docker container tag version to use
declare NODE_VERSION='latest'
## Server port to listen on
declare SERVER_PORT=3000
## docsify conf
declare DOCSIFY_VERSION="${DOCSIFY_VERSION:-latest}"
declare DOCSIFY_CLI_MODULE_NAME="${DOCSIFY_CLI_MODULE_NAME:-docsify-cli}"
declare DOCSIFY_CLI_NAME="${DOCSIFY_CLI_NAME:-docsify}"
declare DOCSIFY_CLI_COMMAND="${DOCSIFY_CLI_COMMAND:-serve}"
declare DOCSIFY_CLI_DEFAULT_PATH="${DOCSIFY_CLI_DEFAULT_PATH:-.}"
## docker conf
declare DOCKER="${DOCKER:-$(which docker)}"
declare DOCKER_BASE="node"
declare DOCKER_FLAGS+=' --label docker_docsify_build_timestamp="$(date +%s)" '
declare DOCKER_FILE="${DOCKER_FILE:-Dockerfile}"
declare DOCKER_TAG=${DOCKER_TAG:-littlstar/docsify}
## Script name
declare SELF="$(basename $0)"
## Project version
declare CONTAINER_VERSION="$(./version)"
## Required system commands
declare -a COMMAND_DEPENDENCIES=( "$DOCKER" make )
## Optional user commands
declare -a OPTIONAL_COMMAND_DEPENDENCIES=( )
case $OS in
linux) ;;
darwin) SED_REGEX_FLAG="-E" ;;
esac
## Flags passed to this script
declare CONFIGURE_FLAGS=""
## Configurable environment variables
declare PREFIX="${PREFIX:-/usr/local/docsify}"
## Template variables map with the following stride:
## <key, value, description>
declare -a TEMPLATE_VARS=(
OS "Operating System"
DEBUG "An optional DEBUG environment variable"
PREFIX "Default installation prefix"
SERVER_PORT "The server port to listen on"
NODE_VERSION "The base Node.js container version"
DOCKER "Docker binary path"
DOCKER_TAG "Docker build tag"
DOCKER_BASE "Docker base image"
DOCKER_FILE "Dockerfile path"
DOCKER_FLAGS "Docker build flags"
VERSION_MAJOR "The major version of the docker container"
VERSION_MINOR "The minor version of the docker container"
VERSION_PATCH "The patch version of the docker container"
VERSION_REVISION "The revision version of the docker container"
DOCSIFY_VERSION "The docsify-cli version to use"
DOCSIFY_CLI_NAME "The command line program name to invoke"
DOCSIFY_CLI_COMMAND "The docsify-cli command to use"
DOCSIFY_CLI_MODULE_NAME "The module name to install from npm"
DOCSIFY_CLI_DEFAULT_PATH "The default path for the docsify-cli command"
)
## White space trim helper
function trim {
echo -n "$@" | sed "$SED_REGEX_FLAG" 's/^\s*(\S+(\s+\S+)*)\s*$/\1/'
return $?
}
## Gets or sets a template var
function var {
let local i=0
local key=$1
local value=$2
for (( i = 0; i < ${#TEMPLATE_VARS[@]}; i += 2 )); do
local name=${TEMPLATE_VARS[$i]}
if [ $name = $key ]; then
if (( 2 == $# )); then
eval "$name=$value"
info "$name = $value"
else
echo ${!name}
fi
return 0
fi
done
return 1
}
##
# Outputs prefixed info to stdout
function info {
printf "%s: %s" $SELF "$@"
echo
return 0
}
##
# Oututs ok and returns a 0 status code
function ok {
info "ok" && true && return $?
}
## Outputs a message to stderr
## and exits with status 1 immediately
function fatal {
info "fatal: $@" >&2
exit 1
}
## Outputs a warning message to stderr
function warn {
info "warn: $@" >&2
return 0
}
## Just print yes
function yes {
printf " yes"
return 0
}
## Just print no
function no {
printf " no"
return 0
}
## Print dots (...)
function dots {
let local count=${1:-20}
local i=0
for (( i = 0; i < count; ++i )); do
printf "."
done
return 0
}
## Check if a dependency command exists with
## human readable output
function check_command {
let local rc=0
local cmd="$1"
if [ -z $cmd ]; then return 1; fi
printf "Checking for $cmd " && dots $(expr $DOTWIDTH - ${#cmd} )
if [ -z $(which $cmd) ]; then
no; rc=1
else
yes; rc=0
fi
echo
return $rc
}
## Generates a output from a named template
function template {
local input="$1"
local output
local buffer
local i=0
if ! [[ "$input" =~ ".in" ]]; then
input+=".in"
fi
if test -f "$input"; then
output="${input/.in/}"
## clean up existing output file
if test -f "$output"; then
rm -f "$output";
fi
info "Generating file \`$output'"
cmd="$(echo "sed -e $(eval echo $( \
for (( i = 0; i < ${#TEMPLATE_VARS[@]}; i += 2 )); do \
local name="${TEMPLATE_VARS[$i]}"; \
local value; eval 'value=${'$name'[@]}'; \
local values="${value[@]}"; \
local escaped="${values//\//\\/}"; \
echo "\-e \'\"s/@$name@/$escaped/g\"\'"; \
done)) "$input" > "$output"")"
## exec
eval $cmd
fi
}
## Configures library build files
function configure {
let local i=0
info "$(uname -a)"
info "Configuring docker-docsify Dockerfile."
case "$OS" in
Linux|Unix|GNU|*BSD|Darwin|Win32) ;;
*) fatal "Unsupported operating system."
esac
if (( ${#COMMAND_DEPENDENCIES[@]} > 0 )); then
echo
info "Checking required command dependencies"
for (( i = 0; i < ${#COMMAND_DEPENDENCIES[@]}; i++ )); do
local cmd="${COMMAND_DEPENDENCIES[$i]}"
if ! check_command $cmd; then
fatal "Missing required command $cmd"
fi
done
fi
info "Generating template files"
## TODO(werle) - Check nested *.in with "find . -name *.in" ?
for file in *.in; do
template $file
done
return 0
}
## Outputs command help
function usage {
let local i=0
cat <<HELP
usage: $SELF [-hV]
options:
-h, --help Print this message
-V, --version Output version
--debug=[DEBUG] Configures the DEBUG environment variable for the Docker container
--prefix=PREFIX Docsify docker working directory path (default: $PREFIX)
--port=PORT Server port exposed by Docker and the Docsify server should listen on (default: $SERVER_PORT)
--ssr Enable SSR server
--docsify-default-path=PATH The default docsify path when starting the server (defualt: $DOCSIFY_CLI_DEFAULT_PATH)
--docsify-version=VERSION Docsify (CLI) version to use (default: $DOCSIFY_VERSION)
--docsify-module=NAME The module name to install from name (default: $DOCSIFY_CLI_MODULE_NAME)
--docker-base=IMAGE The base docker image to base the docker build from (default: $DOCKER_BASE)
--docker-bin=PATH The path to the docker binary (default: $DOCKER)
--docker-tag=TAG The Docker tag to use when building the container (default: $DOCKER_TAG)
--docker-file=PATH The Dockerfile path (default: $DOCKER_FILE)
--docker-flags=" ...FLAGS" A space separated list of flags to pass to \`docker build'
--node-version=VERSION Node.js version to use (default: $NODE_VERSION)
HELP
return 0
}
## Scripts entry
function main {
local arg value
for arg in $@; do
## parse opt value
case "$arg" in
*=false) value=0 ;;
*=true) value=1 ;;
*=?*) value="$(expr -- "$arg" : '[^=]*=\(.*\)')" ;;
*=) value= ;;
*) value=1 ;;
esac
case $arg in
-h|--help) usage && return 0;;
-V|--version) echo docker-docsify@$CONTAINER_VERSION && return 0;;
## Output directories
--prefix=?*) PREFIX="$value" ;;
--docsify-version=?*) DOCSIFY_VERSION="$value" ;;
--node-version=?*) NODE_VERSION="$value" ;;
--port=?*) SERVER_PORT="$value" ;;
--ssr) DOCSIFY_CLI_COMMAND="start" ;;
## debug configuration
--debug|--debug=?*)
if (( 1 == value )); then
DEBUG="$DOCSIFY_CLI_NAME*"
else
DEBUG="${value:-$DOCSIFY_CLI_NAME:*}"
fi
;;
esac
done
CONFIGURE_FLAGS+="--prefix=$PREFIX "
CONFIGURE_FLAGS+="--port=$SERVER_PORT "
CONFIGURE_FLAGS+="--node-version=$NODE_VERSION "
CONFIGURE_FLAGS+="--docsify-version=$DOCSIFY_VERSION "
if [ "$DOCSIFY_CLI_COMMAND" == "start" ]; then
CONFIGURE_FLAGS+="--ssr=true "
else
CONFIGURE_FLAGS+="--ssr=false "
fi
if ! [ -z "$DEBUG" ]; then
CONFIGURE_FLAGS+="--debug=true"
else
CONFIGURE_FLAGS+="--debug=false"
fi
info "flags: $CONFIGURE_FLAGS"
configure
return $?
}
## init
(main $@) && exit $?