You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.6 KiB
Makefile

# Variables
BINARY_NAME := ${REPO_NAME}
BUILD_DIR := ./build
DIST_DIR := ./dist
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "0.1.0-dev")
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
GIT_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
PKG_PATH=git.runcible.io/${REPO_OWNER}/${REPO_NAME}/cmd
# Attempt to get the latest tag and clean the 'v' prefix.
# If no tags exist, this command will produce an empty string, and `|| true` prevents make from erroring out.
CLEAN_TAG := $(shell git describe --tags --abbrev=0 2>/dev/null | sed 's/^v//' || true)
# Strict, Debian-compliant version for packaging.
# It tries to find the latest tag (e.g., v1.2.3) from CLEAN_TAG and uses that.
# If no tags exist, it defaults to "0.1.0-1", which is a valid starting version for a Debian package.
DEB_VERSION := $(if $(CLEAN_TAG),$(CLEAN_TAG),0.1.0-1)
DEB_PACKAGE_NAME := $(BINARY_NAME)_$(DEB_VERSION)_amd64.deb
# Default target
.PHONY: all
all: build
# Build the binary
.PHONY: build
build:
@echo "Building $(BINARY_NAME) version $(VERSION)..."
@mkdir -p $(BUILD_DIR)
@go build -ldflags "-X $(PKG_PATH).Version=$(VERSION) -X $(PKG_PATH).BuildTime=$(BUILD_TIME) -X $(PKG_PATH).GitCommit=$(GIT_COMMIT)" \
-o $(BUILD_DIR)/$(BINARY_NAME) .
# Clean build artifacts
.PHONY: clean
clean:
@echo "Cleaning build artifacts..."
@rm -rf $(BUILD_DIR)
@rm -rf $(DIST_DIR)
# Build Debian package
.PHONY: deb
deb: build
@echo "Building Debian package..."
@mkdir -p $(DIST_DIR)
@mkdir -p $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)
@mkdir -p $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)/DEBIAN
@mkdir -p $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)/usr/local/bin
# Copy binary
@cp $(BUILD_DIR)/$(BINARY_NAME) $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)/usr/local/bin/
@chmod +x $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)/usr/local/bin/$(BINARY_NAME)
# Create control file
@echo "Package: $(BINARY_NAME)" > $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)/DEBIAN/control
@echo "Version: $(DEB_VERSION)" >> $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)/DEBIAN/control
@echo "Architecture: amd64" >> $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)/DEBIAN/control
@echo "Maintainer: ${REPO_NAME} Maintainer <maintainer@example.com>" >> $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)/DEBIAN/control
@echo "Description: ${REPO_NAME} - A Go application" >> $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)/DEBIAN/control
@echo " ${REPO_NAME} is a Go application that provides various functionalities." >> $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)/DEBIAN/control
# Copy changelog if it exists
@if [ -f changelog ]; then \
cp changelog $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION)/DEBIAN/; \
fi
# Build the .deb package
@cd $(DIST_DIR) && dpkg-deb --build $(BINARY_NAME)_$(DEB_VERSION)
@mv $(DIST_DIR)/$(BINARY_NAME)_$(DEB_VERSION).deb $(DIST_DIR)/$(DEB_PACKAGE_NAME)
@echo "Debian package created: $(DIST_DIR)/$(DEB_PACKAGE_NAME)"
# Install dependencies
.PHONY: deps
deps:
@echo "Installing dependencies..."
@go mod download
# Run tests
.PHONY: test
test:
@echo "Running tests..."
@go test ./...
# Format code
.PHONY: fmt
fmt:
@echo "Formatting code..."
@go fmt ./...
# Lint code
.PHONY: lint
lint:
@echo "Linting code..."
@go vet ./...
# Show help
.PHONY: help
help:
@echo "Available targets:"
@echo " build - Build the binary in ./build directory"
@echo " deb - Build Debian package in ./dist directory"
@echo " clean - Clean build and dist directories"
@echo " deps - Install dependencies"
@echo " test - Run tests"
@echo " fmt - Format code"
@echo " lint - Lint code"
@echo " help - Show this help message"