Merge pull request #1765 from monopole/makaMaka

Move code generation to makefile.
This commit is contained in:
Jeff Regan
2019-11-09 08:54:15 -08:00
committed by GitHub
4 changed files with 76 additions and 71 deletions

View File

@@ -8,16 +8,27 @@
# gradually being moved here. # gradually being moved here.
MYGOBIN := $(shell go env GOPATH)/bin MYGOBIN := $(shell go env GOPATH)/bin
PATH := $(PATH):$(MYGOBIN)
SHELL := env PATH=$(PATH) /bin/bash
.PHONY: all .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 ./travis/pre-commit.sh
$(MYGOBIN)/golangci-lint: $(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: $(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. # 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. # 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 # - 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' # - change the following to 'cd api; go install sigs.k8s.io/kustomize/pluginator'
$(MYGOBIN)/pluginator: $(MYGOBIN)/pluginator:
cd pluginator; go install . cd pluginator; \
go install .
$(MYGOBIN)/stringer: $(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 # Specific version tags for these utilities are pinned
# which seems to be as good a place as any to do so. # in ./api/go.mod, which seems to be as good a place as
# That's the reason for all the occurances of 'cd api;' in the # any to do so. That's the reason for all the occurances
# dependencies; 'go install' uses the local 'go.mod' to get the version. # of 'cd api;' in the dependencies; 'go install' uses the
install-tools: $(MYGOBIN)/golangci-lint \ # local 'go.mod' to find the correct version to install.
.PHONY: install-tools
install-tools: \
$(MYGOBIN)/golangci-lint \
$(MYGOBIN)/mdrip \ $(MYGOBIN)/mdrip \
$(MYGOBIN)/pluginator \ $(MYGOBIN)/pluginator \
$(MYGOBIN)/stringer $(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 .PHONY: lint
lint: install-tools lint: install-tools $(builtinplugins)
cd api; $(MYGOBIN)/golangci-lint run ./... cd api; $(MYGOBIN)/golangci-lint run ./...
cd kustomize; $(MYGOBIN)/golangci-lint run ./... cd kustomize; $(MYGOBIN)/golangci-lint run ./...
cd pluginator; $(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 .PHONY: unit-test-api
unit-test-api: unit-test-api: $(builtinplugins)
cd api; go test ./... cd api; go test ./...
.PHONY: unit-test-plugins .PHONY: unit-test-plugins
@@ -77,3 +121,8 @@ $(MYGOBIN)/helm:
tar -xvzf helm-v2.13.1-linux-amd64.tar.gz; \ tar -xvzf helm-v2.13.1-linux-amd64.tar.gz; \
mv linux-amd64/helm $(MYGOBIN); \ mv linux-amd64/helm $(MYGOBIN); \
rm -rf $$d rm -rf $$d
.PHONY: clean
clean:
rm -f $(builtinplugins)
rm -f $(MYGOBIN)/pluginator

View File

@@ -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.

View File

@@ -132,7 +132,8 @@ func makeOutputFileName(root string) string {
} }
func (w *writer) close() { func (w *writer) close() {
fmt.Println("Generated " + w.root) // Do this for debugging.
// fmt.Println("Generated " + makeOutputFileName(w.root))
w.f.Close() w.f.Close()
} }

View File

@@ -74,11 +74,6 @@ function testExamplesAgainstLocalHead {
echo "Example tests passed against HEAD" 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 # This script tries to work for both travis
# and contributors who have or do not have # and contributors who have or do not have
# GOPATH set. # GOPATH set.
@@ -86,8 +81,8 @@ function generateCode {
# Use GOPATH to define XDG_CONFIG_HOME, then unset # Use GOPATH to define XDG_CONFIG_HOME, then unset
# GOPATH so that go.mod is unambiguously honored. # GOPATH so that go.mod is unambiguously honored.
# #
function setPreferredGoPathAndUnsetGoPath { function unsetGoPath {
preferredGoPath=$GOPATH local preferredGoPath=$GOPATH
if [ -z ${GOPATH+x} ]; then if [ -z ${GOPATH+x} ]; then
# GOPATH is unset # GOPATH is unset
local tmp=$HOME/gopath local tmp=$HOME/gopath
@@ -107,21 +102,20 @@ function setPreferredGoPathAndUnsetGoPath {
echo "GOPATH=$GOPATH, but should be unset at this point." echo "GOPATH=$GOPATH, but should be unset at this point."
exit 1 exit 1
fi 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 unsetGoPath
# has too much baggage.
setPreferredGoPathAndUnsetGoPath
# 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 # With GOPATH now undefined, this most
# likely this puts $HOME/go/bin on the path. # likely this puts $HOME/go/bin on the path.
@@ -144,7 +138,6 @@ echo " "
echo "Working..." echo "Working..."
runFunc installTools runFunc installTools
runFunc generateCode
runFunc runLint runFunc runLint
runFunc runUnitTests runFunc runUnitTests
runFunc testExamplesAgainstLatestKustomizeRelease runFunc testExamplesAgainstLatestKustomizeRelease