-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
107 lines (81 loc) · 2.41 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
PROGRAM := gitforgefs
TARGET_DIR := ./bin
VERSION := $(shell git describe --tags --always)
SRC := $(shell find -name '*.go' -not -name '*_test.go' -type f)
BIN := $(TARGET_DIR)/$(PROGRAM)
# tests
ALL_SRC := $(shell find -name '*.go' -type f)
COVER_PROFILE := $(TARGET_DIR)/coverage.out
COVER_REPORT := $(TARGET_DIR)/coverage.html
# go
GO := go
GOFMT := $(GO) fmt
GORUN := $(GO) run -race
GOCLEAN := $(GO) clean
GOTEST := $(GO) test
GOCOVER := $(GO) tool cover
GOBUILD := $(GO) build
# docker
DOCKER := docker
DOCKERBUILD := $(DOCKER) build
DOCKERTAG := $(DOCKER) tag
DOCKERPUSH := $(DOCKER) push
DOCKER_TAGS := $(PROGRAM):$(VERSION)
.PHONY: all
all: build
.PHONY: help
help:
@echo "clean revert back to clean state"
@echo "lint lint and format the code"
@echo "run run without building"
@echo "prepare prepare environment for build"
@echo "test build and run short test suite"
@echo "coverage build and run full test suite and generate coverage report"
@echo "build build the binary"
@echo "build-docker build the docker image"
# clean: revert back to initial state
.PHONY: clean
clean:
$(GOCLEAN)
rm -r $(TARGET_DIR)
# lint: lint and format the code
.PHONY: lint
lint:
$(GOFMT) ./...
# run: run without building with debug flags
ifeq (run,$(firstword $(MAKECMDGOALS)))
# use the rest as arguments for "run"
RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
# ...and turn them into do-nothing targets
$(eval $(RUN_ARGS):;@:)
endif
.PHONY: run
run: lint
$(GORUN) main.go $(RUN_ARGS)
# prepare: prepare environment for build
$(TARGET_DIR):
mkdir $(TARGET_DIR) &>/dev/null || true
.PHONY: prepare
prepare: $(TARGET_DIR)
# test: run short test suite
.PHONY: test
test: lint
$(GOTEST) -short ./...
# coverage: run full test suite with coverage report
$(COVER_PROFILE): $(TARGET_DIR) $(ALL_SRC)
$(GOTEST) -coverprofile=$(COVER_PROFILE) ./...
$(COVER_REPORT): $(TARGET_DIR) $(COVER_PROFILE)
$(GOCOVER) -html=$(COVER_PROFILE) -o=$(COVER_REPORT)
.PHONY: coverage
coverage: lint $(COVER_REPORT)
$(GOCOVER) -func=$(COVER_PROFILE)
# build: build the program for release
$(BIN): $(TARGET_DIR) $(SRC)
$(GOBUILD) -o=$(BIN)
.PHONY: build
build: lint $(BIN)
# build-docker: build the docker image
.PHONY: build-docker
build-docker: $(BIN)
$(DOCKERBUILD) -t $(PROGRAM) .
$(foreach tag,$(DOCKER_TAGS),$(DOCKERTAG) $(PROGRAM) $(tag);)