diff --git a/hack/codegen.sh b/hack/codegen.sh index 147c505b..91e78db5 100755 --- a/hack/codegen.sh +++ b/hack/codegen.sh @@ -68,8 +68,33 @@ function codegen::generate_for_module() { } function codegen::generate() { + local mod_basename_for_version + mod_basename_for_version="${K8S_PKG_VERSION}/$(basename "${MOD_DIR}")" + codegen::ensure_module_in_gopath - codegen::generate_for_module "${K8S_PKG_VERSION}/$(basename "${MOD_DIR}")" + codegen::generate_for_module "${mod_basename_for_version}" +} + +function codegen::verify() { + local have_stash='' + if [[ "$(git status --porcelain 2>/dev/null | wc -l | tr -d ' ')" -ne "0" ]]; then + git stash --all >/dev/null 2>&1 && have_stash=1 + fi + + codegen::generate + + if [[ "$(git status --porcelain 2>/dev/null | wc -l | tr -d ' ')" -eq "0" ]]; then + echo "Generated code in ${MOD_DIR} up to date." + else + echo "Generated code in ${MOD_DIR} is out of date." + echo "Please run hack/module.sh codegen" + git diff "${ROOT}" + git checkout "${ROOT}" + fi + + if [[ -n "${have_stash}" ]]; then + git stash pop >/dev/null 2>&1 + fi } function codegen::usage() { diff --git a/hack/module.sh b/hack/module.sh index e8b2ff51..f5c94834 100755 --- a/hack/module.sh +++ b/hack/module.sh @@ -37,6 +37,10 @@ function codegen_cmd() { echo "${ROOT}/hack/codegen.sh codegen::generate" } +function codegen_verify_cmd() { + echo "${ROOT}/hack/codegen.sh codegen::verify" +} + # The race detector is slow, so sometimes you don't want to use it function unittest_no_race_cmd() { if [ -x "$(command -v gotest)" ]; then @@ -66,7 +70,7 @@ function with_modules() { function usage() { echo "Error: must be specified" - echo " do.sh [tidy, lint, test, unittest, unittest_no_race, codegen]" + echo " do.sh [tidy, lint, test, unittest, unittest_no_race, codegen, codegen_verify]" exit 1 } @@ -78,6 +82,7 @@ function main() { 'unittest') with_modules 'unittest_cmd' ;; 'unittest_no_race') with_modules 'unittest_no_race_cmd' ;; 'codegen') with_modules 'codegen_cmd' ;; + 'codegen_verify') with_modules 'codegen_verify_cmd' ;; *) usage ;; esac }