|
16 | 16 |
|
17 | 17 | # Setup the env for doing Knative style codegen.
|
18 | 18 |
|
19 |
| -kn_hack_library=${kn_hack_library:-"$(dirname $0)/../vendor/knative.dev/hack/library.sh"} |
| 19 | +# Store Bash options |
| 20 | +oldstate="$(set +o)" |
| 21 | + |
| 22 | +set -Eeuo pipefail |
| 23 | + |
| 24 | +export repodir kn_hack_dir kn_hack_library \ |
| 25 | + MODULE_NAME CODEGEN_TMP_GOPATH CODEGEN_ORIGINAL_GOPATH GOPATH GOBIN \ |
| 26 | + CODEGEN_PKG KNATIVE_CODEGEN_PKG |
| 27 | + |
| 28 | +kn_hack_dir="$(realpath "$(dirname "${BASH_SOURCE[0]:-$0}")")" |
| 29 | +kn_hack_library=${kn_hack_library:-"${kn_hack_dir}/library.sh"} |
20 | 30 |
|
21 | 31 | if [[ -f "$kn_hack_library" ]]; then
|
22 |
| - source $kn_hack_library |
| 32 | + # shellcheck disable=SC1090 |
| 33 | + source "$kn_hack_library" |
23 | 34 | else
|
24 |
| - echo "this file is intended to be imported from a golang project that vendors knative.dev/hack" |
25 |
| - exit |
| 35 | + echo "The \$kn_hack_library points to a non-existent file: $kn_hack_library" >&2 |
| 36 | + exit 42 |
26 | 37 | fi
|
27 | 38 |
|
28 |
| -export MODULE_NAME=$(go_mod_module_name) |
29 |
| -export GOPATH=$(go_mod_gopath_hack) |
30 |
| -export GOBIN=${GOPATH}/bin # Set GOBIN explicitly as deepcopy-gen is installed by go install. |
31 |
| -export CODEGEN_PKG=${CODEGEN_PKG:-$(cd ${REPO_ROOT_DIR}; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)} |
32 |
| -export KNATIVE_CODEGEN_PKG=${KNATIVE_CODEGEN_PKG:-$(cd ${REPO_ROOT_DIR}; ls -d -1 ./vendor/knative.dev/pkg 2>/dev/null || echo "${REPO_ROOT_DIR}")} |
| 39 | +repodir="$(go_run knative.dev/toolbox/modscope@latest current --path)" |
| 40 | + |
| 41 | +function go-resolve-pkg-dir() { |
| 42 | + local pkg="${1:?Pass the package name}" |
| 43 | + local pkgdir |
| 44 | + if [ -d "${repodir}/vendor" ]; then |
| 45 | + pkgdir="${repodir}/vendor/${pkg}" |
| 46 | + if [ -d "${pkgdir}" ]; then |
| 47 | + echo "${pkgdir}" |
| 48 | + return 0 |
| 49 | + else |
| 50 | + return 1 |
| 51 | + fi |
| 52 | + else |
| 53 | + go mod download -x > /dev/stderr |
| 54 | + go list -m -f '{{.Dir}}' "${pkg}" 2>/dev/null |
| 55 | + return $? |
| 56 | + fi |
| 57 | +} |
| 58 | + |
| 59 | +# Change dir to the original executing script's directory, not the current source! |
| 60 | +pushd "$(dirname "$(realpath "$0")")" > /dev/null |
| 61 | + |
| 62 | +if ! CODEGEN_PKG="${CODEGEN_PKG:-"$(go-resolve-pkg-dir k8s.io/code-generator)"}"; then |
| 63 | + warning "Failed to determine the k8s.io/code-generator package" |
| 64 | +fi |
| 65 | +if ! KNATIVE_CODEGEN_PKG="${KNATIVE_CODEGEN_PKG:-"$(go-resolve-pkg-dir knative.dev/pkg)"}"; then |
| 66 | + warning "Failed to determine the knative.dev/pkg package" |
| 67 | +fi |
| 68 | + |
| 69 | +popd > /dev/null |
| 70 | + |
| 71 | +CODEGEN_ORIGINAL_GOPATH="$(go env GOPATH)" |
| 72 | +CODEGEN_TMP_GOPATH=$(go_mod_gopath_hack) |
| 73 | +GOPATH="${CODEGEN_TMP_GOPATH}" |
| 74 | +GOBIN="${GOPATH}/bin" # Set GOBIN explicitly as k8s-gen' are installed by go install. |
| 75 | + |
| 76 | +if [[ -n "${CODEGEN_PKG}" ]] && ! [ -x "${CODEGEN_PKG}/generate-groups.sh" ]; then |
| 77 | + chmod +x "${CODEGEN_PKG}/generate-groups.sh" |
| 78 | + chmod +x "${CODEGEN_PKG}/generate-internal-groups.sh" |
| 79 | +fi |
| 80 | +if [[ -n "${KNATIVE_CODEGEN_PKG}" ]] && ! [ -x "${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh" ]; then |
| 81 | + chmod +x "${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh" |
| 82 | +fi |
| 83 | + |
| 84 | +# Generate boilerplate file with the current year |
| 85 | +function boilerplate() { |
| 86 | + local go_header_file="${kn_hack_dir}/boilerplate.go.txt" |
| 87 | + local current_boilerplate_file="${TMPDIR}/boilerplate.go.txt" |
| 88 | + # Replace #{YEAR} with the current year |
| 89 | + sed "s/#{YEAR}/$(date +%Y)/" \ |
| 90 | + < "${go_header_file}" \ |
| 91 | + > "${current_boilerplate_file}" |
| 92 | + echo "${current_boilerplate_file}" |
| 93 | +} |
| 94 | + |
| 95 | +# Generate K8s' groups codegen |
| 96 | +function generate-groups() { |
| 97 | + if [[ -z "${CODEGEN_PKG}" ]]; then |
| 98 | + abort "CODEGEN_PKG is not set" |
| 99 | + fi |
| 100 | + "${CODEGEN_PKG}"/generate-groups.sh \ |
| 101 | + "$@" \ |
| 102 | + --go-header-file "$(boilerplate)" |
| 103 | +} |
| 104 | + |
| 105 | +# Generate K8s' internal groups codegen |
| 106 | +function generate-internal-groups() { |
| 107 | + if [[ -z "${CODEGEN_PKG}" ]]; then |
| 108 | + abort "CODEGEN_PKG is not set" |
| 109 | + fi |
| 110 | + "${CODEGEN_PKG}"/generate-internal-groups.sh \ |
| 111 | + "$@" \ |
| 112 | + --go-header-file "$(boilerplate)" |
| 113 | +} |
| 114 | + |
| 115 | +# Generate Knative style codegen |
| 116 | +function generate-knative() { |
| 117 | + if [[ -z "${KNATIVE_CODEGEN_PKG}" ]]; then |
| 118 | + abort "KNATIVE_CODEGEN_PKG is not set" |
| 119 | + fi |
| 120 | + "${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh" \ |
| 121 | + "$@" \ |
| 122 | + --go-header-file "$(boilerplate)" |
| 123 | +} |
| 124 | + |
| 125 | +# Cleanup after generating code |
| 126 | +function cleanup-codegen() { |
| 127 | + restore-changes-if-its-copyright-year-only |
| 128 | + restore-gopath |
| 129 | +} |
| 130 | + |
| 131 | +# Restore changes if the file contains only the change in the copyright year |
| 132 | +function restore-changes-if-its-copyright-year-only() { |
| 133 | + local difflist |
| 134 | + log "Cleaning up generated code" |
| 135 | + difflist="$(mktemp)" |
| 136 | + if ! git diff --exit-code --name-only > /dev/null; then |
| 137 | + # list git changes and skip those which differ only in the boilerplate year |
| 138 | + git diff --name-only > "$difflist" |
| 139 | + while read -r file; do |
| 140 | + # check if the file contains just the change in the boilerplate year |
| 141 | + if [ "$(LANG=C git diff --exit-code --shortstat -- "$file")" = ' 1 file changed, 1 insertion(+), 1 deletion(-)' ] && \ |
| 142 | + [[ "$(git diff --exit-code -U1 -- "$file" | grep -Ec '^[+-]\s*[*#]?\s*Copyright 2[0-9]{3}')" -eq 2 ]]; then |
| 143 | + # restore changes to that file |
| 144 | + git checkout -- "$file" |
| 145 | + fi |
| 146 | + done < "$difflist" |
| 147 | + fi |
| 148 | + rm -f "$difflist" |
| 149 | +} |
| 150 | + |
| 151 | +# Restore the GOPATH and clean up the temporary directory |
| 152 | +function restore-gopath() { |
| 153 | + # Skip this if the directory is already checked out onto the GOPATH. |
| 154 | + if __is_checkout_onto_gopath; then |
| 155 | + return |
| 156 | + fi |
| 157 | + if [ -d "$CODEGEN_TMP_GOPATH" ]; then |
| 158 | + chmod -R u+w "${CODEGEN_TMP_GOPATH}" |
| 159 | + rm -rf "${CODEGEN_TMP_GOPATH}" |
| 160 | + unset CODEGEN_TMP_GOPATH |
| 161 | + fi |
| 162 | + unset GOPATH GOBIN |
| 163 | + # Restore the original GOPATH, if it was different from the default one. |
| 164 | + if [ "$CODEGEN_ORIGINAL_GOPATH" != "$(go env GOPATH)" ]; then |
| 165 | + export GOPATH="$CODEGEN_ORIGINAL_GOPATH" |
| 166 | + fi |
| 167 | + unset CODEGEN_ORIGINAL_GOPATH |
| 168 | +} |
| 169 | + |
| 170 | +add_trap cleanup-codegen EXIT |
33 | 171 |
|
34 |
| -[ -x ${CODEGEN_PKG}/generate-groups.sh ] || chmod +x ${CODEGEN_PKG}/generate-groups.sh |
35 |
| -[ -x ${CODEGEN_PKG}/generate-internal-groups.sh ] || chmod +x ${CODEGEN_PKG}/generate-internal-groups.sh |
36 |
| -[ -x ${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh ] || chmod +x ${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh |
| 172 | +# Restore Bash options |
| 173 | +eval "$oldstate" |
0 commit comments