-
Notifications
You must be signed in to change notification settings - Fork 8
/
Makefile
120 lines (98 loc) · 3.09 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# commands
GO = go
GOBUILD = $(GO) build
GOCOV = $(TOOLBIN)/gocov
GOCOVXML = $(TOOLBIN)/gocov-xml
GOINSTALL := GOOS= GOARCH= $(GO) install
LINTER = $(TOOLBIN)/golangci-lint
STENTOR = $(TOOLBIN)/stentor
TESTER = $(TOOLBIN)/gotestsum
# variables
BUILDDATE := $(shell date +%Y-%m-%d)
COMMIT := $(shell git rev-parse HEAD)
GOOS := $(shell $(GO) env GOOS)
VERSION := $(shell $(GO) run ./cmd/gotagger)
$(if $(VERSION),,$(error failed to determine version))
# directories
REPORTDIR = build/reports
TOOLBIN = build/tools
# flags
BUILDFLAGS = -v -ldflags '-X main.AppVersion=$(VERSION) -X main.Commit=$(COMMIT) -X main.BuildDate=$(BUILDDATE)'
COVERFLAGS = -covermode $(COVERMODE) -coverprofile $(COVEROUT)
COVERMODE = atomic
COVEROUT = $(REPORTDIR)/coverage.out
COVERXML = $(REPORTDIR)/coverage.xml
LINTFLAGS =
REPORTFLAGS = --jsonfile $(REPORTJSON) --junitfile $(REPORTXML)
REPORTJSON = $(REPORTDIR)/go-test.json
REPORTXML = $(REPORTDIR)/go-test.xml
TESTFLAGS = --format=$(TESTFORMAT) -- -timeout $(TIMEOUT) $(COVERFLAGS)
TESTFORMAT = short
TIMEOUT = 60s
# conditional flags
ifeq ($(DRY_RUN),false)
STENTORFLAGS = -release
else
STENTORFLAGS =
endif
TARGET = build/$(GOOS)/gotagger
TOOLREQS = tools/go.mod tools/go.sum
# recipes
.PHONY: all
all: lint build test
.PHONY: build
build:
$(GOBUILD) $(BUILDFLAGS) -o $(TARGET) ./cmd/gotagger/main.go
.PHONY: changelog
changelog: | $(STENTOR)
$(STENTOR) $(STENTORFLAGS) $(VERSION) "$$(git tag --list --sort=version:refname | tail -n1)"
.PHONY: clean
clean:
$(RM) $(TARGET)
$(RM) -r $(REPORTDIR)/ dist/
.PHONY: distclean
distclean: clean
$(RM) -r $(TOOLBIN)/
.PHONY: format
format: LINTFLAGS += --fix
format: lint
.PHONY: lint
lint: | $(LINTER)
$(LINTER) run $(LINTFLAGS)
.PHONY: report
report: TESTFLAGS := $(REPORTFLAGS) $(TESTFLAGS)
report: test | $(GOCOV) $(GOCOVXML)
$(GOCOV) convert $(COVEROUT) | $(GOCOVXML) > $(COVERXML)
.PHONY: test tests
test tests: | $(TESTER) $(REPORTDIR)
$(TESTER) $(TESTFLAGS) ./...
.PHONY: version
version:
@echo $(VERSION)
$(REPORTDIR) $(TOOLBIN):
@mkdir -p $@
tools/go.mod tools/go.sum: tools/tools.go
cd tools/ && go mod tidy
define installtool
$1: tools/go.mod tools/go.sum | $$(TOOLBIN)
cd tools/ && GOBIN=$$(CURDIR)/$$(TOOLBIN) $$(GOINSTALL) $2
endef
# tool targets
$(eval $(call installtool,$(GOCOV),github.com/axw/gocov/gocov))
$(eval $(call installtool,$(GOCOVXML),github.com/AlekSi/gocov-xml))
$(eval $(call installtool,$(LINTER),github.com/golangci/golangci-lint/cmd/golangci-lint))
$(eval $(call installtool,$(STENTOR),github.com/wfscheper/stentor/cmd/stentor))
$(eval $(call installtool,$(TESTER),gotest.tools/gotestsum))
.PHONY: help
help:
@printf "Available targets:\
\n all lint, build, and test code\
\n build builds gotagger exectuable\
\n changelog run stentor to show changelog entry\
\n clean removes generated files\
\n distclean reset's workspace to original state\
\n format format source code\
\n lint run linters on source code\
\n report generate test and coverage reports\
\n test run tests\
"