-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
90 lines (75 loc) · 2.49 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
NAME = toddler
PACKAGE = github.com/xgxw/toddler-go
MAIN = $(PACKAGE)/entry
DEFAULT_TAG = toddler-go:latest
DEFAULT_BUILD_TAG = 1.10-alpine
REMOTE_IMAGE = ccr.ccs.tencentyun.com/xgxw/toddler-go
BUILD_FLAGS= -ldflags "-s -w" -mod vendor -v -o $(NAME) entry/main.go
REMOTE_TAG = "$(shell git tag -l --sort=-v:refname|head -1)"
ifeq "$(MODE)" "dev"
REMOTE_TAG = staging
endif
ifeq "$(REMOTE_TAG)" ""
REMOTE_TAG = latest
endif
REMOTE_IMAGE_TAG = "$(REMOTE_IMAGE):$(REMOTE_TAG)"
ifeq "$(BUILD_TAG)" ""
BUILD_TAG = $(DEFAULT_BUILD_TAG)
endif
CL_RED = "\033[0;31m"
CL_BLUE = "\033[0;34m"
CL_GREEN = "\033[0;32m"
CL_ORANGE = "\033[0;33m"
CL_NONE = "\033[0m"
define color_out
@echo $(1)$(2)$(CL_NONE)
endef
docker-build:
@go mod vendor
$(call color_out,$(CL_BLUE),"Building binary in docker ...")
@docker run --rm -v "$(PWD)":/go/src/$(PACKAGE) \
-w /go/src/$(PACKAGE) \
golang:$(BUILD_TAG) \
go build -v -o $(NAME) $(MAIN)
$(call color_out,$(CL_GREEN),"Building binary ok")
docker: docker-build
$(call color_out,$(CL_BLUE),"Building docker image ...")
@docker build -t $(DEFAULT_TAG) .
$(call color_out,$(CL_GREEN),"Building docker image ok")
push: docker
@docker tag $(DEFAULT_TAG) $(REMOTE_IMAGE_TAG)
$(call color_out,$(CL_BLUE),"Pushing image $(REMOTE_IMAGE_TAG) ...")
@docker push $(REMOTE_IMAGE_TAG)
$(call color_out,$(CL_ORANGE),"Done")
build:
@go mod vendor
@go build $(BUILD_FLAGS)
linux:
@go mod vendor
@GOOS=linux GOARCH=amd64 go build $(BUILD_FLAGS)
proto:
# If build proto failed, make sure you have protoc installed and:
# go get -u github.com/google/protobuf
# go get -u github.com/golang/protobuf/protoc-gen-go
# go install github.com/mwitkow/go-proto-validators/protoc-gen-govalidators
# mkdir -p ${GOPATH}/src/github.com/googleapis && git clone [email protected]:googleapis/googleapis.git ${GOPATH}/src/github.com/googleapis/
# Building proto for Golang
@protoc \
--proto_path=${GOPATH}/src \
--proto_path=. \
--go_out=plugins=grpc:$(PWD)/pb \
--govalidators_out=$(PWD)/pb \
demo.proto
$(call color_out,$(CL_ORANGE),"Done")
mock:
# go get github.com/golang/mock/gomock
# Source Mode
@mockgen -package=mocks -destination internal/tests/mocks/demo.go -source=demo.go
# Reflect Mode. 当 Interface 有 embedded interface 时反射模式好用
# @mockgen -package=mocks -destination internal/tests/mocks/demo.go . DemoService
# 简化写法
# @mockgen -package=mocks -destination internal/tests/mocks/demo.go \
# github.com/xgxw/toddler-go DemoService
.PHONY: all
all:
build