Provides a `gitlocal version` command to display the application's version, commit hash, and build date. Introduces a `Makefile` to automate builds, installations, and tests. The Makefile dynamically extracts version, commit, and date from git tags, commit hashes, and build timestamps, injecting this metadata into the Go binary via `ldflags`. Updates the `README.md` to document the new command and recommended build process using the Makefile.
44 lines
1.2 KiB
Makefile
44 lines
1.2 KiB
Makefile
.PHONY: build install test clean version
|
|
|
|
# Get version from git tag, fallback to dev
|
|
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
|
|
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
|
|
DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
# Ldflags to inject version information
|
|
LDFLAGS := -ldflags "-X git.membo.co.uk/dtomlinson/gitlocal/cmd.Version=$(VERSION) \
|
|
-X git.membo.co.uk/dtomlinson/gitlocal/cmd.Commit=$(COMMIT) \
|
|
-X git.membo.co.uk/dtomlinson/gitlocal/cmd.Date=$(DATE)"
|
|
|
|
# Build the binary
|
|
build:
|
|
@echo "Building gitlocal $(VERSION)..."
|
|
go build $(LDFLAGS) -o gitlocal .
|
|
|
|
# Install to $GOPATH/bin
|
|
install:
|
|
@echo "Installing gitlocal $(VERSION)..."
|
|
go install $(LDFLAGS) .
|
|
|
|
# Run tests
|
|
test:
|
|
@echo "Running tests..."
|
|
go test -v ./...
|
|
|
|
# Run tests with coverage
|
|
coverage:
|
|
@echo "Running tests with coverage..."
|
|
go test -coverprofile=coverage.out ./...
|
|
go tool cover -func=coverage.out
|
|
|
|
# Clean build artifacts
|
|
clean:
|
|
@echo "Cleaning..."
|
|
rm -f gitlocal coverage.out
|
|
|
|
# Show version that would be built
|
|
version:
|
|
@echo "Version: $(VERSION)"
|
|
@echo "Commit: $(COMMIT)"
|
|
@echo "Date: $(DATE)"
|