-
Notifications
You must be signed in to change notification settings - Fork 44
/
Makefile
181 lines (130 loc) · 4.32 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
169
170
171
172
173
174
175
176
177
178
179
180
181
PATH := $(CURDIR)/bin:$(PATH)
MODULES := video comment
BUILD_DIR := bin/app
BUILD_STATIC_DIR := $(BUILD_DIR)/static
STATIC_DIRS := $(wildcard modules/*/migration)
DOCKER_COMPOSE := $(or $(DOCKER_COMPOSE),$(DOCKER_COMPOSE),docker compose)
####################################################################################################
### Automatically include components' extensions and ad-hoc rules (makefile.mk)
###
-include */makefile.mk
.PHONY: clean
clean:
rm -rf bin/*
####################################################################################################
### Rule for the `generate` command
###
define make-dc-generate-rules
.PHONY: dc.$1.generate
# to generate individual module, override the command defined in the docker-compose.yml file
dc.$1.generate:
$(DOCKER_COMPOSE) run --rm generate make $1.generate
endef
$(foreach module,$(MODULES),$(eval $(call make-dc-generate-rules,$(module))))
.PHONY: dc.pkg.generate
dc.pkg.generate:
$(DOCKER_COMPOSE) run --rm generate make pkg.generate
.PHONY: dc.generate
dc.generate:
$(DOCKER_COMPOSE) run --rm generate
define make-generate-rules
.PHONY: $1.generate
$1.generate: bin/protoc-gen-go bin/protoc-gen-go-grpc bin/protoc-gen-grpc-gateway bin/protoc-gen-grpc-sarama bin/mockgen
protoc \
-I . \
-I ./pkg/pb \
-I $(dir $(shell (go list -f '{{ .Dir }}' github.com/justin0u0/protoc-gen-grpc-sarama/proto))) \
--go_out=paths=source_relative:. \
--go-grpc_out=paths=source_relative:. \
--grpc-gateway_out=paths=source_relative:. \
--grpc-sarama_out=paths=source_relative:. \
./modules/$1/pb/*.proto
go generate ./modules/$1/...
endef
$(foreach module,$(MODULES),$(eval $(call make-generate-rules,$(module))))
.PHONY: pkg.generate
pkg.generate: bin/protoc-gen-go bin/protoc-gen-go-grpc bin/protoc-gen-grpc-gateway bin/protoc-gen-grpc-sarama bin/mockgen
go generate ./pkg/...
.PHONY: generate
generate: pkg.generate $(addsuffix .generate,$(MODULES))
bin/protoc-gen-go: go.mod
go build -o $@ google.golang.org/protobuf/cmd/protoc-gen-go
bin/protoc-gen-go-grpc: go.mod
go build -o $@ google.golang.org/grpc/cmd/protoc-gen-go-grpc
bin/protoc-gen-grpc-gateway: go.mod
go build -o $@ github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
bin/protoc-gen-grpc-sarama: go.mod
go build -o $@ github.com/justin0u0/protoc-gen-grpc-sarama
bin/mockgen: go.mod
go build -o $@ github.com/golang/mock/mockgen
####################################################################################################
### Rule for the `lint` command
###
define make-dc-lint-rules
.PHONY: dc.$1.lint
dc.$1.lint:
$(DOCKER_COMPOSE) run --rm lint make $1.lint
endef
$(foreach module,$(MODULES),$(eval $(call make-dc-lint-rules,$(module))))
.PHONY: dc.pkg.lint
dc.pkg.lint:
$(DOCKER_COMPOSE) run --rm lint make pkg.lint
.PHONY: dc.lint
dc.lint:
$(DOCKER_COMPOSE) run --rm lint
define make-lint-rules
.PHONY: $1.lint
$1.lint:
golangci-lint run ./modules/$1/...
endef
$(foreach module,$(MODULES),$(eval $(call make-lint-rules,$(module))))
.PHONY: pkg.lint
pkg.lint:
golangci-lint run ./pkg/...
.PHONY: lint
lint:
golangci-lint run ./...
####################################################################################################
### Rule for the `test` command
###
define make-dc-test-rules
.PHONY: dc.$1.test
dc.$1.test:
$(DOCKER_COMPOSE) run --rm test make $1.test
endef
$(foreach module,$(MODULES),$(eval $(call make-dc-test-rules,$(module))))
.PHONY: dc.pkg.test
dc.pkg.test:
$(DOCKER_COMPOSE) run --rm test make pkg.test
.PHONY: dc.test
dc.test:
$(DOCKER_COMPOSE) run --rm test
define make-test-rules
.PHONY: $1.test
$1.test:
go test -v -race ./modules/$1/...
endef
$(foreach module,$(MODULES),$(eval $(call make-test-rules,$(module))))
.PHONY: pkg.test
pkg.test:
go test -v -race ./pkg/...
.PHONY: test
test: pkg.test $(addsuffix .test,$(MODULES))
####################################################################################################
### Rule for the `build` command
###
.PHONY: dc.image
dc.image: dc.build
$(DOCKER_COMPOSE) build --force-rm image
.PHONY: dc.build
dc.build:
$(DOCKER_COMPOSE) run --rm build
.PHONY: build
build: $(STATIC_DIRS)
@mkdir -p $(BUILD_DIR)
go build -o $(BUILD_DIR)/cmd ./cmd/main.go
.PHONY: $(STATIC_DIRS)
.SECONDEXPANSION:
$(STATIC_DIRS): %: $$(wildcard %/*)
@mkdir -p $(BUILD_STATIC_DIR)/$@
cp -R $@/. $(BUILD_STATIC_DIR)/$@