|
| 1 | +.PHONY: all build build-linux test test-race test-short lint vet fmt fmt-check \ |
| 2 | + clean deps install check ci coverage verify help |
| 3 | + |
| 4 | +REPO_PATH := github.com/projecteru2/cocoon |
| 5 | +REVISION := $(shell git rev-parse HEAD || echo unknown) |
| 6 | +BUILTAT := $(shell date +%Y-%m-%dT%H:%M:%S) |
| 7 | +VERSION := $(shell git describe --tags $(shell git rev-list --tags --max-count=1) 2>/dev/null || echo dev) |
| 8 | +GO_LDFLAGS ?= -X $(REPO_PATH)/version.REVISION=$(REVISION) \ |
| 9 | + -X $(REPO_PATH)/version.BUILTAT=$(BUILTAT) \ |
| 10 | + -X $(REPO_PATH)/version.VERSION=$(VERSION) |
| 11 | + |
| 12 | +ifneq ($(KEEP_SYMBOL), 1) |
| 13 | + GO_LDFLAGS += -s |
| 14 | +endif |
| 15 | + |
| 16 | +## Location to install dependencies to |
| 17 | +LOCALBIN ?= $(shell pwd)/bin |
| 18 | +$(LOCALBIN): |
| 19 | + mkdir -p $(LOCALBIN) |
| 20 | + |
| 21 | +## Tool versions |
| 22 | +GOLANGCILINT_VERSION ?= v2.9.0 |
| 23 | +GOLANGCILINT_ROOT := $(LOCALBIN)/golangci-lint-$(GOLANGCILINT_VERSION) |
| 24 | +GOLANGCILINT := $(GOLANGCILINT_ROOT)/golangci-lint |
| 25 | + |
| 26 | +GOFMT := $(LOCALBIN)/gofumpt |
| 27 | +GOIMPORTS := $(LOCALBIN)/goimports |
| 28 | + |
| 29 | +## Tool download targets |
| 30 | +.PHONY: golangci-lint |
| 31 | +golangci-lint: $(GOLANGCILINT) |
| 32 | +$(GOLANGCILINT): |
| 33 | + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOLANGCILINT_ROOT) $(GOLANGCILINT_VERSION) |
| 34 | + |
| 35 | +.PHONY: gofumpt |
| 36 | +gofumpt: $(GOFMT) |
| 37 | +$(GOFMT): | $(LOCALBIN) |
| 38 | + GOBIN=$(LOCALBIN) go install mvdan.cc/gofumpt@latest |
| 39 | + |
| 40 | +.PHONY: goimports |
| 41 | +goimports: $(GOIMPORTS) |
| 42 | +$(GOIMPORTS): | $(LOCALBIN) |
| 43 | + GOBIN=$(LOCALBIN) go install golang.org/x/tools/cmd/goimports@latest |
| 44 | + |
| 45 | +# --- Primary targets --- |
| 46 | + |
| 47 | +all: deps lint test build ## Run deps, lint, test, and build |
| 48 | + |
| 49 | +ci: fmt-check vet lint test build ## Run all CI checks |
| 50 | + |
| 51 | +verify: lint fmt-check ## Verify code is lint-clean and formatted |
| 52 | + @if ! git diff --quiet HEAD; then \ |
| 53 | + git diff; \ |
| 54 | + echo "files are out of date, run 'make fmt' and commit"; exit 1; \ |
| 55 | + fi |
| 56 | + |
| 57 | +# --- Dependencies --- |
| 58 | + |
| 59 | +deps: ## Tidy Go modules |
| 60 | + go mod tidy |
| 61 | + |
| 62 | +# --- Build --- |
| 63 | + |
| 64 | +build: ## Build cocoon binary |
| 65 | + CGO_ENABLED=0 go build -ldflags "$(GO_LDFLAGS)" -o cocoon . |
| 66 | + |
| 67 | +build-linux: ## Cross-compile for linux/amd64 |
| 68 | + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "$(GO_LDFLAGS)" -o cocoon-linux-amd64 . |
| 69 | + |
| 70 | +install: ## Install cocoon binary to GOPATH/bin |
| 71 | + go install -ldflags "$(GO_LDFLAGS)" . |
| 72 | + |
| 73 | +# --- Testing --- |
| 74 | + |
| 75 | +test: vet ## Run tests with race detection and coverage |
| 76 | + go test -race -timeout 120s -count=1 -cover -coverprofile=coverage.out ./... |
| 77 | + |
| 78 | +test-race: ## Run tests with race detector only |
| 79 | + go test -race -timeout 120s -count=1 ./... |
| 80 | + |
| 81 | +test-short: ## Run short tests (skip long-running tests) |
| 82 | + go test -short ./... |
| 83 | + |
| 84 | +coverage: test ## Generate and display coverage report |
| 85 | + go tool cover -func=coverage.out |
| 86 | + @echo "" |
| 87 | + @echo "To view HTML coverage report: go tool cover -html=coverage.out" |
| 88 | + |
| 89 | +# --- Code quality --- |
| 90 | + |
| 91 | +vet: ## Run go vet for all target platforms |
| 92 | + GOOS=linux GOARCH=amd64 go vet ./... |
| 93 | + GOOS=darwin GOARCH=amd64 go vet ./... |
| 94 | + |
| 95 | +lint: golangci-lint ## Run golangci-lint for all target platforms |
| 96 | + GOOS=linux GOARCH=amd64 $(GOLANGCILINT) run |
| 97 | + GOOS=darwin GOARCH=amd64 $(GOLANGCILINT) run |
| 98 | + |
| 99 | +fmt: gofumpt goimports ## Format code with gofumpt and goimports |
| 100 | + $(GOFMT) -l -w . |
| 101 | + $(GOIMPORTS) -l -w --local 'github.com/projecteru2/cocoon' . |
| 102 | + |
| 103 | +fmt-check: gofumpt goimports ## Check formatting (fails if files need formatting) |
| 104 | + @test -z "$$($(GOFMT) -l .)" || { echo "Files need formatting (gofumpt):"; $(GOFMT) -l .; exit 1; } |
| 105 | + @test -z "$$($(GOIMPORTS) -l .)" || { echo "Files need formatting (goimports):"; $(GOIMPORTS) -l .; exit 1; } |
| 106 | + |
| 107 | +check: vet lint test ## Run vet, lint, and test |
| 108 | + |
| 109 | +# --- Maintenance --- |
| 110 | + |
| 111 | +clean: ## Remove build artifacts, coverage files, and test cache |
| 112 | + rm -f cocoon cocoon-linux-* cocoon-darwin-* |
| 113 | + rm -rf bin/ dist/ |
| 114 | + rm -f coverage.out coverage.html coverage.txt |
| 115 | + go clean -testcache |
| 116 | + |
| 117 | +cloc: ## Count lines of code (requires cloc) |
| 118 | + cloc --exclude-dir=vendor,dist --exclude-ext=json . |
| 119 | + |
| 120 | +# --- Help --- |
| 121 | + |
| 122 | +help: ## Show this help message |
| 123 | + @echo "Cocoon Makefile targets:" |
| 124 | + @echo "" |
| 125 | + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ |
| 126 | + awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}' |
| 127 | + @echo "" |
0 commit comments