github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/scripts/update-codegen-helper.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2017 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 set -o errexit 18 set -o nounset 19 set -o pipefail 20 21 if [ "${BASH_VERSINFO:-0}" -lt 5 ]; then 22 >&2 printf "This script requires Bash 5.0+.\n\nOn macOS, run brew install bash and relaunch your terminal.\n" 23 exit 2 24 fi 25 26 GOPATH=$(go env GOPATH) 27 export GOPATH 28 29 SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. 30 CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)} 31 32 if [[ "$(pwd)" != "${GOPATH}"* ]]; then 33 >&2 echo "ERROR: update-codegen.sh does not work correctly outside of GOPATH: $GOPATH $(pwd)" 34 exit 1 35 fi 36 37 git config --global --add safe.directory /go/src/github.com/tilt-dev/tilt 38 39 sed -i "s/types.go/*_types.go/g" "${CODEGEN_PKG}/kube_codegen.sh" 40 source "${CODEGEN_PKG}/kube_codegen.sh" 41 sed -i "s/[*]_types.go/types.go/g" "${CODEGEN_PKG}/kube_codegen.sh" 42 43 echo "Generating code..." 44 rm -fR pkg/apis/**/zz_generated* 45 kube::codegen::gen_helpers \ 46 --boilerplate "${SCRIPT_ROOT}"/hack/boilerplate.go.txt \ 47 ./pkg/apis 48 49 rm -fR pkg/openapi 50 VIOLATIONS_ORIG_FILE=$(mktemp) 51 VIOLATIONS_FILE=$(mktemp) 52 kube::codegen::gen_openapi \ 53 --output-pkg github.com/tilt-dev/tilt/pkg/openapi \ 54 --output-dir ./pkg/openapi \ 55 --report-filename "${VIOLATIONS_ORIG_FILE}" \ 56 --update-report \ 57 --boilerplate "${SCRIPT_ROOT}"/hack/boilerplate.go.txt \ 58 ./pkg/apis 59 60 # add a sentinel line at the end of the file. 61 # this ensures grep -v doesn't remove all the warnings. 62 echo " 63 end" >> "$VIOLATIONS_ORIG_FILE" 64 65 echo "Checking violations..." 66 cat "$VIOLATIONS_ORIG_FILE" | \ 67 grep -v "API rule violation.*k8s.io" | \ 68 grep -v list_type_missing > "$VIOLATIONS_FILE" 69 70 VIOLATIONS=$(grep "API rule violation" "$VIOLATIONS_FILE" || echo -n "ok") 71 if [[ "$VIOLATIONS" != "ok" ]]; then 72 echo "ERROR: found API rule violations in tilt code" 73 echo "$VIOLATIONS" 74 exit 1 75 fi 76 77 FIXUPS=( 78 ./pkg/openapi 79 ./pkg/openapi/zz_generated.openapi.go 80 ./pkg/apis/core/v1alpha1/zz_generated.conversion.go 81 ./pkg/apis/core/v1alpha1/generated.proto 82 ./pkg/apis/core/v1alpha1/zz_generated.defaults.go 83 ./pkg/apis/core/v1alpha1/generated.pb.go 84 ./pkg/apis/core/v1alpha1/zz_generated.deepcopy.go 85 ./pkg/apis/core/zz_generated.deepcopy.go 86 ) 87 88 if [[ "$CODEGEN_UID" != "$(id -u)" ]]; then 89 groupadd --gid "$CODEGEN_GID" codegen-user 90 useradd --uid "$CODEGEN_UID" -g codegen-user codegen-user 91 92 for f in "${FIXUPS[@]}"; do 93 if [ -d "$f" ]; then 94 chmod 775 "$f" 95 else 96 chmod 664 "$f" 97 fi 98 chown codegen-user:codegen-user "$f" 99 done 100 fi