|
| 1 | +#/* |
| 2 | +# * Copyright 2024 The CNAI Authors |
| 3 | +# * |
| 4 | +# * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# * you may not use this file except in compliance with the License. |
| 6 | +# * You may obtain a copy of the License at |
| 7 | +# * |
| 8 | +# * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# * |
| 10 | +# * Unless required by applicable law or agreed to in writing, software |
| 11 | +# * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# * See the License for the specific language governing permissions and |
| 14 | +# * limitations under the License. |
| 15 | +# */ |
| 16 | + |
| 17 | +# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) |
| 18 | +ifeq (,$(shell go env GOBIN)) |
| 19 | +GOBIN=$(shell go env GOPATH)/bin |
| 20 | +else |
| 21 | +GOBIN=$(shell go env GOBIN) |
| 22 | +endif |
| 23 | +# Setting SHELL to bash allows bash commands to be executed by recipes. |
| 24 | +# Options are set to exit when a recipe line exits non-zero or a piped command fails. |
| 25 | +SHELL = /usr/bin/env bash -o pipefail |
| 26 | +.SHELLFLAGS = -ec |
| 27 | + |
| 28 | +.PHONY: all |
| 29 | +all: build |
| 30 | + |
| 31 | +##@ General |
| 32 | + |
| 33 | +# The help target prints out all targets with their descriptions organized |
| 34 | +# beneath their categories. The categories are represented by '##@' and the |
| 35 | +# target descriptions by '##'. The awk command is responsible for reading the |
| 36 | +# entire set of makefiles included in this invocation, looking for lines of the |
| 37 | +# file as xyz: ## something, and then pretty-format the target and help. Then, |
| 38 | +# if there's a line with ##@ something, that gets pretty-printed as a category. |
| 39 | +# More info on the usage of ANSI control characters for terminal formatting: |
| 40 | +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters |
| 41 | +# More info on the awk command: |
| 42 | +# http://linuxcommand.org/lc3_adv_awk.php |
| 43 | + |
| 44 | +.PHONY: help |
| 45 | +help: ## Display this help. |
| 46 | + @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) |
| 47 | + |
| 48 | +.PHONY: fmt |
| 49 | +fmt: ## Run go fmt against code. |
| 50 | + go fmt ./... |
| 51 | + |
| 52 | +.PHONY: vet |
| 53 | +vet: ## Run go vet against code. |
| 54 | + go vet ./... |
| 55 | + |
| 56 | +.PHONY: test |
| 57 | +test: fmt vet ## Run unit test and display the coverage. |
| 58 | + go test $$(go list ./pkg/...) -coverprofile cover.out |
| 59 | + go tool cover -func cover.out |
| 60 | + |
| 61 | +GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint |
| 62 | +GOLANGCI_LINT_VERSION ?= v1.54.2 |
| 63 | +golangci-lint: |
| 64 | + @[ -f $(GOLANGCI_LINT) ] || { \ |
| 65 | + set -e ;\ |
| 66 | + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $(GOLANGCI_LINT_VERSION) ;\ |
| 67 | + } |
| 68 | + |
| 69 | +.PHONY: lint |
| 70 | +lint: golangci-lint ## Run golangci-lint linter & yamllint |
| 71 | + $(GOLANGCI_LINT) run |
| 72 | + |
| 73 | +.PHONY: lint-fix |
| 74 | +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes |
| 75 | + |
| 76 | +GOIMPORTS := $(shell command -v goimports 2> /dev/null) |
| 77 | +ifeq ($(GOIMPORTS),) |
| 78 | +GOIMPORTS_INSTALL = go install golang.org/x/tools/cmd/goimports@latest |
| 79 | +else |
| 80 | +GOIMPORTS_INSTALL = |
| 81 | +endif |
| 82 | + |
| 83 | +$(GOIMPORTS_INSTALL): |
| 84 | + $(GOIMPORTS_INSTALL) |
| 85 | + |
| 86 | +lint-fix: $(GOIMPORTS_INSTALL) |
| 87 | + $(GOIMPORTS) -l -w . |
| 88 | + $(GOLANGCI_LINT) run --fix |
| 89 | + |
| 90 | +##@ Build |
| 91 | + |
| 92 | +.PHONY: build |
| 93 | +build: fmt vet |
| 94 | + go build -o output/modctl main.go |
| 95 | + |
| 96 | +## Location to install dependencies to |
| 97 | +LOCALBIN ?= $(shell pwd)/bin |
| 98 | +$(LOCALBIN): |
| 99 | + mkdir -p $(LOCALBIN) |
| 100 | + |
| 101 | +.PHONY: gen |
| 102 | +gen: gen-mockery## Generate all we need! |
| 103 | + |
| 104 | +.PHONY: gen-mockery check-mockery install-mockery |
| 105 | +gen-mockery: check-mockery ## Generate mockery code |
| 106 | + @echo "generating mockery code according to .mockery.yaml" |
| 107 | + @mockery |
| 108 | + |
| 109 | +check-mockery: |
| 110 | + @which mockery > /dev/null || { echo "mockery not found. Trying to install via Homebrew..."; $(MAKE) install-mockery; } |
| 111 | + @mockery --version | grep -q "2.46.3" || { echo "mockery version is not v2.46.3. Trying to install the correct version..."; $(MAKE) install-mockery; } |
| 112 | + |
| 113 | +install-mockery: |
| 114 | + @if command -v brew > /dev/null; then \ |
| 115 | + echo "Installing mockery via Homebrew"; \ |
| 116 | + brew install mockery; \ |
| 117 | + else \ |
| 118 | + echo "Error: Homebrew is not installed. Please install Homebrew first and ensure it's in your PATH."; \ |
| 119 | + exit 1; \ |
| 120 | + fi |
0 commit comments