forked from rl-os/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
168 lines (144 loc) · 5.45 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# --- Makefile ----
# setup enviroment
SHELL := /bin/bash
export GO111MODULE := on
export PATH := bin:$(PATH)
VERSION ?= $(shell git describe --tags --always --dirty --match="v*" 2> /dev/null || cat $(CURDIR)/.version 2> /dev/null || echo v1.0.0)
COMMIT ?= $(shell git rev-parse HEAD)
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
GIT_BRANCH_CLEAN := $(shell echo $(GIT_BRANCH) | sed -e "s/[^[:alnum:]]/-/g")
BUILDTIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
DOCKER_REGISTRY_DOMAIN ?= docker.pkg.github.com
DOCKER_REGISTRY_PATH ?= deissh
DOCKER_IMAGE ?= $(DOCKER_REGISTRY_PATH)/$(PACKAGE):$(VERSION)
DOCKER_IMAGE_DOMAIN ?= $(DOCKER_REGISTRY_DOMAIN)/$(DOCKER_IMAGE)
# ===============================================
GOCMD = go
GOLINT := $(shell which golint)
GOIMPORT := $(shell which goimports)
GOFMT := $(shell which gofmt)
GOCYCLO := $(shell which gocyclo)
GOBUILD := $(GOCMD) build
GOCLEAN := $(GOCMD) clean
GOTEST := $(GOCMD) test
GOMOD := $(GOCMD) mod
GOGET := $(GOCMD) get
GOLIST := $(GOCMD) list
GOVET := $(GOCMD) vet
# ===============================================
# build setting
CMD_DIR ?= ./cmd
BIN_DIR ?= $(PWD)/bin
BUILD_CMDS := $(shell $(GOLIST) ./$(CMD_DIR)/...)
GOFILES := $(shell find . -name "*.go" -type f)
GOLDFLAGS += -X main.Version=$(VERSION)
GOLDFLAGS += -X main.Commit=$(COMMIT)
GOLDFLAGS += -X main.Branch=$(GIT_BRANCH_CLEAN)
GOLDFLAGS += -X main.BuildTimestamp=$(BUILDTIME)
GOFLAGS = -ldflags "$(GOLDFLAGS)"
# ===============================================
GO = go
.DEFAULT_GOAL := help
MAKE_ENV += PACKAGE VERSION DOCKER_IMAGE DOCKER_IMAGE_DOMAIN
SHELL_EXPORT := $(foreach v,$(MAKE_ENV),$(v)='$($(v))' )
# check requeres commands
EXECUTABLES = wget
_ := $(foreach exec,$(EXECUTABLES),\
$(if $(shell which $(exec)),some string,$(error "No $(exec) in PATH")))
# ===============================================
## Clear bin folder
.PHONY: clear
clear:
@rm -rf bin
## Set impotants flags and then build all commands in cmd folder
.PHONY: build
build: clear
@(cd cmd ; for CMD in *; do $(GOBUILD) $(GOFLAGS) -o $(BIN_DIR)/$$CMD $$CMD/*.go && echo "$$CMD build done" || exit 1; done;)
## Run all checks
.PHONY: lint
lint: vet fmt-check
@for PKG in $(PACKAGES); do golint -set_exit_status $$PKG || exit 1; done;
@$(GOIMPORT) -d $(shell find . -type f -name '*.go' -not -path "./vendor/*")
@$(GOCYCLO) -over 55 $(shell find . -iname '*.go' -type f | grep -v /vendor/)
## Install missing dependencies
## Also setup unnessosory packages that using in CI
.PHONY: install
install:
@echo "> Checking if there is any missing dependencies..."
@$(GOMOD) download
@$(GOGET) -u github.com/amacneil/dbmate
@echo "> Installing unnessosory packages"
$(GOGET) -u golang.org/x/lint/golint
$(GOGET) -u github.com/fzipp/gocyclo
$(GOGET) -u golang.org/x/tools/cmd/goimports
$(GOGET) -u github.com/git-chglog/git-chglog/cmd/git-chglog
wget -O - -q https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.15.0
## This help message
## Which can also be multiline
.PHONY: help
help: message
@printf "Usage\n";
@awk '{ \
if ($$0 ~ /^.PHONY: [a-zA-Z\-\_0-9]+$$/) { \
helpCommand = substr($$0, index($$0, ":") + 2); \
if (helpMessage) { \
printf "\033[36m%-20s\033[0m %s\n", \
helpCommand, helpMessage; \
helpMessage = ""; \
} \
} else if ($$0 ~ /^[a-zA-Z\-\_0-9.]+:/) { \
helpCommand = substr($$0, 0, index($$0, ":")); \
if (helpMessage) { \
printf "\033[36m%-20s\033[0m %s\n", \
helpCommand, helpMessage; \
helpMessage = ""; \
} \
} else if ($$0 ~ /^##/) { \
if (helpMessage) { \
helpMessage = helpMessage"\n "substr($$0, 3); \
} else { \
helpMessage = substr($$0, 3); \
} \
} else { \
if (helpMessage) { \
print "\n "helpMessage"\n" \
} \
helpMessage = ""; \
} \
}' \
$(MAKEFILE_LIST)
## -- Utils --
## Generate CHANGELOG.md based on git
.PHONY: chglog
chglog:
git-chglog -o CHANGELOG.md $(shell git describe --tags $(shell git rev-list --tags --max-count=1))
.PHONY: message
message:
@echo -e "\n _.====.._ ___ _ \n ,:. ~-_ / _ \ ___ _ _ | |\n \`\ ~-_ | (_) | (_-< | || | |_|\n | \`. \___/ /__/ \_,_| (_)\n ,/ ~-_ lazer server\n-..__..-'' ~~--..__...----...--.....---.....--....---...\n\n GitHub: github.com/deissh/osu-api-server\n\n\n © 2019-2020, deissh.\n\n"
## -- Go commands --
## Run go vet on current project
.PHONY: vet
vet:
$(GOVET) ./...
## Run go fmt on current project
.PHONY: fmt
fmt:
$(GOFMT) -s -w $(GOFILES)
## Run check fmt and show message if not all ok
.PHONY: fmt-check
fmt-check:
@diff=$$($(GOFMT) -s -d $(GOFILES)); \
if [ -n "$$diff" ]; then \
echo "Please run 'make fmt' and commit the result:"; \
echo "$${diff}"; \
exit 1; \
fi;
## Run go test on current project
.PHONY: test
test:
$(GOTEST) ./...
## Generate coverage report and then show
.PHONY: view-covered
view-covered:
$(GOTEST) -coverprofile=cover.out ./...
$(GOCMD) tool cover -html=cover.out