|
| 1 | +# kspec Makefile |
| 2 | +# Run `make help` for available commands |
| 3 | + |
| 4 | +.PHONY: help build install clean test lint lint-fix fmt vet check \ |
| 5 | + run dev release snapshot hooks deps tidy |
| 6 | + |
| 7 | +# Build variables |
| 8 | +BINARY_NAME := kspec |
| 9 | +BUILD_DIR := ./bin |
| 10 | +MAIN_PKG := ./cmd/kspec |
| 11 | +VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev") |
| 12 | +COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "none") |
| 13 | +DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 14 | +LDFLAGS := -s -w \ |
| 15 | + -X main.version=$(VERSION) \ |
| 16 | + -X main.commit=$(COMMIT) \ |
| 17 | + -X main.date=$(DATE) |
| 18 | + |
| 19 | +# Go variables |
| 20 | +GOBIN := $(shell go env GOBIN) |
| 21 | +ifeq ($(GOBIN),) |
| 22 | +GOBIN := $(shell go env GOPATH)/bin |
| 23 | +endif |
| 24 | + |
| 25 | +# Colors |
| 26 | +BLUE := \033[34m |
| 27 | +GREEN := \033[32m |
| 28 | +YELLOW := \033[33m |
| 29 | +RED := \033[31m |
| 30 | +RESET := \033[0m |
| 31 | + |
| 32 | +##@ General |
| 33 | + |
| 34 | +help: ## Display this help |
| 35 | + @awk 'BEGIN {FS = ":.*##"; printf "\n$(BLUE)Usage:$(RESET)\n make $(GREEN)<target>$(RESET)\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " $(GREEN)%-15s$(RESET) %s\n", $$1, $$2 } /^##@/ { printf "\n$(YELLOW)%s$(RESET)\n", substr($$0, 5) } ' $(MAKEFILE_LIST) |
| 36 | + |
| 37 | +##@ Development |
| 38 | + |
| 39 | +build: ## Build the binary |
| 40 | + @echo "$(BLUE)Building $(BINARY_NAME)...$(RESET)" |
| 41 | + @mkdir -p $(BUILD_DIR) |
| 42 | + CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PKG) |
| 43 | + @echo "$(GREEN)Built $(BUILD_DIR)/$(BINARY_NAME)$(RESET)" |
| 44 | + |
| 45 | +install: build ## Install binary to GOBIN |
| 46 | + @echo "$(BLUE)Installing $(BINARY_NAME) to $(GOBIN)...$(RESET)" |
| 47 | + @cp $(BUILD_DIR)/$(BINARY_NAME) $(GOBIN)/$(BINARY_NAME) |
| 48 | + @echo "$(GREEN)Installed to $(GOBIN)/$(BINARY_NAME)$(RESET)" |
| 49 | + |
| 50 | +dev: ## Build and run with dev version |
| 51 | + @go run -ldflags="$(LDFLAGS)" $(MAIN_PKG) $(ARGS) |
| 52 | + |
| 53 | +run: build ## Build and run the binary |
| 54 | + @$(BUILD_DIR)/$(BINARY_NAME) $(ARGS) |
| 55 | + |
| 56 | +clean: ## Remove build artifacts |
| 57 | + @echo "$(BLUE)Cleaning...$(RESET)" |
| 58 | + @rm -rf $(BUILD_DIR) |
| 59 | + @rm -f coverage.out coverage.html |
| 60 | + @echo "$(GREEN)Cleaned$(RESET)" |
| 61 | + |
| 62 | +##@ Testing |
| 63 | + |
| 64 | +test: ## Run tests |
| 65 | + @echo "$(BLUE)Running tests...$(RESET)" |
| 66 | + go test -v -race ./... |
| 67 | + |
| 68 | +test-coverage: ## Run tests with coverage |
| 69 | + @echo "$(BLUE)Running tests with coverage...$(RESET)" |
| 70 | + go test -v -race -coverprofile=coverage.out ./... |
| 71 | + go tool cover -html=coverage.out -o coverage.html |
| 72 | + @echo "$(GREEN)Coverage report: coverage.html$(RESET)" |
| 73 | + |
| 74 | +test-short: ## Run short tests only |
| 75 | + go test -v -short ./... |
| 76 | + |
| 77 | +##@ Code Quality |
| 78 | + |
| 79 | +lint: ## Run linter |
| 80 | + @echo "$(BLUE)Running linter...$(RESET)" |
| 81 | + @if command -v golangci-lint >/dev/null 2>&1; then \ |
| 82 | + golangci-lint run --timeout=5m; \ |
| 83 | + else \ |
| 84 | + echo "$(RED)golangci-lint not installed. Run: make lint-install$(RESET)"; \ |
| 85 | + exit 1; \ |
| 86 | + fi |
| 87 | + |
| 88 | +lint-fix: ## Run linter with auto-fix |
| 89 | + @echo "$(BLUE)Running linter with fixes...$(RESET)" |
| 90 | + @if command -v golangci-lint >/dev/null 2>&1; then \ |
| 91 | + golangci-lint run --fix --timeout=5m; \ |
| 92 | + else \ |
| 93 | + echo "$(RED)golangci-lint not installed. Run: make lint-install$(RESET)"; \ |
| 94 | + exit 1; \ |
| 95 | + fi |
| 96 | + |
| 97 | +lint-install: ## Install golangci-lint |
| 98 | + @echo "$(BLUE)Installing golangci-lint...$(RESET)" |
| 99 | + go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest |
| 100 | + @echo "$(GREEN)Installed golangci-lint$(RESET)" |
| 101 | + |
| 102 | +fmt: ## Format code |
| 103 | + @echo "$(BLUE)Formatting code...$(RESET)" |
| 104 | + go fmt ./... |
| 105 | + @echo "$(GREEN)Done$(RESET)" |
| 106 | + |
| 107 | +vet: ## Run go vet |
| 108 | + @echo "$(BLUE)Running go vet...$(RESET)" |
| 109 | + go vet ./... |
| 110 | + |
| 111 | +check: fmt vet lint test ## Run all checks (fmt, vet, lint, test) |
| 112 | + @echo "$(GREEN)All checks passed!$(RESET)" |
| 113 | + |
| 114 | +##@ Dependencies |
| 115 | + |
| 116 | +deps: ## Download dependencies |
| 117 | + @echo "$(BLUE)Downloading dependencies...$(RESET)" |
| 118 | + go mod download |
| 119 | + |
| 120 | +tidy: ## Tidy and verify dependencies |
| 121 | + @echo "$(BLUE)Tidying dependencies...$(RESET)" |
| 122 | + go mod tidy |
| 123 | + go mod verify |
| 124 | + |
| 125 | +##@ Git Hooks |
| 126 | + |
| 127 | +hooks: ## Install git hooks via lefthook |
| 128 | + @echo "$(BLUE)Installing git hooks...$(RESET)" |
| 129 | + @if command -v lefthook >/dev/null 2>&1; then \ |
| 130 | + lefthook install; \ |
| 131 | + echo "$(GREEN)Git hooks installed$(RESET)"; \ |
| 132 | + else \ |
| 133 | + echo "$(RED)lefthook not installed. Run: make hooks-install$(RESET)"; \ |
| 134 | + exit 1; \ |
| 135 | + fi |
| 136 | + |
| 137 | +hooks-install: ## Install lefthook |
| 138 | + @echo "$(BLUE)Installing lefthook...$(RESET)" |
| 139 | + go install github.com/evilmartians/lefthook@latest |
| 140 | + @echo "$(GREEN)Installed lefthook$(RESET)" |
| 141 | + |
| 142 | +hooks-uninstall: ## Uninstall git hooks |
| 143 | + @if command -v lefthook >/dev/null 2>&1; then \ |
| 144 | + lefthook uninstall; \ |
| 145 | + echo "$(GREEN)Git hooks uninstalled$(RESET)"; \ |
| 146 | + fi |
| 147 | + |
| 148 | +##@ Release |
| 149 | + |
| 150 | +release: ## Create a release build (requires goreleaser) |
| 151 | + @echo "$(BLUE)Creating release...$(RESET)" |
| 152 | + goreleaser release --clean |
| 153 | + |
| 154 | +snapshot: ## Create a snapshot release (no publish) |
| 155 | + @echo "$(BLUE)Creating snapshot...$(RESET)" |
| 156 | + goreleaser release --snapshot --clean |
| 157 | + |
| 158 | +release-check: ## Validate goreleaser config |
| 159 | + @echo "$(BLUE)Checking goreleaser config...$(RESET)" |
| 160 | + goreleaser check |
| 161 | + |
| 162 | +##@ Docker (future) |
| 163 | + |
| 164 | +# docker-build: ## Build Docker image |
| 165 | +# docker build -t kspec:$(VERSION) . |
| 166 | + |
| 167 | +##@ Utilities |
| 168 | + |
| 169 | +version: ## Show version info |
| 170 | + @echo "Version: $(VERSION)" |
| 171 | + @echo "Commit: $(COMMIT)" |
| 172 | + @echo "Date: $(DATE)" |
| 173 | + |
| 174 | +tools: lint-install hooks-install ## Install all development tools |
| 175 | + @echo "$(GREEN)All tools installed$(RESET)" |
| 176 | + |
| 177 | +.DEFAULT_GOAL := help |
0 commit comments