Skip to content

Commit 0b7f8ff

Browse files
ahmetbknative-prow-robot
authored andcommitted
hack: update ref docs gen script to cleanup tmp dirs (#1001)
Adding an EXIT trap to cleanup the /tmp directories containing 1. cloned knative repos (can be huge, especially going forward) 2. `gen-crd-api-reference-docs` tool's repo + built binary Fixes #1000. Signed-off-by: Ahmet Alp Balkan <[email protected]>
1 parent 3a02419 commit 0b7f8ff

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

hack/gen-api-reference-docs.sh

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ KNATIVE_EVENTING_SOURCES_REPO="github.com/knative/eventing-sources"
4141
KNATIVE_EVENTING_SOURCES_COMMIT="${KNATIVE_EVENTING_SOURCES_COMMIT:?specify the \$KNATIVE_EVENTING_SOURCES_COMMIT variable}"
4242
KNATIVE_EVENTING_SOURCES_OUT_FILE="eventing/eventing-sources.md"
4343

44+
cleanup_refdocs_root=
45+
cleanup_repo_clone_root=
46+
trap cleanup EXIT
47+
4448
log() {
4549
echo "$@" >&2
4650
}
@@ -89,21 +93,32 @@ clone_at_commit() {
8993
}
9094

9195
gen_refdocs() {
92-
local refdocs_bin gopath out_file repo_root
96+
local refdocs_bin gopath out_dir out_file repo_root
9397
refdocs_bin="$1"
9498
gopath="$2"
95-
out_file="$3"
96-
repo_root="$4"
99+
out_dir="$3"
100+
out_file="$4"
101+
repo_root="$5"
97102

98103
(
99104
cd "${repo_root}"
100105
env GOPATH="${gopath}" "${refdocs_bin}" \
101-
-out-file "${gopath}/out/${out_file}" \
106+
-out-file "${out_dir}/${out_file}" \
102107
-api-dir "./pkg/apis" \
103108
-config "${SCRIPTDIR}/reference-docs-gen-config.json"
104109
)
105110
}
106111

112+
cleanup() {
113+
if [ -d "${cleanup_refdocs_root}" ]; then
114+
echo "Cleaning up tmp directory: ${cleanup_refdocs_root}"
115+
rm -rf -- "${cleanup_refdocs_root}"
116+
fi
117+
if [ -d "${cleanup_repo_clone_root}" ]; then
118+
echo "Cleaning up tmp directory: ${cleanup_repo_clone_root}"
119+
rm -rf -- "${cleanup_repo_clone_root}"
120+
fi
121+
}
107122

108123
main() {
109124
if [[ -n "${GOPATH:-}" ]]; then
@@ -119,53 +134,56 @@ main() {
119134
# install and place the refdocs tool
120135
local refdocs_bin refdocs_bin_expected refdocs_dir
121136
refdocs_dir="$(mktemp -d)"
122-
refdocs_bin="${refdocs_dir}/refdocs"
137+
cleanup_refdocs_root="${refdocs_dir}"
123138
# clone repo for ./templates
124139
git clone --quiet --depth=1 "${REFDOCS_REPO}" "${refdocs_dir}"
125140
# install bin
126141
install_go_bin "${REFDOCS_PKG}@${REFDOCS_VER}"
127142
# move bin to final location
143+
refdocs_bin="${refdocs_dir}/refdocs"
128144
refdocs_bin_expected="$(go env GOPATH)/bin/$(basename ${REFDOCS_PKG})"
129145
mv "${refdocs_bin_expected}" "${refdocs_bin}"
130146
[[ ! -f "${refdocs_bin}" ]] && fail "refdocs failed to install"
131147

132-
local clone_root
148+
local clone_root out_dir
133149
clone_root="$(mktemp -d)"
150+
cleanup_repo_clone_root="${clone_root}"
151+
out_dir="$(mktemp -d)"
134152

135153
local knative_serving_root
136154
knative_serving_root="${clone_root}/src/${KNATIVE_SERVING_REPO}"
137155
clone_at_commit "https://${KNATIVE_SERVING_REPO}.git" "${KNATIVE_SERVING_COMMIT}" \
138156
"${knative_serving_root}"
139-
gen_refdocs "${refdocs_bin}" "${clone_root}" "${KNATIVE_SERVING_OUT_FILE}" \
140-
"${knative_serving_root}"
157+
gen_refdocs "${refdocs_bin}" "${clone_root}" "${out_dir}" \
158+
"${KNATIVE_SERVING_OUT_FILE}" "${knative_serving_root}"
141159

142160
local knative_build_root
143161
knative_build_root="${clone_root}/src/${KNATIVE_BUILD_REPO}"
144162
clone_at_commit "https://${KNATIVE_BUILD_REPO}.git" "${KNATIVE_BUILD_COMMIT}" \
145163
"${knative_build_root}"
146-
gen_refdocs "${refdocs_bin}" "${clone_root}" "${KNATIVE_BUILD_OUT_FILE}" \
147-
"${knative_build_root}"
164+
gen_refdocs "${refdocs_bin}" "${clone_root}" "${out_dir}" \
165+
"${KNATIVE_BUILD_OUT_FILE}" "${knative_build_root}"
148166

149167
local knative_eventing_root
150168
knative_eventing_root="${clone_root}/src/${KNATIVE_EVENTING_REPO}"
151169
clone_at_commit "https://${KNATIVE_EVENTING_REPO}.git" "${KNATIVE_EVENTING_COMMIT}" \
152170
"${knative_eventing_root}"
153-
gen_refdocs "${refdocs_bin}" "${clone_root}" "${KNATIVE_EVENTING_OUT_FILE}" \
154-
"${knative_eventing_root}"
171+
gen_refdocs "${refdocs_bin}" "${clone_root}" "${out_dir}" \
172+
"${KNATIVE_EVENTING_OUT_FILE}" "${knative_eventing_root}"
155173

156174
local knative_eventing_sources_root
157175
knative_eventing_sources_root="${clone_root}/src/${KNATIVE_EVENTING_SOURCES_REPO}"
158176
clone_at_commit "https://${KNATIVE_EVENTING_SOURCES_REPO}.git" "${KNATIVE_EVENTING_SOURCES_COMMIT}" \
159177
"${knative_eventing_sources_root}"
160-
gen_refdocs "${refdocs_bin}" "${clone_root}" "${KNATIVE_EVENTING_SOURCES_OUT_FILE}" \
161-
"${knative_eventing_sources_root}"
178+
gen_refdocs "${refdocs_bin}" "${clone_root}" "${out_dir}" \
179+
"${KNATIVE_EVENTING_SOURCES_OUT_FILE}" "${knative_eventing_sources_root}"
162180

163-
log "Generated files written to ${clone_root}/out/."
181+
log "SUCCESS: Generated docs written to ${out_dir}/."
164182
log "Copy the files in reference/ directory to knative/docs."
165-
if command -v open >/dev/null; then
166-
open "${clone_root}/out/"
167-
elif command -v xdg-open >/dev/null; then
168-
xdg-open "${clone_root}/out/"
183+
if command -v xdg-open >/dev/null; then
184+
xdg-open "${out_dir}/"
185+
elif command -v open >/dev/null; then
186+
open "${out_dir}/"
169187
fi
170188
}
171189

0 commit comments

Comments
 (0)