|
| 1 | +# Makefile for github-org-repos-sync |
| 2 | + |
| 3 | +# Variables |
| 4 | +BINARY_NAME := github-org-repos-sync |
| 5 | +MODULE_NAME := github.com/xbglowx/github-org-repos-sync |
| 6 | +VERSION_PACKAGE := $(MODULE_NAME)/cmd |
| 7 | +BUILD_DIR := ./bin |
| 8 | + |
| 9 | +# Version detection - use git tags if available, otherwise use short git SHA for source builds |
| 10 | +GIT_SHA := $(shell git rev-parse --short HEAD 2>/dev/null) |
| 11 | +GIT_TAG := $(shell git describe --tags --exact-match 2>/dev/null) |
| 12 | +# Dirty detection: Repository is "clean" only if ALL conditions are true: |
| 13 | +# 1. No unstaged changes (git diff --quiet returns 0) |
| 14 | +# 2. No staged changes (git diff --quiet --cached returns 0) |
| 15 | +# 3. No untracked files (git status --porcelain is empty) |
| 16 | +# If ANY condition fails, the && chain fails and executes || echo "-dirty" |
| 17 | +GIT_DIRTY := $(shell git diff --quiet 2>/dev/null && git diff --quiet --cached 2>/dev/null && [ -z "$$(git status --porcelain 2>/dev/null)" ] || echo "-dirty") |
| 18 | + |
| 19 | +# If we have an exact tag match, use it; otherwise use git SHA for source builds |
| 20 | +ifdef GIT_TAG |
| 21 | + VERSION := $(GIT_TAG)$(GIT_DIRTY) |
| 22 | +else ifdef GIT_SHA |
| 23 | + VERSION := $(GIT_SHA)$(GIT_DIRTY) |
| 24 | +else |
| 25 | + VERSION := dev-unknown |
| 26 | +endif |
| 27 | + |
| 28 | +# Go build flags |
| 29 | +LDFLAGS := -X $(VERSION_PACKAGE).Version=$(VERSION) |
| 30 | +BUILD_FLAGS := -ldflags "$(LDFLAGS)" |
| 31 | + |
| 32 | +# Platform detection for local builds |
| 33 | +GOOS := $(shell go env GOOS) |
| 34 | +GOARCH := $(shell go env GOARCH) |
| 35 | +GO_VERSION := $(shell go version) |
| 36 | + |
| 37 | +# Cross-compilation targets |
| 38 | +PLATFORMS := \ |
| 39 | + linux/amd64 \ |
| 40 | + linux/arm64 \ |
| 41 | + darwin/amd64 \ |
| 42 | + darwin/arm64 \ |
| 43 | + windows/amd64 \ |
| 44 | + windows/arm64 |
| 45 | + |
| 46 | +.PHONY: help build build-all test version info clean |
| 47 | + |
| 48 | +# Default target |
| 49 | +all: test build |
| 50 | + |
| 51 | +# Help target |
| 52 | +help: |
| 53 | + @printf "%s\n" \ |
| 54 | + "Available targets:" \ |
| 55 | + " build - Build binary for current platform" \ |
| 56 | + " build-all - Build binaries for all platforms" \ |
| 57 | + " test - Run tests" \ |
| 58 | + " clean - Clean build artifacts" \ |
| 59 | + " version - Show version that would be built" \ |
| 60 | + " info - Show build information" \ |
| 61 | + " help - Show this help message" |
| 62 | + |
| 63 | +# Show version |
| 64 | +version: |
| 65 | + @echo "Version: $(VERSION)" |
| 66 | + |
| 67 | +# Build for current platform |
| 68 | +build: |
| 69 | + @echo "Building $(BINARY_NAME) v$(VERSION) for $(GOOS)/$(GOARCH)..." |
| 70 | + @mkdir -p $(BUILD_DIR) |
| 71 | + go build $(BUILD_FLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) . |
| 72 | + |
| 73 | +# Build for all platforms |
| 74 | +build-all: |
| 75 | + @echo "Building $(BINARY_NAME) v$(VERSION) for all platforms..." |
| 76 | + @mkdir -p $(BUILD_DIR) |
| 77 | + @$(foreach platform,$(PLATFORMS), \ |
| 78 | + echo "Building for $(platform)..."; \ |
| 79 | + GOOS=$(word 1,$(subst /, ,$(platform))) \ |
| 80 | + GOARCH=$(word 2,$(subst /, ,$(platform))) \ |
| 81 | + go build $(BUILD_FLAGS) \ |
| 82 | + -o $(BUILD_DIR)/$(BINARY_NAME)-$(word 1,$(subst /, ,$(platform)))-$(word 2,$(subst /, ,$(platform)))$(if $(findstring windows,$(platform)),.exe) . && \ |
| 83 | + ) true |
| 84 | + |
| 85 | +# Run tests |
| 86 | +test: |
| 87 | + @echo "Running tests..." |
| 88 | + go test -v -race ./... |
| 89 | + |
| 90 | +# Clean build artifacts |
| 91 | +clean: |
| 92 | + @echo "Cleaning build artifacts..." |
| 93 | + rm -rf $(BUILD_DIR) |
| 94 | + |
| 95 | +# Print build information |
| 96 | +info: |
| 97 | + @printf "%s\n" \ |
| 98 | + "Build Information:" \ |
| 99 | + " Binary Name: $(BINARY_NAME)" \ |
| 100 | + " Module: $(MODULE_NAME)" \ |
| 101 | + " Version: $(VERSION)" \ |
| 102 | + " Go Version: $(GO_VERSION)" \ |
| 103 | + " Platform: $(GOOS)/$(GOARCH)" \ |
| 104 | + " Build Dir: $(BUILD_DIR)" |
0 commit comments