|
| 1 | +# Variables |
| 2 | +APP_NAME := tmcg |
| 3 | +SRC_DIR := ./cmd/tmcg |
| 4 | +BUILD_DIR := ./bin |
| 5 | +TERRAFORM_DIR := ./terraform |
| 6 | +GOFLAGS := -v |
| 7 | +PLATFORMS := linux/amd64 linux/arm64 windows/amd64 windows/arm64 darwin/amd64 darwin/arm64 |
| 8 | +OUTPUT_DIR := build |
| 9 | +VERSION := $(shell git describe --tags --always --dirty) |
| 10 | +COMMIT := $(shell git rev-parse --short HEAD) |
| 11 | +BUILD_DATE := $(shell date -u +%Y-%m-%dT%H:%M:%SZ) |
| 12 | + |
| 13 | +# Default target is to build the app |
| 14 | +.PHONY: all |
| 15 | +all: clean build |
| 16 | + |
| 17 | +# Build target |
| 18 | +.PHONY: build |
| 19 | +build: |
| 20 | + @echo "Building the application..." |
| 21 | + @mkdir -p $(BUILD_DIR) |
| 22 | + mkdir -p $(OUTPUT_DIR) |
| 23 | + for platform in $(PLATFORMS); do \ |
| 24 | + EXT=""; \ |
| 25 | + if [ $${platform%/*} = "windows" ]; then EXT=".exe"; fi; \ |
| 26 | + GOOS=$${platform%/*} GOARCH=$${platform#*/} go build -ldflags="-X 'main.version=$(VERSION)' -X 'main.commit=$(COMMIT)' -X 'main.buildDate=$(BUILD_DATE)'" -o $(OUTPUT_DIR)/$(APP_NAME)-$${platform%/*}-$${platform#*/}$$EXT $(SRC_DIR); \ |
| 27 | + done |
| 28 | + |
| 29 | +# Clean target to remove build artifacts and the terraform directory |
| 30 | +.PHONY: clean |
| 31 | +clean: |
| 32 | + @echo "Cleaning build artifacts..." |
| 33 | + @if [ -d $(BUILD_DIR) ]; then rm -vrf $(BUILD_DIR); fi |
| 34 | + @if [ -d $(TERRAFORM_DIR) ]; then rm -vrf $(TERRAFORM_DIR); fi |
| 35 | + @if [ -d $(OUTPUT_DIR) ]; then rm -vrf $(OUTPUT_DIR); fi |
| 36 | + |
| 37 | +# Run target to run the built binary |
| 38 | +.PHONY: run |
| 39 | +run: build |
| 40 | + @echo "Running the application..." |
| 41 | + $(BUILD_DIR)/$(APP_NAME) |
| 42 | + |
| 43 | +# Test target to run unit tests |
| 44 | +.PHONY: test |
| 45 | +test: |
| 46 | + @echo "Running tests..." |
| 47 | + gotestsum --format=short-verbose --junitfile=unit-test-report.xml ./... |
| 48 | + |
| 49 | +# Lint target to check code with golangci-lint |
| 50 | +.PHONY: lint |
| 51 | +lint: |
| 52 | + @echo "Linting code..." |
| 53 | + @golangci-lint run ./... |
| 54 | + |
| 55 | +# Format target to ensure code style |
| 56 | +.PHONY: fmt |
| 57 | +fmt: |
| 58 | + @echo "Formatting code..." |
| 59 | + @go fmt ./... |
| 60 | + |
| 61 | +# Help target to list available commands |
| 62 | +.PHONY: help |
| 63 | +help: |
| 64 | + @echo "Makefile targets:" |
| 65 | + @echo " all - Build the application (default)." |
| 66 | + @echo " build - Build the application." |
| 67 | + @echo " clean - Clean the build artifacts and terraform directory." |
| 68 | + @echo " run - Run the application." |
| 69 | + @echo " test - Run tests." |
| 70 | + @echo " lint - Run linter." |
| 71 | + @echo " fmt - Format code." |
| 72 | + @echo " help - Show this help message." |
| 73 | + |
0 commit comments