diff --git a/Makefile b/Makefile index 0feabe1f..05bf09ef 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,14 @@ VER?=0.0.1 MODULES=$(shell find . -mindepth 2 -maxdepth 4 -type f -name 'go.mod' | cut -c 3- | sed 's|/[^/]*$$||' | sort -u | tr / :) targets=$(addprefix test-, $(MODULES)) +root_dir=$(dir $(abspath $(lastword $(MAKEFILE_LIST)))) + +# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) +ifeq (,$(shell go env GOBIN)) +GOBIN=$(shell go env GOPATH)/bin +else +GOBIN=$(shell go env GOBIN) +endif all: $(MAKE) $(targets) @@ -14,7 +22,10 @@ fmt-%: vet-%: cd $(subst :,/,$*); go vet ./... -test-%: tidy-% fmt-% vet-% +generate-%: controller-gen + cd $(subst :,/,$*); $(CONTROLLER_GEN) object:headerFile="$(root_dir)/hack/boilerplate.go.txt" paths="./..." + +test-%: generate-% tidy-% fmt-% vet-% cd $(subst :,/,$*); go test ./... -coverprofile cover.out release-%: @@ -24,3 +35,19 @@ release-%: git pull git tag "$(REL_PATH)/v$(VER)" git push origin "$(REL_PATH)/v$(VER)" + +# Find or download controller-gen +controller-gen: +ifeq (, $(shell which controller-gen)) + @{ \ + set -e ;\ + CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\ + cd $$CONTROLLER_GEN_TMP_DIR ;\ + go mod init tmp ;\ + go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0 ;\ + rm -rf $$CONTROLLER_GEN_TMP_DIR ;\ + } +CONTROLLER_GEN=$(GOBIN)/controller-gen +else +CONTROLLER_GEN=$(shell which controller-gen) +endif diff --git a/hack/boilerplate.go.txt b/hack/boilerplate.go.txt new file mode 100644 index 00000000..439ccd86 --- /dev/null +++ b/hack/boilerplate.go.txt @@ -0,0 +1,15 @@ +/* +Copyright 2021 The Flux authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/