From d54ff235601ee16cdb6c4b9702b9554b87a5d0ed Mon Sep 17 00:00:00 2001 From: Jeffrey Regan Date: Fri, 8 Nov 2019 14:28:19 -0800 Subject: [PATCH] Move code generation to makefile. --- Makefile | 73 ++++++++++++++++--- .../builtinhelpers/generateBuiltins.sh | 38 ---------- pluginator/main.go | 3 +- travis/pre-commit.sh | 33 ++++----- 4 files changed, 76 insertions(+), 71 deletions(-) delete mode 100755 api/internal/plugins/builtinhelpers/generateBuiltins.sh diff --git a/Makefile b/Makefile index fa57ef9fd..7a079c23b 100644 --- a/Makefile +++ b/Makefile @@ -8,16 +8,27 @@ # gradually being moved here. MYGOBIN := $(shell go env GOPATH)/bin +PATH := $(PATH):$(MYGOBIN) +SHELL := env PATH=$(PATH) /bin/bash .PHONY: all -all: +all: pre-commit + +# The pre-commit.sh script generates, lints and tests. +# It uses this makefile. For more clarity, would like +# to stop that - any scripts invoked by targets here +# shouldn't "call back" to the makefile. +.PHONY: pre-commit +pre-commit: ./travis/pre-commit.sh $(MYGOBIN)/golangci-lint: - cd api; go install github.com/golangci/golangci-lint/cmd/golangci-lint + cd api; \ + go install github.com/golangci/golangci-lint/cmd/golangci-lint $(MYGOBIN)/mdrip: - cd api; go install github.com/monopole/mdrip + cd api; \ + go install github.com/monopole/mdrip # TODO: need a new release of the API, followed by a new pluginator. # pluginator v1.1.0 is too old for the code currently needed in the API. @@ -27,28 +38,61 @@ $(MYGOBIN)/mdrip: # - pin the version tag in './api/go.mod' to match the new release # - change the following to 'cd api; go install sigs.k8s.io/kustomize/pluginator' $(MYGOBIN)/pluginator: - cd pluginator; go install . + cd pluginator; \ + go install . $(MYGOBIN)/stringer: - cd api; go install golang.org/x/tools/cmd/stringer + cd api; \ + go install golang.org/x/tools/cmd/stringer -# Specific version tags for these utilities are pinned in ./api/go.mod -# which seems to be as good a place as any to do so. -# That's the reason for all the occurances of 'cd api;' in the -# dependencies; 'go install' uses the local 'go.mod' to get the version. -install-tools: $(MYGOBIN)/golangci-lint \ +# Specific version tags for these utilities are pinned +# in ./api/go.mod, which seems to be as good a place as +# any to do so. That's the reason for all the occurances +# of 'cd api;' in the dependencies; 'go install' uses the +# local 'go.mod' to find the correct version to install. +.PHONY: install-tools +install-tools: \ + $(MYGOBIN)/golangci-lint \ $(MYGOBIN)/mdrip \ $(MYGOBIN)/pluginator \ $(MYGOBIN)/stringer +# Builtin plugins are generated code. +# Add new items here to create new builtins. +builtinplugins = \ + api/builtins/annotationstransformer.go \ + api/builtins/configmapgenerator.go \ + api/builtins/hashtransformer.go \ + api/builtins/imagetagtransformer.go \ + api/builtins/inventorytransformer.go \ + api/builtins/labeltransformer.go \ + api/builtins/legacyordertransformer.go \ + api/builtins/namespacetransformer.go \ + api/builtins/patchjson6902transformer.go \ + api/builtins/patchstrategicmergetransformer.go \ + api/builtins/patchtransformer.go \ + api/builtins/prefixsuffixtransformer.go \ + api/builtins/replicacounttransformer.go \ + api/builtins/secretgenerator.go + .PHONY: lint -lint: install-tools +lint: install-tools $(builtinplugins) cd api; $(MYGOBIN)/golangci-lint run ./... cd kustomize; $(MYGOBIN)/golangci-lint run ./... cd pluginator; $(MYGOBIN)/golangci-lint run ./... +# pluginator consults the GOPATH env var to write generated code. +api/builtins/%.go: $(MYGOBIN)/pluginator + @echo "generating $*"; \ + cd plugin/builtin/$*; \ + GOPATH=$(shell pwd)/../../.. go generate ./...; \ + go fmt ./... + +.PHONY: generate +generate: $(builtinplugins) + .PHONY: unit-test-api -unit-test-api: +unit-test-api: $(builtinplugins) cd api; go test ./... .PHONY: unit-test-plugins @@ -77,3 +121,8 @@ $(MYGOBIN)/helm: tar -xvzf helm-v2.13.1-linux-amd64.tar.gz; \ mv linux-amd64/helm $(MYGOBIN); \ rm -rf $$d + +.PHONY: clean +clean: + rm -f $(builtinplugins) + rm -f $(MYGOBIN)/pluginator diff --git a/api/internal/plugins/builtinhelpers/generateBuiltins.sh b/api/internal/plugins/builtinhelpers/generateBuiltins.sh deleted file mode 100755 index d925bb16f..000000000 --- a/api/internal/plugins/builtinhelpers/generateBuiltins.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash -# -# Generate the Go code for the generator and -# transformer factory functions in -# -# sigs.k8s.io/kustomize/api/builtins -# -# from the raw plugin directories found under -# -# sigs.k8s.io/kustomize/plugin/builtin - -set -e - -myGoPath=$1 -if [ -z ${1+x} ]; then - myGoPath=$GOPATH -fi - -if [ -z "$myGoPath" ]; then - echo "Must specify a GOPATH" - exit 1 -fi - -dir=$myGoPath/src/sigs.k8s.io/kustomize - -if [ ! -d "$dir" ]; then - echo "$dir is not a directory." - exit 1 -fi - -for goMod in $(find ./plugin/builtin -name 'go.mod'); do - dir=$(dirname "${goMod}") - (cd $dir; GOPATH=$myGoPath go generate ./...) - echo "Formatting $dir" - (cd $dir; GOPATH=$myGoPath go fmt ./...) -done - -echo All done. diff --git a/pluginator/main.go b/pluginator/main.go index 9cf10967d..32209aa46 100644 --- a/pluginator/main.go +++ b/pluginator/main.go @@ -132,7 +132,8 @@ func makeOutputFileName(root string) string { } func (w *writer) close() { - fmt.Println("Generated " + w.root) + // Do this for debugging. + // fmt.Println("Generated " + makeOutputFileName(w.root)) w.f.Close() } diff --git a/travis/pre-commit.sh b/travis/pre-commit.sh index 70139e7d2..f01c083d3 100755 --- a/travis/pre-commit.sh +++ b/travis/pre-commit.sh @@ -74,11 +74,6 @@ function testExamplesAgainstLocalHead { echo "Example tests passed against HEAD" } -function generateCode { - echo "preferredGoPath = $preferredGoPath" - ./api/internal/plugins/builtinhelpers/generateBuiltins.sh $preferredGoPath -} - # This script tries to work for both travis # and contributors who have or do not have # GOPATH set. @@ -86,8 +81,8 @@ function generateCode { # Use GOPATH to define XDG_CONFIG_HOME, then unset # GOPATH so that go.mod is unambiguously honored. # -function setPreferredGoPathAndUnsetGoPath { - preferredGoPath=$GOPATH +function unsetGoPath { + local preferredGoPath=$GOPATH if [ -z ${GOPATH+x} ]; then # GOPATH is unset local tmp=$HOME/gopath @@ -107,21 +102,20 @@ function setPreferredGoPathAndUnsetGoPath { echo "GOPATH=$GOPATH, but should be unset at this point." exit 1 fi - echo "preferredGoPath=$preferredGoPath" + + # This is needed for plugins. + # TODO: switch this to set KUSTOMIZE_PLUGIN_HOME instead. + export XDG_CONFIG_HOME=$preferredGoPath/src/sigs.k8s.io + echo "XDG_CONFIG_HOME=$XDG_CONFIG_HOME" + if [ ! -d "$XDG_CONFIG_HOME" ]; then + echo "$XDG_CONFIG_HOME is not a directory." + echo "Unable to compile or otherwise work with kustomize plugins." + exit 1 + fi } -# We don't want GOPATH to be defined, as it -# has too much baggage. -setPreferredGoPathAndUnsetGoPath +unsetGoPath -# This is needed for plugins. -export XDG_CONFIG_HOME=$preferredGoPath/src/sigs.k8s.io -echo "XDG_CONFIG_HOME=$XDG_CONFIG_HOME" -if [ ! -d "$XDG_CONFIG_HOME" ]; then - echo "$XDG_CONFIG_HOME is not a directory." - echo "Unable to compile or otherwise work with kustomize plugins." - exit 1 -fi # With GOPATH now undefined, this most # likely this puts $HOME/go/bin on the path. @@ -144,7 +138,6 @@ echo " " echo "Working..." runFunc installTools -runFunc generateCode runFunc runLint runFunc runUnitTests runFunc testExamplesAgainstLatestKustomizeRelease