Copy make's codegen logic to update-codegen.sh

The `make` rules which auto-generate some of our API stuff are
incredibly baroque, and hard to maintain.  They were originally added on
the assumption that we would stop checking generated files into git.
Since then we have moved away from that goal, and the worst problems
with generated files have been resolved.

Reasons to kill this:
* It is slow on every build, as opposed to just being slow when running
  the generators.  It is even slow to calculate that there's nothing to
  update.
* Most development work doesn't involve changing APIs.
* It only covers about half (or less) of the generated code, and making
  it cover more would be even slower.
* Approximately 1 person knows how this all works.
* We have CI to make sure changes do not get merged without updating
  this code.
* We have corner cases where this does the WRONG thing and tracking
  those down is ugly and hard in perpetuity.

So this commit puts all the same logic that WAS in the
Makefile.generated_files into update-codegen.sh.

I do not love this script, especially WRT sub-packages, but I am trying
not to boil the ocean.  I hope to follow up with some more cleanups over
time.

I have tested this manually and with the scripts and it still seems to
catch errors properly.

This includes a change to kube::util::read-array to make it not unset
variables and not over-write non-array variables.
This commit is contained in:
Tim Hockin
2022-09-23 13:06:35 -07:00
parent b52b50bcea
commit 7afaf23b01
3 changed files with 814 additions and 110 deletions

View File

@@ -798,19 +798,54 @@ function kube::util::md5() {
# kube::util::read-array
# Reads in stdin and adds it line by line to the array provided. This can be
# used instead of "mapfile -t", and is bash 3 compatible.
# used instead of "mapfile -t", and is bash 3 compatible. If the named array
# exists and is an array, it will be used. Otherwise it will be unset and
# recreated.
#
# Assumed vars:
# $1 (name of array to create/modify)
#
# Example usage:
# kube::util::read-array files < <(ls -1)
# kube::util::read-array files < <(ls -1)
#
# When in doubt:
# $ W=abc # a string
# $ X=(a b c) # an array
# $ declare -A Y # an associative array
# $ unset Z # not set at all
# $ declare -p W X Y Z
# declare -- W="abc"
# declare -a X=([0]="a" [1]="b" [2]="c")
# declare -A Y
# bash: line 26: declare: Z: not found
# $ kube::util::read-array W < <(echo -ne "1 1\n2 2\n3 3\n")
# bash: W is defined but isn't an array
# $ kube::util::read-array X < <(echo -ne "1 1\n2 2\n3 3\n")
# $ kube::util::read-array Y < <(echo -ne "1 1\n2 2\n3 3\n")
# bash: Y is defined but isn't an array
# $ kube::util::read-array Z < <(echo -ne "1 1\n2 2\n3 3\n")
# $ declare -p W X Y Z
# declare -- W="abc"
# declare -a X=([0]="1 1" [1]="2 2" [2]="3 3")
# declare -A Y
# declare -a Z=([0]="1 1" [1]="2 2" [2]="3 3")
function kube::util::read-array {
local i=0
unset -v "$1"
while IFS= read -r "$1[i++]"; do :; done
eval "[[ \${$1[--i]} ]]" || unset "$1[i]" # ensures last element isn't empty
if [[ -z "$1" ]]; then
echo "usage: ${FUNCNAME[0]} <varname>" >&2
return 1
fi
if [[ -n $(declare -p "$1" 2>/dev/null) ]]; then
if ! declare -p "$1" 2>/dev/null | grep -q '^declare -a'; then
echo "${FUNCNAME[0]}: $1 is defined but isn't an array" >&2
return 2
fi
fi
# shellcheck disable=SC2034 # this variable _is_ used
local __read_array_i=0
while IFS= read -r "$1[__read_array_i++]"; do :; done
if ! eval "[[ \${$1[--__read_array_i]} ]]"; then
unset "$1[__read_array_i]" # ensures last element isn't empty
fi
}
# Some useful colors.