Skip to content

Commit

Permalink
Better clean up of codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jul 4, 2024
1 parent 00b3533 commit efe1245
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
24 changes: 21 additions & 3 deletions codegen-library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ oldstate="$(set +o)"
set -Eeuo pipefail

export repodir kn_hack_dir kn_hack_library \
MODULE_NAME GOPATH GOBIN \
MODULE_NAME TMP_GOPATH GOPATH GOBIN \
CODEGEN_PKG KNATIVE_CODEGEN_PKG

kn_hack_dir="$(realpath "$(dirname "${BASH_SOURCE[0]:-$0}")")"
Expand Down Expand Up @@ -67,7 +67,8 @@ if ! KNATIVE_CODEGEN_PKG="${KNATIVE_CODEGEN_PKG:-"$(go-resolve-pkg-dir knative.d
fi

popd > /dev/null
GOPATH=$(go_mod_gopath_hack)
TMP_GOPATH=$(go_mod_gopath_hack)
GOPATH="${TMP_GOPATH}"
GOBIN="${GOPATH}/bin" # Set GOBIN explicitly as k8s-gen' are installed by go install.

if [[ -n "${CODEGEN_PKG}" ]] && ! [ -x "${CODEGEN_PKG}/generate-groups.sh" ]; then
Expand Down Expand Up @@ -119,8 +120,14 @@ function generate-knative() {
--go-header-file "$(boilerplate)"
}

# Cleanup generated code if it differs only in the boilerplate year
# Cleanup after generating code
function cleanup-codegen() {
restore-changes-if-its-copyright-year-only
restore-gopath
}

# Restore changes if the file contains only the change in the copyright year
function restore-changes-if-its-copyright-year-only() {
local difflist
log "Cleaning up generated code"
difflist="$(mktemp)"
Expand All @@ -137,6 +144,17 @@ function cleanup-codegen() {
rm -f "$difflist"
}

# Restore the GOPATH and clean up the temporary directory
function restore-gopath() {
if [ -n "$TMP_GOPATH" ] && [ -d "$TMP_GOPATH" ]; then
chmod -R u+w "${TMP_GOPATH}"
rm -rf "${TMP_GOPATH}"
unset TMP_GOPATH
fi
unset GOBIN
unset GOPATH
}

add_trap cleanup-codegen EXIT

# Restore Bash options
Expand Down
12 changes: 6 additions & 6 deletions library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -827,19 +827,19 @@ function go_mod_module_name() {
# Intended to be used like:
# export GOPATH=$(go_mod_gopath_hack)
function go_mod_gopath_hack() {
# Skip this if the directory is already checked out onto the GOPATH.
if [[ "${REPO_ROOT_DIR##$(go env GOPATH)}" != "$REPO_ROOT_DIR" ]]; then
# Skip this if the directory is already checked out onto the GOPATH.
if ! [ "${REPO_ROOT_DIR##"$(go env GOPATH)"}" = "$REPO_ROOT_DIR" ]; then
go env GOPATH
return
fi

local TMP_DIR TMP_REPO_PATH
TMP_DIR="$TMPDIR/go"
TMP_REPO_PATH="${TMP_DIR}/src/$(go_mod_module_name)"
local TMP_GOPATH TMP_REPO_PATH
TMP_GOPATH="$TMPDIR/go"
TMP_REPO_PATH="${TMP_GOPATH}/src/$(go_mod_module_name)"
mkdir -p "$(dirname "${TMP_REPO_PATH}")"
ln -s "${REPO_ROOT_DIR}" "${TMP_REPO_PATH}"

echo "${TMP_DIR}"
echo "${TMP_GOPATH}"
}

# Run kntest tool
Expand Down

0 comments on commit efe1245

Please sign in to comment.