-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
135 lines (113 loc) · 3.53 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
SHELL:=/usr/bin/env bash
DOCKER_NAME?=$(notdir $(shell pwd))
OPENAPI_GENERATOR_CLI_VERSION?=latest
OPENAPI_CLIENT?=\
c\
dart-dio\
dart\
go\
python\
rust\
typescript-angular\
typescript-fetch\
OPENAPI_SERVER?=\
go-server\
python-aiohttp\
rust-server\
KEEP?=\
grpc/python/.keep\
ogen/.keep\
GO:=go
GOM:=GO111MODULE=on $(GO)
GOPATH:=$(shell $(GO) env GOPATH)
PROTO_DIR:=pb
PROTO:=$(shell find $(PROTO_DIR) -type d -name .git -prune -or -type f -name "*.proto" -print)
PROTO_GO:=$(shell find $(PROTO_DIR) -type f -name "*.go" -print)
PB_GO:=$(PROTO:.proto=.pb.go)
PB_GO_VALIDATE:=$(PROTO:.proto=.pb.validate.go)
PB_GO_GQLGEN:=$(PROTO:.proto=.gqlgen.pb.go)
GOSRC:=$(PB_GO) $(PB_GO_VALIDATE) $(PB_GO_GQLGEN)
BUF_IMAGE:=buf-image.bin
GRAPH_DIR:=graph
GRAPHQLS:=$(patsubst $(PROTO_DIR)/%.proto,$(GRAPH_DIR)/%.pb.graphqls,$(PROTO))
GQLGEN:=$(patsubst $(PROTO_DIR)/%.proto,$(GRAPH_DIR)/%.gqlgen.pb.yml,$(PROTO))
GRAPH:=$(GRAPHQLS) $(GQLGEN) gqlgen.yml
.PHONY: all
all:\
apidocs.swagger.yaml\
openapi.yaml\
$(BUF_IMAGE)\
$(GOSRC)\
$(GRAPH)\
$(KEEP)\
$(addprefix openapi-client/,$(addsuffix /.openapi-generator-ignore,$(OPENAPI_CLIENT)))\
$(addprefix openapi-server/,$(addsuffix /.openapi-generator-ignore,$(OPENAPI_SERVER)))\
find . -name "*.go" -exec gofmt -w {} \;
.PHONY: clean
clean:
find $(PROTO_DIR) -name "*.pb*.go" -delete
find . -depth 1 -type f -name "*.swagger.*" -delete
find . -depth 1 -type f -name "openapi.*" -delete
rm -rf\
$(BUF_IMAGE)\
gqlgen.yml\
graph\
$(KEEP)\
grpc/python\
ogen/oas\
openapi-client\
openapi-server\
;
$(BUF_IMAGE): $(PROTO)
buf lint
buf format --write $(PROTO_DIR)
buf build -o $@
%.pb.go: %.proto
buf generate
%.pb.validate.go: %.proto
buf generate
%.gqlgen.pb.go: %.proto $(BUF_IMAGE)
( type protoc > /dev/null 2>&1 ) && protoc\
--descriptor_set_in=$(BUF_IMAGE)\
--gogqlgen_out=gogoimport=false,paths=source_relative:$(PROTO_DIR)\
$(patsubst $(PROTO_DIR)/%,%,$(PROTO))
$(GRAPH_DIR):
mkdir -p $@
$(GRAPHQLS): $(GRAPH_DIR) $(BUF_IMAGE)
( type protoc > /dev/null 2>&1 ) && protoc\
--descriptor_set_in=$(BUF_IMAGE)\
--gql_out=paths=source_relative:$(GRAPH_DIR)\
$(patsubst $(PROTO_DIR)/%,%,$(PROTO))
$(GQLGEN): $(GRAPH_DIR) $(BUF_IMAGE)
( type protoc > /dev/null 2>&1 ) && protoc\
--descriptor_set_in=$(BUF_IMAGE)\
--gqlgencfg_out=paths=source_relative:$(GRAPH_DIR)\
$(patsubst $(PROTO_DIR)/%,%,$(PROTO))
gqlgen.yml: $(GQLGEN)
[ -f $@ ] || echo 'schema:' > $@
yq eval-all '. as $$item ireduce ({}; . * $$item )' $(GQLGEN) $@ > $@.tmp
yq --inplace --prettyPrint eval 'sort_keys(..)' $@.tmp
mv $@.tmp $@
grpc/python/.keep: $(PROTO)
target=$(shell dirname $@)\
;mkdir -p $${target}\
;buf generate
touch $@
ogen/.keep: openapi.yaml
( cd $(dir $@) && go generate ./... )
docker build --tag "$(DOCKER_NAME)-$(shell dirname $@)" $(dir $@)
touch $@
apidocs.swagger.json: $(BUF_IMAGE)
buf generate
apidocs.swagger.yaml: apidocs.swagger.json
yq --output-format=yaml --prettyPrint eval $< > $@
openapi.json: apidocs.swagger.json
npx swagger2openapi --outfile $@ $<
openapi.yaml: openapi.json
yq --output-format=yaml --prettyPrint eval $< > $@
openapi-client/%: openapi.json
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:$(OPENAPI_GENERATOR_CLI_VERSION) generate -i /local/$< -g $(shell basename $$(dirname $@)) -o /local/$(shell dirname $@)
touch $@
openapi-server/%: openapi.json
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:$(OPENAPI_GENERATOR_CLI_VERSION) generate -i /local/$< -g $(shell basename $$(dirname $@)) -o /local/$(shell dirname $@)
touch $@