Skip to content

Commit

Permalink
makefile: rework "{generate,vendor}-check" targets
Browse files Browse the repository at this point in the history
`TMP_DIR := $(shell mktemp -d)` causes everytime we call `make` to leak
a temporary directory "/tmp/tmp.*/". We could fix that by always using
the same directory ("/tmp/something") or by concatenating all make steps
with `TMPDIR=$(mktemp ...) && cd $TMPDIR && ...`.

Instead, add and use a "scripts/check-gittree-for-diff.sh" script.

That is useful, because the make targets "{generate,vendor}-check" are
basically the same. We can implement them both by calling the script.
And by having a stand-alone bash scripts, it's easier to review (as it's
not a shell scripted escaped in inside make).

Also, `make vendor-check` now just calls `make vendor`. Previously, it
reimplemnted how vendoring works.
  • Loading branch information
thom311 committed Nov 18, 2024
1 parent 1539363 commit ba1959a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
15 changes: 2 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,13 @@ vendor:
generate: vendor controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
GOFLAGS='' $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

TMP_DIR := $(shell mktemp -d)
.PHONY: generate-check
generate-check: controller-gen
rm -rf $(TMP_DIR)
mkdir -p $(TMP_DIR)
cp -r . $(TMP_DIR)
cd $(TMP_DIR) && make generate
diff -r . $(TMP_DIR) || exit 1
rm -rf $(TMP_DIR)
./scripts/check-gittree-for-diff.sh make generate

.PHONY: vendor-check
vendor-check:
rm -rf $(TMP_DIR)
mkdir -p $(TMP_DIR)
cp -r . $(TMP_DIR)
cd $(TMP_DIR) && go mod vendor && go mod tidy
diff -r . $(TMP_DIR) || exit 1
rm -rf $(TMP_DIR)
./scripts/check-gittree-for-diff.sh make vendor

.PHONY: fmt
fmt: ## Run go fmt against code.
Expand Down
26 changes: 26 additions & 0 deletions scripts/check-gittree-for-diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/bash

die() {
printf "%s\n" "$*"
exit 1
}

set -e

test $# -gt 0 || die "Usage: $0 CMD..."

cd "$(dirname "$0")/.."

DIR="$(mktemp -t -d dpu-operator-check-gittree-for-diff.XXXX)"

cp -ar ./ "$DIR/"

echo "Checking \`$@\` in \"$DIR\""

pushd "$DIR/" 1>/dev/null
"$@"
popd 1>/dev/null

diff -r . "$DIR/" || die "There is a difference between \"$PWD\" and \"$DIR\" after calling \`$@\`"

rm -rf "$DIR/"

0 comments on commit ba1959a

Please sign in to comment.