Codegen: use ls files and pre-remove gen'ed mocks

This commit is contained in:
Tim Hockin 2023-01-03 21:37:35 -08:00
parent 600e46801d
commit 0a8f102497
No known key found for this signature in database

View File

@ -30,68 +30,73 @@ _tmp="${KUBE_ROOT}/_tmp_build_tag_files"
mkdir -p "${_tmp}" mkdir -p "${_tmp}"
function cleanup { function cleanup {
rm -rf "$_tmp" rm -rf "$_tmp"
rm -f "tempfile" rm -f "tempfile"
} }
trap cleanup EXIT trap cleanup EXIT
kube::golang::verify_go_version kube::golang::verify_go_version
kube::golang::setup_env
echo 'installing mockgen' echo 'installing mockgen'
pushd "${KUBE_ROOT}/hack/tools" >/dev/null pushd "${KUBE_ROOT}/hack/tools" >/dev/null
go install github.com/golang/mock/mockgen go install github.com/golang/mock/mockgen
popd >/dev/null popd >/dev/null
find_files() { function git_find() {
find . -not \( \ # Similar to find but faster and easier to understand. We want to include
\( \ # modified and untracked files because this might be running against code
-wholename './output' \ # which is not tracked by git yet.
-o -wholename './.git' \ git ls-files -cmo --exclude-standard \
-o -wholename './_output' \ ':!:vendor/*' \
-o -wholename './_gopath' \ ':!:third_party/*' \
-o -wholename './release' \ ':!:*/testdata/*' \
-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'
} }
cd "${KUBE_ROOT}" 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' echo 'executing go generate command on below files'
for IFILE in $(find_files | xargs grep --files-with-matches -e '//go:generate mockgen'); do git_find -z ':(glob)**/*.go' | while read -r -d $'\0' file; do
temp_file_name=$(mktemp --tmpdir="${_tmp}") test -f "$file" || continue
# serach for build tag used in file grep -q "//go:generate mockgen" "$file" || continue
build_tag_string=$(grep -o '+build.*$' "$IFILE") || true
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 the file does not have build string
if [ -n "$build_tag_string" ] if [ -n "$build_tag_string" ]; then
then
# write the build tag in the temp file # write the build tag in the temp file
echo -n "$build_tag_string" > "$temp_file_name" echo -n "$build_tag_string" > "$temp_file_name"
# if +build tag is defined in interface file # 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 else
# if no +build tag is defined in interface file # if no +build tag is defined in interface file
go generate -v "$IFILE" go generate -v "$file"
fi fi
done 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 # search for build tags used in file
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
# //go:build !providerless # //go:build !providerless
# // +build !providerless # // +build !providerless
go_build_tag_string=$(grep -o 'go:build.*$' "$file") || true go_build_tag_string=$(grep -o 'go:build.*$' "$file") || true
@ -128,7 +133,7 @@ for file in $files; do
tail -n +2 "$file" > tempfile tail -n +2 "$file" > tempfile
fi fi
# if the header if generted # if the header is generated
if [ -n "$new_header" ] if [ -n "$new_header" ]
then then
# write the newly generated header file to the original file # write the newly generated header file to the original file