-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathgenerate-command-docs
executable file
·98 lines (85 loc) · 2.23 KB
/
generate-command-docs
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
#!/usr/bin/env bash
set -euo pipefail
BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && cd .. && pwd)"
DOCS_DIR="${BASE_DIR}/docs"
_arg_jar=""
die()
{
local _ret=$2
test -n "$_ret" || _ret=1
test "$_PRINT_HELP" = yes && print_help >&2
echo "ERROR: $1" >&2
exit ${_ret}
}
print_help()
{
printf '%s\n' "Generate asciidoc files for commands"
printf 'Usage: %s [--jar <arg>] [-h|--help]\n' "$0"
printf '\t%s\n' "--jar: The path to cli jar"
printf '\t%s\n' "-h, --help: Prints help"
}
check_prereq()
{
if ! [ -x "$(command -v $1)" ]; then
echo "This script requires '$1' to be installed."
exit 1
fi
}
parse_commandline()
{
while test $# -gt 0
do
_key="$1"
case "$_key" in
# Since we know that we got the long or short option,
# we just reach out for the next argument to get the value.
--jar)
test $# -lt 2 && _PRINT_HELP=yes die "Missing value for the argument '$_key'." 1
_arg_jar="$2"
shift
;;
# The help argurment doesn't accept a value,
# we expect the --help or -h, so we watch for them.
-h|--help)
print_help
exit 0
;;
*)
_PRINT_HELP=yes die "Got an unexpected argument '$1'" 1
;;
esac
shift
done
}
get_abs_filename() {
echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
}
generate() {
jarabsfile=$(get_abs_filename $_arg_jar)
pushd $DOCS_DIR > /dev/null
echo ".Commands" > modules/ROOT/nav-commands.adoc
mapfile arr < <(SPRING_SHELL_COMMAND_HELP_COMMANDS_TEMPLATE=file:help-commands-adoc.stg java -jar $jarabsfile help 2>&1)
for path in "${arr[@]}"; do
IFS=':' read -ra FIELDS <<< "$path"
echo ${FIELDS[0]}
for F in "${FIELDS[@]:1}"; do
echo $F
NAME="${F// /-}"
SPRING_SHELL_COMMAND_HELP_COMMAND_TEMPLATE=file:help-command-adoc.stg java -jar $jarabsfile help $F &> modules/ROOT/pages/commands/$NAME.adoc
echo "** xref:commands/$NAME.adoc[$F]" >> modules/ROOT/nav-commands.adoc
done
done
popd > /dev/null
}
parse_commandline "$@"
check_prereq 'java'
if [[ "$_arg_jar" = "" ]]; then
echo "Jar path '$_arg_jar' is not set"
exit 1
fi
if [[ -f "$_arg_jar" ]]; then
generate
else
echo "Jar path '$_arg_jar' is not a file"
exit 1
fi