Skip to content

Commit 4093c35

Browse files
[code-generator] feat: add func for generating register code (kubernetes#124946)
* feat: add func for generating register code * refactor:remove unused local variable * fix: make the function name singular Signed-off-by: Lin Yang <[email protected]> * fix: precisely matching the comment tag for register-gen Signed-off-by: Lin Yang <[email protected]> --------- Signed-off-by: Lin Yang <[email protected]>
1 parent 3fdf06a commit 4093c35

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

staging/src/k8s.io/code-generator/kube_codegen.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,95 @@ function kube::codegen::gen_client() {
665665
"${input_pkgs[@]}"
666666
fi
667667
}
668+
669+
# Generate register code
670+
#
671+
# USAGE: kube::codegen::gen_register [FLAGS] <input-dir>
672+
#
673+
# <input-dir>
674+
# The root directory under which to search for Go files which request code to
675+
# be generated. This must be a local path, not a Go package.
676+
#
677+
# See note at the top about package structure below that.
678+
#
679+
# FLAGS:
680+
#
681+
# --boilerplate <string = path_to_kube_codegen_boilerplate>
682+
# An optional override for the header file to insert into generated files.
683+
#
684+
function kube::codegen::gen_register() {
685+
local in_dir=""
686+
local boilerplate="${KUBE_CODEGEN_ROOT}/hack/boilerplate.go.txt"
687+
local v="${KUBE_VERBOSE:-0}"
688+
689+
while [ "$#" -gt 0 ]; do
690+
case "$1" in
691+
"--boilerplate")
692+
boilerplate="$2"
693+
shift 2
694+
;;
695+
*)
696+
if [[ "$1" =~ ^-- ]]; then
697+
echo "unknown argument: $1" >&2
698+
return 1
699+
fi
700+
if [ -n "$in_dir" ]; then
701+
echo "too many arguments: $1 (already have $in_dir)" >&2
702+
return 1
703+
fi
704+
in_dir="$1"
705+
shift
706+
;;
707+
esac
708+
done
709+
710+
if [ -z "${in_dir}" ]; then
711+
echo "input-dir argument is required" >&2
712+
return 1
713+
fi
714+
715+
(
716+
# To support running this from anywhere, first cd into this directory,
717+
# and then install with forced module mode on and fully qualified name.
718+
cd "${KUBE_CODEGEN_ROOT}"
719+
BINS=(
720+
register-gen
721+
)
722+
# shellcheck disable=2046 # printf word-splitting is intentional
723+
GO111MODULE=on go install $(printf "k8s.io/code-generator/cmd/%s " "${BINS[@]}")
724+
)
725+
# Go installs in $GOBIN if defined, and $GOPATH/bin otherwise
726+
gobin="${GOBIN:-$(go env GOPATH)/bin}"
727+
728+
# Register
729+
#
730+
local input_pkgs=()
731+
while read -r dir; do
732+
pkg="$(cd "${dir}" && GO111MODULE=on go list -find .)"
733+
input_pkgs+=("${pkg}")
734+
done < <(
735+
( kube::codegen::internal::grep -l --null \
736+
-e '^\s*//\s*+groupName' \
737+
-r "${in_dir}" \
738+
--include '*.go' \
739+
|| true \
740+
) | while read -r -d $'\0' F; do dirname "${F}"; done \
741+
| LC_ALL=C sort -u
742+
)
743+
744+
if [ "${#input_pkgs[@]}" != 0 ]; then
745+
echo "Generating register code for ${#input_pkgs[@]} targets"
746+
747+
kube::codegen::internal::findz \
748+
"${in_dir}" \
749+
-type f \
750+
-name zz_generated.register.go \
751+
| xargs -0 rm -f
752+
753+
"${gobin}/register-gen" \
754+
-v "${v}" \
755+
--output-file zz_generated.register.go \
756+
--go-header-file "${boilerplate}" \
757+
"${input_pkgs[@]}"
758+
fi
759+
}

0 commit comments

Comments
 (0)