|
| 1 | +PROJECT_FULL_NAME := controller-utils |
| 2 | +REPO_ROOT := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) |
| 3 | +EFFECTIVE_VERSION := $(shell $(REPO_ROOT)/hack/get-version.sh) |
| 4 | + |
| 5 | +# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) |
| 6 | +ifeq (,$(shell go env GOBIN)) |
| 7 | +GOBIN=$(shell go env GOPATH)/bin |
| 8 | +else |
| 9 | +GOBIN=$(shell go env GOBIN) |
| 10 | +endif |
| 11 | + |
| 12 | +# CONTAINER_TOOL defines the container tool to be used for building images. |
| 13 | +# Be aware that the target commands are only tested with Docker which is |
| 14 | +# scaffolded by default. However, you might want to replace it to use other |
| 15 | +# tools. (i.e. podman) |
| 16 | +CONTAINER_TOOL ?= docker |
| 17 | + |
| 18 | +# Setting SHELL to bash allows bash commands to be executed by recipes. |
| 19 | +# Options are set to exit when a recipe line exits non-zero or a piped command fails. |
| 20 | +SHELL = /usr/bin/env bash -o pipefail |
| 21 | +.SHELLFLAGS = -ec |
| 22 | + |
| 23 | +ROOT_CODE_DIRS := $(REPO_ROOT)/pkg/... |
| 24 | + |
| 25 | +##@ General |
| 26 | + |
| 27 | +# The help target prints out all targets with their descriptions organized |
| 28 | +# beneath their categories. The categories are represented by '##@' and the |
| 29 | +# target descriptions by '##'. The awk commands is responsible for reading the |
| 30 | +# entire set of makefiles included in this invocation, looking for lines of the |
| 31 | +# file as xyz: ## something, and then pretty-format the target and help. Then, |
| 32 | +# if there's a line with ##@ something, that gets pretty-printed as a category. |
| 33 | +# More info on the usage of ANSI control characters for terminal formatting: |
| 34 | +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters |
| 35 | +# More info on the awk command: |
| 36 | +# http://linuxcommand.org/lc3_adv_awk.php |
| 37 | + |
| 38 | +.PHONY: help |
| 39 | +help: ## Display this help. |
| 40 | + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) |
| 41 | + |
| 42 | +##@ Development |
| 43 | + |
| 44 | +.PHONY: tidy |
| 45 | +tidy: ## Runs 'go mod tidy' for all modules in this repo. |
| 46 | + @$(REPO_ROOT)/hack/tidy.sh |
| 47 | + |
| 48 | +.PHONY: format |
| 49 | +format: goimports ## Formats the imports. |
| 50 | + @FORMATTER=$(FORMATTER) $(REPO_ROOT)/hack/format.sh $(ROOT_CODE_DIRS) |
| 51 | + |
| 52 | +.PHONY: verify |
| 53 | +verify: golangci-lint goimports ## Runs linter, 'go vet', and checks if the formatter has been run. |
| 54 | + @( echo "> Verifying root module ..." && \ |
| 55 | + pushd $(REPO_ROOT) &>/dev/null && \ |
| 56 | + go vet $(ROOT_CODE_DIRS) && \ |
| 57 | + $(LINTER) run -c $(REPO_ROOT)/.golangci.yaml $(ROOT_CODE_DIRS) && \ |
| 58 | + popd &>/dev/null ) |
| 59 | + @test "$(SKIP_FORMATTING_CHECK)" = "true" || \ |
| 60 | + ( echo "> Checking for unformatted files ..." && \ |
| 61 | + FORMATTER=$(FORMATTER) $(REPO_ROOT)/hack/format.sh --verify $(ROOT_CODE_DIRS) ) |
| 62 | + |
| 63 | +.PHONY: test |
| 64 | +test: ## Run tests. |
| 65 | + go test $(ROOT_CODE_DIRS) -coverprofile cover.out |
| 66 | + go tool cover --html=cover.out -o cover.html |
| 67 | + go tool cover -func cover.out | tail -n 1 |
| 68 | + |
| 69 | +##@ Release |
| 70 | + |
| 71 | +.PHONY: prepare-release |
| 72 | +prepare-release: tidy format verify test |
| 73 | + |
| 74 | +.PHONY: release-major |
| 75 | +release-major: prepare-release ## Creates a new major release. |
| 76 | + @$(REPO_ROOT)/hack/release.sh major |
| 77 | + |
| 78 | +.PHONY: release-minor |
| 79 | +release-minor: prepare-release ## Creates a new minor release. |
| 80 | + @$(REPO_ROOT)/hack/release.sh minor |
| 81 | + |
| 82 | +.PHONY: release-patch |
| 83 | +release-patch: prepare-release ## Creates a new patch release. |
| 84 | + @$(REPO_ROOT)/hack/release.sh patch |
| 85 | + |
| 86 | +##@ Build Dependencies |
| 87 | + |
| 88 | +## Location to install dependencies to |
| 89 | +LOCALBIN ?= $(REPO_ROOT)/bin |
| 90 | + |
| 91 | +## Tool Binaries |
| 92 | +FORMATTER ?= $(LOCALBIN)/goimports |
| 93 | +LINTER ?= $(LOCALBIN)/golangci-lint |
| 94 | + |
| 95 | +## Tool Versions |
| 96 | +FORMATTER_VERSION ?= v0.22.0 |
| 97 | +LINTER_VERSION ?= 1.61.0 |
| 98 | + |
| 99 | +.PHONY: localbin |
| 100 | +localbin: |
| 101 | + @test -d $(LOCALBIN) || mkdir -p $(LOCALBIN) |
| 102 | + |
| 103 | +.PHONY: goimports |
| 104 | +goimports: localbin ## Download goimports locally if necessary. If wrong version is installed, it will be overwritten. |
| 105 | + @test -s $(FORMATTER) && test -s ./hack/goimports_version && cat ./hack/goimports_version | grep -q $(FORMATTER_VERSION) || \ |
| 106 | + ( echo "Installing goimports $(FORMATTER_VERSION) ..."; \ |
| 107 | + GOBIN=$(LOCALBIN) go install golang.org/x/tools/cmd/goimports@$(FORMATTER_VERSION) && \ |
| 108 | + echo $(FORMATTER_VERSION) > ./hack/goimports_version ) |
| 109 | + |
| 110 | +.PHONY: golangci-lint |
| 111 | +golangci-lint: localbin ## Download golangci-lint locally if necessary. If wrong version is installed, it will be overwritten. |
| 112 | + @test -s $(LINTER) && $(LINTER) --version | grep -q $(LINTER_VERSION) || \ |
| 113 | + ( echo "Installing golangci-lint $(LINTER_VERSION) ..."; \ |
| 114 | + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LOCALBIN) v$(LINTER_VERSION) ) |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +## Location to install dependencies to |
| 121 | +LOCALBIN ?= $(shell pwd)/bin |
| 122 | +$(LOCALBIN): |
| 123 | + mkdir -p $(LOCALBIN) |
| 124 | + |
| 125 | +CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen |
| 126 | +CONTROLLER_TOOLS_VERSION ?= v0.15.0 |
| 127 | + |
| 128 | +.PHONY: generate |
| 129 | +generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. |
| 130 | + $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." |
| 131 | + |
| 132 | +.PHONY: controller-gen |
| 133 | +controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten. |
| 134 | +$(CONTROLLER_GEN): $(LOCALBIN) |
| 135 | + test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \ |
| 136 | + GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) |
0 commit comments