|
| 1 | +GOMODNAME := $(shell grep 'module' go.mod | sed -e 's/^module //') |
| 2 | +SOURCES := $(shell find . -name "*.go" -or -name "go.mod" -or -name "go.sum" \ |
| 3 | + -or -name "Makefile") |
| 4 | + |
| 5 | +# Verbose output |
| 6 | +ifdef VERBOSE |
| 7 | +V = -v |
| 8 | +endif |
| 9 | + |
| 10 | +# |
| 11 | +# Environment |
| 12 | +# |
| 13 | + |
| 14 | +BINDIR := bin |
| 15 | +TOOLDIR := $(BINDIR)/tools |
| 16 | + |
| 17 | +# Global environment variables for all targets |
| 18 | +SHELL ?= /bin/bash |
| 19 | +SHELL := env \ |
| 20 | + GO111MODULE=on \ |
| 21 | + GOBIN=$(CURDIR)/$(TOOLDIR) \ |
| 22 | + CGO_ENABLED=1 \ |
| 23 | + PATH='$(CURDIR)/$(BINDIR):$(CURDIR)/$(TOOLDIR):$(PATH)' \ |
| 24 | + $(SHELL) |
| 25 | + |
| 26 | +# |
| 27 | +# Defaults |
| 28 | +# |
| 29 | + |
| 30 | +# Default target |
| 31 | +.DEFAULT_GOAL := test |
| 32 | + |
| 33 | +# |
| 34 | +# Tools |
| 35 | +# |
| 36 | + |
| 37 | +TOOLS += $(TOOLDIR)/gobin |
| 38 | +$(TOOLDIR)/gobin: |
| 39 | + GO111MODULE=off go get -u github.com/myitcv/gobin |
| 40 | + |
| 41 | +# external tool |
| 42 | +define tool # 1: binary-name, 2: go-import-path |
| 43 | +TOOLS += $(TOOLDIR)/$(1) |
| 44 | + |
| 45 | +$(TOOLDIR)/$(1): $(TOOLDIR)/gobin Makefile |
| 46 | + gobin $(V) "$(2)" |
| 47 | +endef |
| 48 | + |
| 49 | +$(eval $(call tool,godoc,golang.org/x/tools/cmd/godoc)) |
| 50 | +$(eval $(call tool,gofumpt,mvdan.cc/gofumpt)) |
| 51 | +$(eval $(call tool,goimports,golang.org/x/tools/cmd/goimports)) |
| 52 | +$(eval $(call tool,golangci-lint,github.com/golangci/golangci-lint/cmd/ [email protected])) |
| 53 | +$(eval $(call tool,gomod,github.com/Helcaraxan/gomod)) |
| 54 | + |
| 55 | +.PHONY: tools |
| 56 | +tools: $(TOOLS) |
| 57 | + |
| 58 | +# |
| 59 | +# Development |
| 60 | +# |
| 61 | + |
| 62 | +BENCH ?= . |
| 63 | +TESTARGS ?= |
| 64 | + |
| 65 | +.PHONY: clean |
| 66 | +clean: |
| 67 | + rm -f $(TOOLS) |
| 68 | + rm -f ./coverage.out ./go.mod.tidy-check ./go.sum.tidy-check |
| 69 | + |
| 70 | +.PHONY: test |
| 71 | +test: |
| 72 | + go test $(V) -count=1 -race $(TESTARGS) ./... |
| 73 | + |
| 74 | +.PHONY: test-deps |
| 75 | +test-deps: |
| 76 | + go test all |
| 77 | + |
| 78 | +.PHONY: lint |
| 79 | +lint: $(TOOLDIR)/golangci-lint |
| 80 | + golangci-lint $(V) run |
| 81 | + |
| 82 | +.PHONY: format |
| 83 | +format: $(TOOLDIR)/goimports $(TOOLDIR)/gofumpt |
| 84 | + goimports -w . && gofumpt -w . |
| 85 | + |
| 86 | +.SILENT: bench |
| 87 | +.PHONY: bench |
| 88 | +bench: |
| 89 | + go test $(V) -count=1 -bench=$(BENCH) $(TESTARGS) ./... |
| 90 | + |
| 91 | +.PHONY: golden-update |
| 92 | +golden-update: |
| 93 | + GOLDEN_UPDATE=1 $(MAKE) test |
| 94 | + |
| 95 | +.PHONY: golden-clean |
| 96 | +golden-clean: |
| 97 | + find . -type f -name '*.golden' -path '*/testdata/*' -delete |
| 98 | + find . -type d -empty -path '*/testdata/*' -delete |
| 99 | + |
| 100 | +.PHONY: golden-regen |
| 101 | +golden-regen: golden-clean golden-update |
| 102 | + |
| 103 | +# |
| 104 | +# Code Generation |
| 105 | +# |
| 106 | + |
| 107 | +.PHONY: generate |
| 108 | +generate: |
| 109 | + go generate ./... |
| 110 | + |
| 111 | +.PHONY: check-generate |
| 112 | +check-generate: |
| 113 | + $(eval CHKDIR := $(shell mktemp -d)) |
| 114 | + cp -av . "$(CHKDIR)" |
| 115 | + make -C "$(CHKDIR)/" generate |
| 116 | + ( diff -rN . "$(CHKDIR)" && rm -rf "$(CHKDIR)" ) || \ |
| 117 | + ( rm -rf "$(CHKDIR)" && exit 1 ) |
| 118 | + |
| 119 | +# |
| 120 | +# Coverage |
| 121 | +# |
| 122 | + |
| 123 | +.PHONY: cov |
| 124 | +cov: coverage.out |
| 125 | + |
| 126 | +.PHONY: cov-html |
| 127 | +cov-html: coverage.out |
| 128 | + go tool cover -html=./coverage.out |
| 129 | + |
| 130 | +.PHONY: cov-func |
| 131 | +cov-func: coverage.out |
| 132 | + go tool cover -func=./coverage.out |
| 133 | + |
| 134 | +coverage.out: $(SOURCES) |
| 135 | + go test $(V) -covermode=count -coverprofile=./coverage.out ./... |
| 136 | + |
| 137 | +# |
| 138 | +# Dependencies |
| 139 | +# |
| 140 | + |
| 141 | +.PHONY: deps |
| 142 | +deps: |
| 143 | + go mod download |
| 144 | + |
| 145 | +.PHONY: deps-update |
| 146 | +deps-update: |
| 147 | + go get -u -t ./... |
| 148 | + |
| 149 | +.PHONY: deps-analyze |
| 150 | +deps-analyze: $(TOOLDIR)/gomod |
| 151 | + gomod analyze |
| 152 | + |
| 153 | +.PHONY: tidy |
| 154 | +tidy: |
| 155 | + go mod tidy $(V) |
| 156 | + |
| 157 | +.PHONY: verify |
| 158 | +verify: |
| 159 | + go mod verify |
| 160 | + |
| 161 | +.SILENT: check-tidy |
| 162 | +.PHONY: check-tidy |
| 163 | +check-tidy: |
| 164 | + cp go.mod go.mod.tidy-check |
| 165 | + cp go.sum go.sum.tidy-check |
| 166 | + go mod tidy |
| 167 | + ( \ |
| 168 | + diff go.mod go.mod.tidy-check && \ |
| 169 | + diff go.sum go.sum.tidy-check && \ |
| 170 | + rm -f go.mod go.sum && \ |
| 171 | + mv go.mod.tidy-check go.mod && \ |
| 172 | + mv go.sum.tidy-check go.sum \ |
| 173 | + ) || ( \ |
| 174 | + rm -f go.mod go.sum && \ |
| 175 | + mv go.mod.tidy-check go.mod && \ |
| 176 | + mv go.sum.tidy-check go.sum; \ |
| 177 | + exit 1 \ |
| 178 | + ) |
| 179 | + |
| 180 | +# |
| 181 | +# Documentation |
| 182 | +# |
| 183 | + |
| 184 | +# Serve docs |
| 185 | +.PHONY: docs |
| 186 | +docs: $(TOOLDIR)/godoc |
| 187 | + $(info serviing docs on http://127.0.0.1:6060/pkg/$(GOMODNAME)/) |
| 188 | + @godoc -http=127.0.0.1:6060 |
| 189 | + |
| 190 | +# |
| 191 | +# Release |
| 192 | +# |
| 193 | + |
| 194 | +.PHONY: new-version |
| 195 | +new-version: check-npx |
| 196 | + npx standard-version |
| 197 | + |
| 198 | +.PHONY: next-version |
| 199 | +next-version: check-npx |
| 200 | + npx standard-version --dry-run |
| 201 | + |
| 202 | +.PHONY: check-npx |
| 203 | +check-npx: |
| 204 | + $(if $(shell which npx),,\ |
| 205 | + $(error No npx found in PATH, please install NodeJS)) |
0 commit comments