From 0a8f10249772bee9d6c1548bf93080abd0f8355a Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Tue, 3 Jan 2023 21:37:35 -0800 Subject: [PATCH] Codegen: use ls files and pre-remove gen'ed mocks --- hack/update-mocks.sh | 77 +++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/hack/update-mocks.sh b/hack/update-mocks.sh index f32feb97e03..afa7a53fcd1 100755 --- a/hack/update-mocks.sh +++ b/hack/update-mocks.sh @@ -30,68 +30,73 @@ _tmp="${KUBE_ROOT}/_tmp_build_tag_files" mkdir -p "${_tmp}" function cleanup { - rm -rf "$_tmp" - rm -f "tempfile" + rm -rf "$_tmp" + rm -f "tempfile" } trap cleanup EXIT kube::golang::verify_go_version +kube::golang::setup_env echo 'installing mockgen' pushd "${KUBE_ROOT}/hack/tools" >/dev/null go install github.com/golang/mock/mockgen popd >/dev/null -find_files() { - find . -not \( \ - \( \ - -wholename './output' \ - -o -wholename './.git' \ - -o -wholename './_output' \ - -o -wholename './_gopath' \ - -o -wholename './release' \ - -o -wholename './target' \ - -o -wholename '*/third_party/*' \ - -o -wholename '*/vendor/*' \ - -o -wholename './staging/src/k8s.io/client-go/*vendor/*' \ - -o -wholename '*/bindata.go' \ - \) -prune \ - \) -name '*.go' +function git_find() { + # Similar to find but faster and easier to understand. We want to include + # modified and untracked files because this might be running against code + # which is not tracked by git yet. + git ls-files -cmo --exclude-standard \ + ':!:vendor/*' \ + ':!:third_party/*' \ + ':!:*/testdata/*' \ + "$@" } cd "${KUBE_ROOT}" +GENERATED_MOCK_FILE_REGEX="^// Code generated by MockGen. DO NOT EDIT.$" + +# We use this pattern here rather than `git grep` because we don't really want +# to encode the pathspec list in multiple places and anything more complicated +# just isn't worth the effort. +git_find -z ':(glob)**/*.go' \ + | { xargs -0 grep -l --null "${GENERATED_MOCK_FILE_REGEX}" || true; } \ + | xargs -0 rm -f + echo 'executing go generate command on below files' -for IFILE in $(find_files | xargs grep --files-with-matches -e '//go:generate mockgen'); do - temp_file_name=$(mktemp --tmpdir="${_tmp}") - # serach for build tag used in file - build_tag_string=$(grep -o '+build.*$' "$IFILE") || true - +git_find -z ':(glob)**/*.go' | while read -r -d $'\0' file; do + test -f "$file" || continue + grep -q "//go:generate mockgen" "$file" || continue + + temp_file_name="$(kube::realpath "$(mktemp -t "$(basename "$0").XXXXXX")")" + + # search for build tag used in file + build_tag_string=$(grep -o '+build.*$' "$file") || true + # if the file does not have build string - if [ -n "$build_tag_string" ] - then + if [ -n "$build_tag_string" ]; then # write the build tag in the temp file echo -n "$build_tag_string" > "$temp_file_name" - + # if +build tag is defined in interface file - BUILD_TAG_FILE=$temp_file_name go generate -v "$IFILE" + BUILD_TAG_FILE=$temp_file_name go generate -v "$file" else # if no +build tag is defined in interface file - go generate -v "$IFILE" + go generate -v "$file" fi done +# get the changed or new mock files +git ls-files -mo --exclude-standard -z | while read -r -d $'\0' file; do + # only process files that appear to be mocks + test -f "$file" || continue + grep -q "${GENERATED_MOCK_FILE_REGEX}" "$file" || continue -# get the changed mock files -files=$(git diff --name-only) -for file in $files; do - if [ "$file" == "hack/update-mocks.sh" ]; then - continue - fi - - # serach for build tags used in file + # search for build tags used in file # //go:build !providerless # // +build !providerless go_build_tag_string=$(grep -o 'go:build.*$' "$file") || true @@ -128,7 +133,7 @@ for file in $files; do tail -n +2 "$file" > tempfile fi - # if the header if generted + # if the header is generated if [ -n "$new_header" ] then # write the newly generated header file to the original file