-
Notifications
You must be signed in to change notification settings - Fork 0
/
autodoc
executable file
·183 lines (146 loc) · 5.43 KB
/
autodoc
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
#!/bin/bash
set -e
DOCS_REPO=$1
DOCS_VERSION=$2
RED='\033[0;31m'
NC='\033[0m' # No Color
function echoerr() {
printf '%s%s%s\n' "$RED" "$@" "$NC" 1>&2;
}
function copy() {
printf 'cp %s\t→\t%s\n' "$1" "$2"
cp "$1" "$2"
}
INFINITY=9223372036854775807
COUNT_INDENTS=0
function count_indents_on_line() {
local file=$1
local lineno=$2
local line
line=$(sed -n "${lineno}p" "$file")
local trim="${line#*[^[:blank:]]}"
COUNT_INDENTS="$(( ${#line} - ${#trim} - 1 ))"
# empty lines count as "infinitely indented"
if [ "$COUNT_INDENTS" -lt 0 ]
then
COUNT_INDENTS="$INFINITY"
fi
}
function insert_yaml_file_into_nav_file() {
local new_nav_partial_file=$1
local docs_nav_file=$2
local first_line_text=$3
echo "Patching $docs_nav_file with $new_nav_partial_file ..."
# find first_line and last line of the current pdk index in docs_nav_file
# first line contains the content of first_line_text
local first_line
first_line=$(sed -n "$first_line_text" "$docs_nav_file")
# find last_line by looking at the indentation of all subsequent lines, looking at the indentation
# last line is the last one with more indentation that first_line
local max_line
max_line=$(wc -l < "$docs_nav_file" | bc)
count_indents_on_line "$docs_nav_file" "$first_line"
local initial_indent="$COUNT_INDENTS"
local current_indent="$(( initial_indent + 1 ))"
local current_line="$first_line"
while [ "$current_indent" -gt "$initial_indent" ] && [ "$current_line" -lt "$max_line" ]
do
current_line="$((current_line + 1))"
count_indents_on_line "$docs_nav_file" "$current_line"
current_indent="$COUNT_INDENTS"
done
local last_line="$(( current_line - 1 ))"
# insert_line is where we want to put the new pdk docs after deleting the old ones (one less than first)
local insert_line="$(( first_line - 1 ))"
# delete existing pdk index, insert new pdk nav file instead
# Passing an option to -i is required on macOS, but not required on Linux
# Commenting out to make the GitHub Actions build work
#sed -i '' -e "${first_line},${last_line}d" -e "${insert_line}r${new_nav_partial_file}" "$docs_nav_file"
# Recommended to `brew install gnu-sed && brew info gnu-sed` to override default sed on Mac
sed -i -e "${first_line},${last_line}d" -e "${insert_line}r${new_nav_partial_file}" "$docs_nav_file"
}
echo "Generating docs ..."
rm -rf ./autodoc/output
./autodoc/admin-api/generate.lua
./autodoc/cli/generate.lua
./autodoc/upgrading/generate.lua
./autodoc/pdk/generate.lua
exit_code=$?
if [[ $exit_code -ne 0 ]]
then
exit $exit_code
fi
if [ -z "$DOCS_REPO" ] || [ -z "$DOCS_VERSION" ]
then
echo
echo "No docs repo & version parameters found. For example, this:"
echo
echo " script/autodocs ../docs.konghq.com 2.1.x"
echo
echo "would install the files in the docs repo, located in ../docs.konghq.com,"
echo "and in the 2.1.x version"
echo
echo "Since no repo or version was specified, I won't attempt to copy the files, and exit successfully now"
echo
exit 0
fi
if [ -d "$DOCS_REPO" ]
then
echo
echo "docs repo: $DOCS_REPO"
echo "docs version: $DOCS_VERSION"
echo
else
echoerr
echoerr "Could not find the docs repo in $DOCS_REPO . Please clone it there so I can copy the autodocs files there"
echoerr "For example with:"
echoerr "cd .."
echoerr "git clone https://github.com/kong/docs.konghq.com"
echoerr "cd docs.konghq.com"
echoerr "git checkout -b docs/autodocs"
echoerr
exit 1
fi
if [ ! -d "$DOCS_REPO/autodoc-nav" ]
then
echoerr
echoerr "The folder $DOCS_REPO/autodoc-nav does not exist. Please create it in order to copy the autodocs there"
echoerr "Usually this is done by copying and renaming a previous version of the docs"
exit 2
fi
DOCS_APP="$DOCS_REPO/app/gateway/$DOCS_VERSION"
if [ ! -d "$DOCS_APP" ]
then
echoerr
echoerr "The app doc folder for the chosen version ($DOCS_APP) does not exist. Please create it in order to copy the autodocs there"
echoerr "Usually this is done by copying and renaming a previous version of the docs"
echoerr "The file $DOCS_REPO/app/_data/kong_versions.yml might need to be updated with the new version as well"
echoerr
exit 3
fi
DOCS_NAV="$DOCS_REPO/app/_data/docs_nav_gateway_$DOCS_VERSION.yml"
if [ ! -f "$DOCS_NAV" ]
then
echoerr
echoerr "The doc file $DOCS_NAV does not exist. Please create it in order to copy the autodocs there"
echoerr "Usually this is done by copying and renaming a previous version of the docs. Example:"
echoerr
echoerr "cp $DOCS_REPO/app/_data/docs_nav_**REPLACE_THIS**.yml $DOCS_NAV"
echoerr
exit 4
fi
copy autodoc/output/admin-api/admin-api.md "$DOCS_APP/admin-api/index.md"
copy autodoc/output/cli.md "$DOCS_APP/reference/cli.md"
rm -rf "$DOCS_APP/install-and-run/upgrade-oss.md"
copy autodoc/output/upgrading.md "$DOCS_APP/install-and-run/upgrade-oss.md"
rm -rf "$DOCS_APP/pdk/"
mkdir -p "$DOCS_APP/pdk"
for module in autodoc/output/pdk/*; do
copy "$module" "$DOCS_APP/pdk/"
done
insert_yaml_file_into_nav_file ./autodoc/output/nav/docs_nav.yml.admin-api.in \
"$DOCS_NAV" \
"/title: Admin API/="
insert_yaml_file_into_nav_file ./autodoc/output/_pdk_nav.yml \
"$DOCS_NAV" \
"/text: Plugin Development Kit/="