Debian Codegraph @

Makefile

raw
1 # Git remote for pushing tags
2 REMOTE ?= origin
3
4 # Version for release tagging (required for tag/release targets)
5 RELEASE_VERSION ?=
6
7 # Convenience
8 GO ?= go
9 GOLANGCI ?= golangci-lint
10 GORELEASER?= goreleaser
11
12 .PHONY: help \
13 test race bench fmt tidy lint check \
14 ensure-clean ensure-release-version tag tag-delete \
15 release release-dry
16
17 help:
18 @echo "Targets:"
19 @echo " fmt - gofmt + go fmt"
20 @echo " tidy - go mod tidy"
21 @echo " test - go test ./..."
22 @echo " race - go test -race ./..."
23 @echo " bench - go test -bench=. ./..."
24 @echo " lint - golangci-lint run ./... (if installed)"
25 @echo " check - fmt + tidy + test + race"
26 @echo ""
27 @echo "Release targets:"
28 @echo " tag - Create annotated tag RELEASE_VERSION and push"
29 @echo " tag-delete - Delete tag RELEASE_VERSION locally + remote"
30 @echo " release - tag + goreleaser release --clean (if you use goreleaser)"
31 @echo " release-dry - tag + goreleaser release --clean --skip=publish"
32 @echo ""
33 @echo "Usage:"
34 @echo " make check"
35 @echo " make tag RELEASE_VERSION=v0.1.2"
36 @echo " make release RELEASE_VERSION=v0.1.2"
37
38 fmt:
39 @echo "Formatting..."
40 gofmt -w -s .
41 $(GO) fmt ./...
42
43 tidy:
44 @echo "Tidying..."
45 $(GO) mod tidy
46
47 test:
48 @echo "Testing..."
49 $(GO) test ./... -count=1
50
51 race:
52 @echo "Race testing..."
53 $(GO) test ./... -race -count=1
54
55 bench:
56 @echo "Bench..."
57 $(GO) test ./... -bench=. -run=^$$
58
59 lint:
60 @echo "Linting..."
61 @command -v $(GOLANGCI) >/dev/null 2>&1 || { echo "golangci-lint not found"; exit 1; }
62 $(GOLANGCI) run ./...
63
64 check: fmt tidy test race
65
66 # --------------------------
67 # Release helpers
68 # --------------------------
69
70 ensure-clean:
71 @echo "Checking git working tree..."
72 @git diff --quiet || (echo "Error: tracked changes exist. Commit/stash them."; exit 1)
73 @test -z "$$(git status --porcelain)" || (echo "Error: uncommitted/untracked files:"; git status --porcelain; exit 1)
74 @echo "OK: working tree clean"
75
76 ensure-release-version:
77 @test -n "$(RELEASE_VERSION)" || (echo "Error: set RELEASE_VERSION, e.g. make tag RELEASE_VERSION=v0.1.2"; exit 1)
78
79 tag: ensure-clean ensure-release-version
80 @if git rev-parse "$(RELEASE_VERSION)" >/dev/null 2>&1; then \
81 echo "Error: tag $(RELEASE_VERSION) already exists. Bump version."; \
82 exit 1; \
83 fi
84 @echo "Tagging $(RELEASE_VERSION) at HEAD $$(git rev-parse --short HEAD)"
85 @git tag -a $(RELEASE_VERSION) -m "$(RELEASE_VERSION)"
86 @git push $(REMOTE) $(RELEASE_VERSION)
87
88 tag-delete: ensure-release-version
89 @echo "Deleting tag $(RELEASE_VERSION) locally + remote..."
90 @git tag -d $(RELEASE_VERSION) 2>/dev/null || true
91 @git push $(REMOTE) :refs/tags/$(RELEASE_VERSION) || true
92
93 release: tag
94 @command -v $(GORELEASER) >/dev/null 2>&1 || { echo "goreleaser not found"; exit 1; }
95 $(GORELEASER) release --clean
96
97 release-dry: tag
98 @command -v $(GORELEASER) >/dev/null 2>&1 || { echo "goreleaser not found"; exit 1; }
99 $(GORELEASER) release --clean --skip=publish