Skip to content

Instantly share code, notes, and snippets.

@lukaszx0
Last active February 7, 2021 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaszx0/cb17e8af04eebfc6c97183f1572b325a to your computer and use it in GitHub Desktop.
Save lukaszx0/cb17e8af04eebfc6c97183f1572b325a to your computer and use it in GitHub Desktop.
proto + grpc + swagger makefile (golang)
PKG_PATH := pkg
PROTOC := protoc
PROTOS := $(sort $(shell find ./$(PKG_PATH) -type f -name '*.proto' -print))
GO_PROTO_SOURCES := $(PROTOS:%.proto=%.pb.go)
GW_PROTO_SOURCES := $(PROTOS:%.proto=%.pb.gw.go)
SWAGGER_SOURCES := $(PROTOS:%.proto=%.swagger.json)
PROTO_IMPORT_PATH := \
-I $(PKG_PATH) \
-I vendor/github.com/grpc-ecosystem/grpc-gateway/ \
-I vendor/github.com/gogo/googleapis/ \
-I vendor/
PROTO_MAPPINGS := \
Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types, \
Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types, \
Mgoogle/protobuf/empty.proto=github.com/gogo/protobuf/types, \
Mgoogle/api/annotations.proto=github.com/gogo/googleapis/google/api, \
Mgoogle/protobuf/field_mask.proto=github.com/gogo/protobuf/types
space :=
space +=
PROTO_MAPPINGS := $(subst $(space),,$(PROTO_MAPPINGS))
OPENAPI_PATH := third_party/openapi
%.pb.go: %.proto
$(PROTOC) $(PROTO_IMPORT_PATH) \
--gogo_out=plugins=grpc,paths=source_relative,$(PROTO_MAPPINGS):./$(PKG_PATH) \
$<
%.pb.gw.go: %.proto
$(PROTOC) $(PROTO_IMPORT_PATH) \
--grpc-gateway_out=allow_patch_feature=false,paths=source_relative,$(PROTO_MAPPINGS):./$(PKG_PATH) \
$<
%.swagger.json: %.proto $(OPENAPI_PATH)
$(PROTOC) $(PROTO_IMPORT_PATH) \
--swagger_out=$(OPENAPI_PATH) \
$<
$(OPENAPI_PATH):
@mkdir -p $@
.PHONY: swagger
swagger: $(SWAGGER_SOURCES)
.PHONY: proto
proto: $(GO_PROTO_SOURCES) $(GW_PROTO_SOURCES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment