Improve and simplify build scripts

We can simplify the scripts added in #2928 to make them more robust and maintainable. Proposed improvements are as follows.

script/generate.sh

The current script/generate.sh
serves two purposes:

  1. Generating Go code by running go generate and go mod tidy.
  2. Verifying whether the first step produces any diff.

Currently, the second step is overly complex:

  1. Creating a temporary git worktree
  2. Copying current files into it
  3. Making a commit with the current state
  4. Running generate.sh to regenerate files
  5. Checking if any files changed

While this approach avoids modifying the local working directory, the complexity often leads to unexpected issues like #3982.

I propose removing the Git worktree and commit logic. We can simply run go generate, git diff, git restore.

Proposed new script/generate.sh
#!/bin/sh
#/ script/generate.sh runs go generate on all modules in this repo.
#/ `script/generate.sh --check` checks that the generated files are up to date.

set -e

CDPATH="" cd -- "$(dirname -- "$0")/.."

CHECK_MODE=""
if [ "$1" = "--check" ]; then
  CHECK_MODE=1
fi

MOD_DIRS="$(git ls-files '*go.mod' | xargs dirname | sort)"

for dir in $MOD_DIRS; do
  (
    cd "$dir"
    go generate ./...
    go mod tidy
  )
done

if [ -n "$CHECK_MODE" ]; then
  if [ -n "$(git status --porcelain)" ]; then
    msg="Generated files are out of date. Please run script/generate.sh and commit the results"
    if [ -n "$GITHUB_ACTIONS" ]; then
      echo "::error ::$msg"
    else
      echo "$msg" 1>&2
    fi
    git diff
    git restore .
    exit 1
  fi
fi

script/metadata.sh

The script/metadata.sh currently builds and executes tools/metadata with various parameters. It can be replaced with go run for consistency with go run gen-accessors.go, go run gen-iterators.go.