-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakefile
More file actions
108 lines (92 loc) · 3.15 KB
/
Makefile
File metadata and controls
108 lines (92 loc) · 3.15 KB
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
SHELL = /usr/bin/env bash
.SHELLFLAGS = -ecuo pipefail
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
MAKEFLAGS += --no-builtin-variables
.DEFAULT_GOAL := help
# ==============================================================================
# Variables
# ==============================================================================
CI ?=
OUTPUTDIR ?= output
$(shell mkdir -p $(OUTPUTDIR))
COVERPROFILE ?= coverage.out
COVERHTML ?= coverage.html
GOFILES := $(shell go list -f '{{ range $$file := .GoFiles }}{{ printf "%s/%s\n" $$.Dir $$file }} {{- end }}' ./... | sort)
GOTESTFILES := $(shell go list -f '{{ range $$file := .TestGoFiles }}{{ printf "%s/%s\n" $$.Dir $$file }} {{- end }}' ./... | sort)
FMTSTAMP := .fmt.stamp
LINTSTAMP := .lint.stamp
# ==============================================================================
# Functions
# ==============================================================================
define open_browser
@case $$(uname -s) in \
Linux) xdg-open $(1) ;; \
Darwin) open $(1) ;; \
*) echo "Unsupported platform" ;; \
esac
endef
# ==============================================================================
# Targets
# ==============================================================================
## Dependencies:
.PHONY: deps/get
deps/get: ## Get dependencies
go get -v ./...
go mod tidy -v
.PHONY: deps/update
deps/update: ## Update dependencies
go get -v -u ./...
go mod tidy -v
## Generators:
.PHONY: gif
gif: ## Generate GIF for example application
./scripts/gif.sh
## Code Quality:
fmt: $(FMTSTAMP) ## Format code
$(FMTSTAMP): $(GOFILES) $(GOTESTFILES)
golangci-lint run --fix --verbose
lint: $(LINTSTAMP) ## Run linters
$(LINTSTAMP): $(GOFILES) $(GOTESTFILES)
golangci-lint run --verbose
touch $@
## Testing:
test: $(OUTPUTDIR)/$(COVERHTML) ## Run tests
$(OUTPUTDIR)/$(COVERPROFILE): $(GOFILES) $(GOTESTFILES) go.mod ## Run tests with coverage
mkdir -pv $(dir $@)
go test -v \
-outputdir=$(dir $@) \
-coverprofile=$(notdir $@) \
-coverpkg=./... \
-run= ./... | \
tee $(OUTPUTDIR)/test.log
$(OUTPUTDIR)/$(COVERHTML): $(OUTPUTDIR)/$(COVERPROFILE) ## Generate HTML coverage report
ifeq ($(CI),)
mkdir -pv $(dir $@)
go tool cover -html=$(OUTPUTDIR)/$(COVERPROFILE) -o $(OUTPUTDIR)/$(COVERHTML)
@echo "🌐 Run 'make browser/cover' to open coverage report in browser"
else
@echo "CI detected, skipping HTML coverage report generation"
endif
.PHONY: browser/cover
browser/cover: ## Open coverage report in browser
$(call open_browser,$(OUTPUTDIR)/$(COVERHTML))
.PHONY: clean
clean: ## Clean up build artifacts
rm -rfv $(OUTPUTDIR) || true
## Help:
.PHONY: help
help: GREEN := $(shell tput -Txterm setaf 2)
help: YELLOW := $(shell tput -Txterm setaf 3)
help: CYAN := $(shell tput -Txterm setaf 6)
help: RESET := $(shell tput -Txterm sgr0)
help: ## Show this help
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[a-zA-Z0-9_\/-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)