-
Notifications
You must be signed in to change notification settings - Fork 63
/
Makefile
91 lines (75 loc) · 3.34 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
MODULE = $(shell $(GO) list -m)
DATE ?= $(shell date +%FT%T%z)
VERSION ?= $(shell git describe --tags --always --dirty --match=v* 2> /dev/null || \
cat .version 2> /dev/null || echo v0)
PKGS = $(or $(PKG),$(shell $(GO) list ./...))
BIN = bin
GO = go
TIMEOUT = 15
V = 0
Q = $(if $(filter 1,$V),,@)
M = $(shell if [ "$$(tput colors 2> /dev/null || echo 0)" -ge 8 ]; then printf "\033[34;1m▶\033[0m"; else printf "▶"; fi)
GENERATED = # List of generated files
.SUFFIXES:
.PHONY: all
all: fmt lint $(GENERATED) | $(BIN) ; $(info $(M) building executable…) @ ## Build program binary
$Q $(GO) build \
-tags release \
-ldflags '-X $(MODULE)/cmd.Version=$(VERSION) -X $(MODULE)/cmd.BuildDate=$(DATE)' \
-o $(BIN)/$(basename $(MODULE)) main.go
# Tools
$(BIN):
@mkdir -p $@
$(BIN)/%: PACKAGE=$(shell $(GO) list -e -f '{{ range .Imports }}{{ printf "%s\n" . }}{{ end }}' tools.go | grep "/$*$$")
$(BIN)/%: go.mod go.sum | $(BIN) ; $(info $(M) building tool $*…)
@[ -n "$(PACKAGE)" ] || (>&2 echo "*** Unknown tool $*!"; false)
$Q env GOBIN=$(abspath $(BIN)) $(GO) install $(PACKAGE)
GOIMPORTS = $(BIN)/goimports
REVIVE = $(BIN)/revive
GOCOV = $(BIN)/gocov
GOCOVXML = $(BIN)/gocov-xml
GOTESTSUM = $(BIN)/gotestsum
# Generate
# Tests
TEST_TARGETS := test-short test-race
.PHONY: $(TEST_TARGETS) check test tests
test-short: ARGS=-short ## Run only short tests
test-race: ARGS=-race ## Run tests with race detector
$(TEST_TARGETS): NAME=$(MAKECMDGOALS:test-%=%)
$(TEST_TARGETS): test
check test tests: fmt lint $(GENERATED) | $(GOTESTSUM) ; $(info $(M) running $(NAME:%=% )tests…) @ ## Run tests
$Q mkdir -p test
$Q $(GOTESTSUM) --junitfile test/tests.xml -- -timeout $(TIMEOUT)s $(ARGS) $(PKGS)
.PHONY: test-bench
test-bench: $(GENERATED) ; $(info $(M) running benchmarks…) @ ## Run benchmarks
$Q $(GOTESTSUM) -f standard-quiet -- --timeout $(TIMEOUT)s -run=__absolutelynothing__ -bench=. $(PKGS)
COVERAGE_MODE = atomic
.PHONY: test-coverage
test-coverage: fmt lint $(GENERATED)
test-coverage: | $(GOCOV) $(GOCOVXML) $(GOTESTSUM) ; $(info $(M) running coverage tests…) @ ## Run coverage tests
$Q mkdir -p test
$Q $(GOTESTSUM) -- \
-coverpkg=$(shell echo $(PKGS) | tr ' ' ',') \
-covermode=$(COVERAGE_MODE) \
-coverprofile=test/profile.out $(PKGS)
$Q $(GO) tool cover -html=test/profile.out -o test/coverage.html
$Q $(GOCOV) convert test/profile.out | $(GOCOVXML) > test/coverage.xml
@echo -n "Code coverage: "; \
echo "scale=1;$$(sed -En 's/^<coverage line-rate="([0-9.]+)".*/\1/p' test/coverage.xml) * 100 / 1" | bc -q
.PHONY: lint
lint: | $(REVIVE) ; $(info $(M) running golint…) @ ## Run golint
$Q $(REVIVE) -formatter friendly -set_exit_status ./...
.PHONY: fmt
fmt: | $(GOIMPORTS) ; $(info $(M) running gofmt…) @ ## Run gofmt on all source files
$Q $(GOIMPORTS) -local $(MODULE) -w $(shell $(GO) list -f '{{$$d := .Dir}}{{range $$f := .GoFiles}}{{printf "%s/%s\n" $$d $$f}}{{end}}{{range $$f := .CgoFiles}}{{printf "%s/%s\n" $$d $$f}}{{end}}{{range $$f := .TestGoFiles}}{{printf "%s/%s\n" $$d $$f}}{{end}}' $(PKGS))
# Misc
.PHONY: clean
clean: ; $(info $(M) cleaning…) @ ## Cleanup everything
@rm -rf $(BIN) test $(GENERATED)
.PHONY: help
help:
@grep -hE '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-17s\033[0m %s\n", $$1, $$2}'
.PHONY: version
version:
@echo $(VERSION)