-
Notifications
You must be signed in to change notification settings - Fork 13
/
Makefile
105 lines (81 loc) · 2.72 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
.PHONY: test cover build all clean parallelizable-steps
SHELL := /bin/bash
GOPATH ?= $(shell go env GOPATH)
PATH := $(GOPATH)/bin:$(PATH)
PACKAGES := utils database testutils monitor
LINT_PACKAGES = $(PACKAGES) gonittest log cmd
VET_PACKAGES = $(PACKAGES) gonittest log cmd
fmtcheck = @if goimports -l $(1) | read var; then echo "goimports check failed for $(1):\n `goimports -d $(1)`"; exit 1; fi
ifeq ($(VERBOSE), 1)
GO_TEST_ARGS := -v
else
GO_TEST_ARGS :=
endif
GO_TEST := @go test $(GO_TEST_ARGS)
GO_COVER := @go tool cover
BUILD_DIR := ./.build
DIST_DIR := ./dist/gonit
VENDOR := vendor
BUILD_DATE := $(shell date -u '+%Y-%m-%d %I:%M:%S UTC' 2> /dev/null)
GIT_HASH := $(shell git rev-parse HEAD 2> /dev/null)
GO_BUILD := go build -ldflags "-X 'main.buildDate=$(BUILD_DATE)' -X main.commit=$(GIT_HASH) -s -w"
GO_MOD := @go mod
TOOL_PATH ?= $(DIST_DIR)/$(TOOL_NAME)
# This allows forcing the dependencies to finish installing even in parallel mode
all:
@$(MAKE) get-build-deps
@$(MAKE) download
@$(MAKE) -s validate-command-gocovmerge validate-command-staticcheck
@$(MAKE) -s parallelizable-steps
parallelizable-steps: vet lint build test race-test cover
download:
$(GO_MOD) download
build/%:
@echo "+ Building GOARCH=$(*F)"
@mkdir -p $(DIST_DIR)
@GOARCH=$(*F) $(GO_BUILD) -o $(TOOL_PATH) .
@echo "*** Gonit binary created under $(TOOL_PATH) ***"
build: build/amd64
clean:
-rm -rf $(BUILD_DIR)
-rm -rf $(DIST_DIR)
get-build-deps:
@echo "+ Downloading build dependencies"
@go install golang.org/x/tools/cmd/goimports@latest
@go install honnef.co/go/tools/cmd/staticcheck@latest
@go install github.com/wadey/gocovmerge@latest
validate-command-%:
@which $(*F) > /dev/null || (echo "Tool $(*F) must be in your PATH" 2>&1 && exit 1)
vet: $(addprefix vet-, $(VET_PACKAGES))
@go vet .
vet-%:
@echo "+ $@"
@go vet ./$(*F)
lint: $(addprefix lint-, $(LINT_PACKAGES))
@echo "+ $@"
@$(MAKE) -s validate-command-staticcheck
@staticcheck ./...
$(call fmtcheck, .)
lint-%:
@echo "+ $@"
@staticcheck ./$(*F)
$(call fmtcheck, $(*F))
test: $(addprefix test-, $(PACKAGES))
@echo "+ Testing gonit tool"
@go test .
race-test: $(addprefix race-test-, $(PACKAGES))
cover-%: $(BUILD_DIR)/%.coverprofile
@echo "+ $@"
$(GO_COVER) -html=$^ -o=$(BUILD_DIR)/$(*F).html
cover: test $(addprefix cover-, $(PACKAGES))
@$(MAKE) -s validate-command-gocovmerge
@gocovmerge $(wildcard $(BUILD_DIR)/*.coverprofile) > $(BUILD_DIR)/cover.out
$(GO_COVER) -html=$(BUILD_DIR)/cover.out -o=$(BUILD_DIR)/coverage.html
test-%:
@echo "+ Testing $(*F)"
@mkdir -p $(BUILD_DIR)
$(GO_TEST) -covermode=count -coverprofile=$(BUILD_DIR)/$(*F).coverprofile ./$(*F)
race-test-%:
@echo "+ Testing $(*F) (race conditions)"
@mkdir -p $(BUILD_DIR)
$(GO_TEST) -race ./$(*F)