|
1 | | -BINARY=flashduty |
2 | | -VERSION=$(shell git describe --tags --always --dirty) |
3 | | -COMMIT=$(shell git rev-parse --short HEAD) |
4 | | -DATE=$(shell date -u +%Y-%m-%dT%H:%M:%SZ) |
5 | | -LDFLAGS=-ldflags "-s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(DATE)" |
| 1 | +# Build configuration |
| 2 | +BINARY_NAME := flashduty |
| 3 | +BUILD_DIR := bin |
| 4 | +GOLANGCI_LINT_VERSION := v2.2.1 |
| 5 | +GOLANGCI_LINT := $(BUILD_DIR)/golangci-lint |
| 6 | +GCI_VERSION := v0.13.5 |
| 7 | +GCI := $(BUILD_DIR)/gci |
6 | 8 |
|
7 | | -.PHONY: build check lint test clean |
| 9 | +# Go parameters |
| 10 | +GOCMD := go |
| 11 | +GOBUILD := $(GOCMD) build |
| 12 | +GOTEST := $(GOCMD) test |
| 13 | +GOFMT := gofmt |
| 14 | +MODULE := $(shell go list -m) |
8 | 15 |
|
9 | | -build: |
10 | | - go build $(LDFLAGS) -o bin/$(BINARY) ./cmd/flashduty |
| 16 | +# Build metadata |
| 17 | +VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev") |
| 18 | +COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "none") |
| 19 | +DATE := $(shell date -u +%Y-%m-%dT%H:%M:%SZ) |
| 20 | +LDFLAGS := -ldflags "-s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(DATE)" |
11 | 21 |
|
12 | | -check: lint test build |
| 22 | +# Default target |
| 23 | +.PHONY: all |
| 24 | +all: check |
13 | 25 |
|
14 | | -lint: |
15 | | - golangci-lint run ./... |
| 26 | +# ============================================================================ |
| 27 | +# Development targets |
| 28 | +# ============================================================================ |
16 | 29 |
|
17 | | -test: |
18 | | - go test -race -cover ./... |
| 30 | +.PHONY: build |
| 31 | +build: ## Build the binary |
| 32 | + $(GOBUILD) -v $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/flashduty |
19 | 33 |
|
20 | | -clean: |
21 | | - rm -rf bin/ |
| 34 | +.PHONY: run |
| 35 | +run: build ## Build and run the CLI |
| 36 | + ./$(BUILD_DIR)/$(BINARY_NAME) |
| 37 | + |
| 38 | +# ============================================================================ |
| 39 | +# Quality assurance targets |
| 40 | +# ============================================================================ |
| 41 | + |
| 42 | +FMT_DIRS := cmd internal |
| 43 | + |
| 44 | +.PHONY: fmt |
| 45 | +fmt: $(GCI) ## Format Go source code and sort imports |
| 46 | + $(GOFMT) -s -w $(FMT_DIRS) |
| 47 | + $(GCI) write --skip-generated -s standard -s default -s "prefix($(MODULE))" $(FMT_DIRS) |
| 48 | + |
| 49 | +.PHONY: gci |
| 50 | +gci: $(GCI) ## Sort imports using gci |
| 51 | + $(GCI) write --skip-generated -s standard -s default -s "prefix($(MODULE))" $(FMT_DIRS) |
| 52 | + |
| 53 | +.PHONY: lint |
| 54 | +lint: $(GOLANGCI_LINT) ## Run golangci-lint |
| 55 | + $(GOLANGCI_LINT) run |
| 56 | + |
| 57 | +.PHONY: lint-fix |
| 58 | +lint-fix: $(GOLANGCI_LINT) ## Run golangci-lint with auto-fix |
| 59 | + $(GOLANGCI_LINT) run --fix |
| 60 | + |
| 61 | +.PHONY: test |
| 62 | +test: ## Run unit tests |
| 63 | + $(GOTEST) -race ./... |
| 64 | + |
| 65 | +.PHONY: test-v |
| 66 | +test-v: ## Run unit tests with verbose output |
| 67 | + $(GOTEST) -race -v ./... |
| 68 | + |
| 69 | +.PHONY: test-cover |
| 70 | +test-cover: ## Run unit tests with coverage |
| 71 | + $(GOTEST) -race -cover ./... |
| 72 | + |
| 73 | +# ============================================================================ |
| 74 | +# Pre-push check (recommended before pushing) |
| 75 | +# ============================================================================ |
| 76 | + |
| 77 | +.PHONY: check |
| 78 | +check: fmt lint test build ## Run all checks (fmt, lint, test, build) - recommended before pushing |
| 79 | + |
| 80 | +.PHONY: ci |
| 81 | +ci: check ## Alias for check |
| 82 | + |
| 83 | +# ============================================================================ |
| 84 | +# Dependency management |
| 85 | +# ============================================================================ |
| 86 | + |
| 87 | +.PHONY: deps |
| 88 | +deps: ## Download Go dependencies |
| 89 | + $(GOCMD) mod download |
| 90 | + |
| 91 | +.PHONY: deps-tidy |
| 92 | +deps-tidy: ## Tidy Go modules |
| 93 | + $(GOCMD) mod tidy |
| 94 | + |
| 95 | +.PHONY: deps-verify |
| 96 | +deps-verify: ## Verify Go dependencies |
| 97 | + $(GOCMD) mod verify |
| 98 | + |
| 99 | +# ============================================================================ |
| 100 | +# Tools installation |
| 101 | +# ============================================================================ |
| 102 | + |
| 103 | +$(BUILD_DIR): |
| 104 | + mkdir -p $(BUILD_DIR) |
| 105 | + |
| 106 | +$(GOLANGCI_LINT): $(BUILD_DIR) |
| 107 | + @if [ ! -f "$(GOLANGCI_LINT)" ]; then \ |
| 108 | + echo "Installing golangci-lint $(GOLANGCI_LINT_VERSION)..."; \ |
| 109 | + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s $(GOLANGCI_LINT_VERSION); \ |
| 110 | + fi |
| 111 | + |
| 112 | +$(GCI): $(BUILD_DIR) |
| 113 | + @if [ ! -f "$(GCI)" ]; then \ |
| 114 | + echo "Installing gci $(GCI_VERSION)..."; \ |
| 115 | + GOBIN=$(CURDIR)/$(BUILD_DIR) $(GOCMD) install github.com/daixiang0/gci@$(GCI_VERSION); \ |
| 116 | + fi |
| 117 | + |
| 118 | +.PHONY: tools |
| 119 | +tools: $(GOLANGCI_LINT) $(GCI) ## Install required tools |
| 120 | + |
| 121 | +# ============================================================================ |
| 122 | +# Cleanup |
| 123 | +# ============================================================================ |
| 124 | + |
| 125 | +.PHONY: clean |
| 126 | +clean: ## Remove build artifacts |
| 127 | + rm -rf $(BUILD_DIR) |
| 128 | + |
| 129 | +# ============================================================================ |
| 130 | +# Help |
| 131 | +# ============================================================================ |
| 132 | + |
| 133 | +.PHONY: help |
| 134 | +help: ## Display this help message |
| 135 | + @echo "Available targets:" |
| 136 | + @echo "" |
| 137 | + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}' |
| 138 | + @echo "" |
| 139 | + @echo "Quick start:" |
| 140 | + @echo " make check - Run all pre-push checks (recommended before pushing)" |
| 141 | + @echo " make lint - Run linter only" |
| 142 | + @echo " make test - Run unit tests only" |
0 commit comments