-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
104 lines (81 loc) · 4.19 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
# Custom configuration | 独立配置
# Service name | 项目名称
SERVICE=Pay
# Service name in specific style | 项目经过style格式化的名称
SERVICE_STYLE=pay
# Service name in lowercase | 项目名称全小写格式
SERVICE_LOWER=pay
# Service name in snake format | 项目名称下划线格式
SERVICE_SNAKE=pay
# Service name in snake format | 项目名称短杠格式
SERVICE_DASH=pay
# The project version, if you don't use git, you should set it manually | 项目版本,如果不使用git请手动设置
VERSION=$(shell git describe --tags --always)
# The project file name style | 项目文件命名风格
PROJECT_STYLE=go_zero
# Whether to use i18n | 是否启用 i18n
PROJECT_I18N=true
# The suffix after build or compile | 构建后缀
PROJECT_BUILD_SUFFIX=rpc
# Ent enabled features | Ent 启用的官方特性
ENT_FEATURE=sql/execquery
# The arch of the build | 构建的架构
GOARCH=amd64
# ---- You may not need to modify the codes below | 下面的代码大概率不需要更改 ----
GO ?= go
GOFMT ?= gofmt "-s"
GOFILES := $(shell find . -name "*.go")
LDFLAGS := -s -w
.PHONY: test
test: # Run test for the project | 运行项目测试
go test -v --cover ./internal/..
.PHONY: fmt
fmt: # Format the codes | 格式化代码
$(GOFMT) -w $(GOFILES)
.PHONY: lint
lint: # Run go linter | 运行代码错误分析
golangci-lint run -D staticcheck
.PHONY: tools
tools: # Install the necessary tools | 安装必要的工具
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest;
.PHONY: docker
docker: # Build the docker image | 构建 docker 镜像
docker build -f Dockerfile -t ${DOCKER_USERNAME}/$(SERVICE_DASH)-$(PROJECT_BUILD_SUFFIX):${VERSION} .
@echo "Build docker successfully"
.PHONY: publish-docker
publish-docker: # Publish docker image | 发布 docker 镜像
echo "${DOCKER_PASSWORD}" | docker login --username ${DOCKER_USERNAME} --password-stdin https://${REPO}
docker push ${DOCKER_USERNAME}/$(SERVICE_DASH)-$(PROJECT_BUILD_SUFFIX):${VERSION}
@echo "Publish docker successfully"
.PHONY: gen-rpc
gen-rpc: # Generate RPC files from proto | 生成 RPC 的代码
goctls rpc protoc ./$(SERVICE_STYLE).proto --go_out=./types --go-grpc_out=./types --zrpc_out=. --style=$(PROJECT_STYLE)
ifeq ($(shell uname -s), Darwin)
sed -i "" 's/,omitempty//g' ./types/$(SERVICE_LOWER)/*.pb.go
else
sed -i 's/,omitempty//g' ./types/$(SERVICE_LOWER)/*.pb.go
endif
@echo "Generate RPC codes successfully"
.PHONY: gen-ent
gen-ent: # Generate Ent codes | 生成 Ent 的代码
go run -mod=mod entgo.io/ent/cmd/ent generate --template glob="./ent/template/*.tmpl" ./ent/schema --feature $(ENT_FEATURE)
@echo "Generate Ent codes successfully"
.PHONY: gen-rpc-ent-logic
gen-rpc-ent-logic: # Generate logic code from Ent, need model and group params | 根据 Ent 生成逻辑代码, 需要设置 model 和 group
goctls rpc ent --schema=./ent/schema --style=$(PROJECT_STYLE) --multiple=false --service_name=$(SERVICE) --search_key_num=3 --output=./ --model=$(model) --group=$(group) --proto_out=./desc/$(shell echo $(model) | tr A-Z a-z).proto --i18n=$(PROJECT_I18N) --overwrite=true
@echo "Generate logic codes from Ent successfully"
.PHONY: build-win
build-win: # Build project for Windows | 构建Windows下的可执行文件
env CGO_ENABLED=0 GOOS=windows GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX).exe $(SERVICE_STYLE).go
@echo "Build project for Windows successfully"
.PHONY: build-mac
build-mac: # Build project for MacOS | 构建MacOS下的可执行文件
env CGO_ENABLED=0 GOOS=darwin GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX) $(SERVICE_STYLE).go
@echo "Build project for MacOS successfully"
.PHONY: build-linux
build-linux: # Build project for Linux | 构建Linux下的可执行文件
env CGO_ENABLED=0 GOOS=linux GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX) $(SERVICE_STYLE).go
@echo "Build project for Linux successfully"
.PHONY: help
help: # Show help | 显示帮助
@grep -E '^[a-zA-Z0-9 -]+:.*#' Makefile | sort | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done