|
| 1 | +MODULE = $(shell env GO111MODULE=on $(GO) list -m) |
| 2 | +DATE ?= $(shell date +%FT%T%z) |
| 3 | +VERSION ?= $(shell git describe --tags --always --dirty --match="v*" 2> /dev/null || \ |
| 4 | + cat $(CURDIR)/.version 2> /dev/null || echo v0) |
| 5 | +PKGS = $(or $(PKG),$(shell env GO111MODULE=on $(GO) list ./...)) |
| 6 | +TESTPKGS = $(shell env GO111MODULE=on $(GO) list -f \ |
| 7 | + '{{ if or .TestGoFiles .XTestGoFiles }}{{ .ImportPath }}{{ end }}' \ |
| 8 | + $(PKGS)) |
| 9 | +BIN = $(CURDIR)/.bin |
| 10 | + |
| 11 | +GO = go |
| 12 | +TIMEOUT = 15 |
| 13 | +V = 0 |
| 14 | +Q = $(if $(filter 1,$V),,@) |
| 15 | +M = $(shell printf "\033[34;1m▶\033[0m") |
| 16 | + |
| 17 | +export GO111MODULE=on |
| 18 | +export CGO_ENABLED=0 |
| 19 | +export GOPROXY=https://proxy.golang.org |
| 20 | + |
| 21 | +.PHONY: all |
| 22 | +all: fmt lint | $(BIN) ; $(info $(M) building executable…) @ ## Build program binary |
| 23 | + $Q $(GO) build \ |
| 24 | + -tags release \ |
| 25 | + -ldflags '-X main.Version=$(VERSION) -X main.BuildDate=$(DATE)' \ |
| 26 | + -o $(BIN)/$(basename $(MODULE)) main.go |
| 27 | + |
| 28 | +# Tools |
| 29 | + |
| 30 | +$(BIN): |
| 31 | + @mkdir -p $@ |
| 32 | +$(BIN)/%: | $(BIN) ; $(info $(M) building $(PACKAGE)…) |
| 33 | + $Q tmp=$$(mktemp -d); \ |
| 34 | + env GO111MODULE=off GOPATH=$$tmp GOBIN=$(BIN) $(GO) get $(PACKAGE) \ |
| 35 | + || ret=$$?; \ |
| 36 | + rm -rf $$tmp ; exit $$ret |
| 37 | + |
| 38 | +GOLINT = $(BIN)/golint |
| 39 | +$(BIN)/golint: PACKAGE=golang.org/x/lint/golint |
| 40 | + |
| 41 | +GOCOV = $(BIN)/gocov |
| 42 | +$(BIN)/gocov: PACKAGE=github.com/axw/gocov/... |
| 43 | + |
| 44 | +GOCOVXML = $(BIN)/gocov-xml |
| 45 | +$(BIN)/gocov-xml: PACKAGE=github.com/AlekSi/gocov-xml |
| 46 | + |
| 47 | +GO2XUNIT = $(BIN)/go2xunit |
| 48 | +$(BIN)/go2xunit: PACKAGE=github.com/tebeka/go2xunit |
| 49 | + |
| 50 | +# Tests |
| 51 | + |
| 52 | +TEST_TARGETS := test-default test-bench test-short test-verbose test-race |
| 53 | +.PHONY: $(TEST_TARGETS) test-xml check test tests |
| 54 | +test-bench: ARGS=-run=__absolutelynothing__ -bench=. ## Run benchmarks |
| 55 | +test-short: ARGS=-short ## Run only short tests |
| 56 | +test-verbose: ARGS=-v ## Run tests in verbose mode with coverage reporting |
| 57 | +test-race: ARGS=-race ## Run tests with race detector |
| 58 | +$(TEST_TARGETS): NAME=$(MAKECMDGOALS:test-%=%) |
| 59 | +$(TEST_TARGETS): test |
| 60 | +check test tests: fmt lint ; $(info $(M) running $(NAME:%=% )tests…) @ ## Run tests |
| 61 | + $Q $(GO) test -timeout $(TIMEOUT)s $(ARGS) $(TESTPKGS) |
| 62 | + |
| 63 | +test-xml: fmt lint | $(GO2XUNIT) ; $(info $(M) running xUnit tests…) @ ## Run tests with xUnit output |
| 64 | + $Q mkdir -p test |
| 65 | + $Q 2>&1 $(GO) test -timeout $(TIMEOUT)s -v $(TESTPKGS) | tee test/tests.output |
| 66 | + $(GO2XUNIT) -fail -input test/tests.output -output test/tests.xml |
| 67 | + |
| 68 | +COVERAGE_MODE = atomic |
| 69 | +COVERAGE_PROFILE = $(COVERAGE_DIR)/profile.out |
| 70 | +COVERAGE_XML = $(COVERAGE_DIR)/coverage.xml |
| 71 | +COVERAGE_HTML = $(COVERAGE_DIR)/index.html |
| 72 | +.PHONY: test-coverage test-coverage-tools |
| 73 | +test-coverage-tools: | $(GOCOV) $(GOCOVXML) |
| 74 | +test-coverage: COVERAGE_DIR := $(CURDIR)/test/coverage.$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 75 | +test-coverage: fmt lint test-coverage-tools ; $(info $(M) running coverage tests…) @ ## Run coverage tests |
| 76 | + $Q mkdir -p $(COVERAGE_DIR) |
| 77 | + $Q $(GO) test \ |
| 78 | + -coverpkg=$$($(GO) list -f '{{ join .Deps "\n" }}' $(TESTPKGS) | \ |
| 79 | + grep '^$(MODULE)/' | \ |
| 80 | + tr '\n' ',' | sed 's/,$$//') \ |
| 81 | + -covermode=$(COVERAGE_MODE) \ |
| 82 | + -coverprofile="$(COVERAGE_PROFILE)" $(TESTPKGS) |
| 83 | + $Q $(GO) tool cover -html=$(COVERAGE_PROFILE) -o $(COVERAGE_HTML) |
| 84 | + $Q $(GOCOV) convert $(COVERAGE_PROFILE) | $(GOCOVXML) > $(COVERAGE_XML) |
| 85 | + |
| 86 | +.PHONY: lint |
| 87 | +lint: | $(GOLINT) ; $(info $(M) running golint…) @ ## Run golint |
| 88 | + $Q $(GOLINT) -set_exit_status $(PKGS) |
| 89 | + |
| 90 | +.PHONY: fmt |
| 91 | +fmt: ; $(info $(M) running gofmt…) @ ## Run gofmt on all source files |
| 92 | + $Q $(GO) fmt $(PKGS) |
| 93 | + |
| 94 | +# Misc |
| 95 | + |
| 96 | +.PHONY: clean |
| 97 | +clean: ; $(info $(M) cleaning…) @ ## Cleanup everything |
| 98 | + @rm -rf $(BIN) |
| 99 | + @rm -rf test/tests.* test/coverage.* |
| 100 | + |
| 101 | +.PHONY: help |
| 102 | +help: |
| 103 | + @grep -E '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ |
| 104 | + awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}' |
| 105 | + |
| 106 | +.PHONY: version |
| 107 | +version: |
| 108 | + @echo $(VERSION) |
0 commit comments