-
Notifications
You must be signed in to change notification settings - Fork 7
/
Makefile
135 lines (117 loc) · 3.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
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
PACKAGE_NAME := github.com/tuihub/librarian
GOHOSTOS:=$(shell go env GOHOSTOS)
GOPATH:=$(shell go env GOPATH)
VERSION=$(shell git describe --tags --always)
PROTO_VERSION=$(shell go list -m -f '{{.Version}}' github.com/tuihub/protos)
GOLANG_CROSS_VERSION ?= latest
SHELL:=/bin/bash
ifeq ($(GOHOSTOS), windows)
#the `find.exe` is different from `find` in bash/shell.
#to see https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/find.
#changed to use git-bash.exe to run find cli or other cli friendly, caused of every developer has a Git.
Git_Bash= $(subst cmd\,bin\bash.exe,$(dir $(shell where git)))
INTERNAL_PROTO_FILES=$(shell $(Git_Bash) -c "find internal -name *.proto")
else
INTERNAL_PROTO_FILES=$(shell find internal -name *.proto)
endif
.PHONY: init
# init env
init: init-lint
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
init-lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
init-test:
cd tests && make init
.PHONY: generate
# generate code
generate: generate-config generate-code
generate-config:
protoc --proto_path=./internal \
--go_out=paths=source_relative:./internal \
$(INTERNAL_PROTO_FILES)
generate-code:
go get github.com/google/wire/cmd/wire@latest
go get github.com/jmattheis/goverter@v1.5.0
go generate ./...
go mod tidy
.PHONY: lint
# lint files
lint:
golangci-lint run --fix
golangci-lint run # re-run to make sure fixes are valid, useful in some condition
.PHONY: test-unit
# run go test
test-unit:
go test -coverpkg=./... -race -coverprofile=coverage-unit.out -covermode=atomic ./...
.PHONY: test-goc
# run goc test
test-goc:
cd tests && make all
test-postprocess:
@while read -r p || [ -n "$$p" ]; \
do \
if [[ "$(GOHOSTOS)" == "darwin" ]]; then \
sed -i '' "/$${p//\//\\/}/d" ./coverage-unit.out; \
sed -i '' "/$${p//\//\\/}/d" ./coverage-goc.out; \
else \
sed -i "/$${p//\//\\/}/d" ./coverage-unit.out; \
sed -i "/$${p//\//\\/}/d" ./coverage-goc.out; \
fi \
done < ./.coverageignore
test-all: test-unit test-goc test-postprocess
.PHONY: run
# run server
run:
CGO_ENABLED=1 kratos run
.PHONY: build
# build server in debug mode
build:
mkdir -p bin/ && go build -tags debug -ldflags "-X main.version=$(VERSION) -X main.protoVersion=$(PROTO_VERSION)" -o ./bin/ ./...
.PHONY: release-dry-run
# build server in release mode, for manual test
release-dry-run:
@docker run \
--rm \
-e CGO_ENABLED=1 \
-e PROTO_VERSION=$(PROTO_VERSION) \
-v /var/run/docker.sock:/var/run/docker.sock \
-v `pwd`:/go/src/$(PACKAGE_NAME) \
-w /go/src/$(PACKAGE_NAME) \
ghcr.io/goreleaser/goreleaser-cross:${GOLANG_CROSS_VERSION} \
release --clean --skip-validate --skip-publish
.PHONY: release
# build server in release mode, for CI, do not run manually
release:
@if [ ! -f ".release-env" ]; then \
echo "\033[91m.release-env is required for release\033[0m";\
exit 1;\
fi
docker run \
--rm \
-e CGO_ENABLED=1 \
-e PROTO_VERSION=$(PROTO_VERSION) \
--env-file .release-env \
-v /var/run/docker.sock:/var/run/docker.sock \
-v `pwd`:/go/src/$(PACKAGE_NAME) \
-w /go/src/$(PACKAGE_NAME) \
ghcr.io/goreleaser/goreleaser-cross:${GOLANG_CROSS_VERSION} \
release
# show help
help:
@echo ''
@echo 'Usage:'
@echo ' make [target]'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
helpMessage = match(lastLine, /^# (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help