-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: code generation works with new generator engine
- Loading branch information
daniel vladco
committed
Nov 30, 2020
1 parent
dabc07a
commit 3cf14e1
Showing
85 changed files
with
23,418 additions
and
7,515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
.PHONY: install example | ||
|
||
install: | ||
go install github.com/golang/protobuf/protoc-gen-go | ||
protoc --go_out=paths=source_relative:./pb -I=./pb ./pb/*.proto | ||
go install ./protoc-gen-gql | ||
go install ./protoc-gen-gogqlgen | ||
go install ./protoc-gen-gqlgencfg | ||
|
||
example: | ||
protoc \ | ||
--go_out=plugins=grpc,paths=source_relative:. \ | ||
--gqlgencfg_out=paths=source_relative:. \ | ||
--gql_out=svcdir=true,paths=source_relative:. \ | ||
--gogqlgen_out=paths=source_relative,gogoimport=false:. \ | ||
-I=. -I=./example/ ./example/*.proto | ||
|
||
example2: | ||
protoc --go_out=plugins=grpc,paths=source_relative:. -I=. -I=./reflect/examples/optionsserver/pb/ ./reflect/examples/optionsserver/pb/*.proto | ||
# Go tools dependencies | ||
GO_TOOLS := \ | ||
google.golang.org/grpc/cmd/protoc-gen-go-grpc \ | ||
google.golang.org/protobuf/cmd/protoc-gen-go \ | ||
github.com/99designs/gqlgen \ | ||
./protoc-gen-gql \ | ||
./protoc-gen-gogql | ||
|
||
GOPATH := $(shell go env GOPATH) | ||
|
||
.PHONY: all | ||
all: all-tools pb/graphql.pb.go | ||
|
||
.PHONY: all-tools | ||
all-tools: ${GOPATH}/bin/protoc go-tools | ||
|
||
.PHONY: go-tools | ||
go-tools: $(foreach l, ${GO_TOOLS}, ${GOPATH}/bin/$(notdir $(l))) | ||
|
||
define LIST_RULE | ||
${GOPATH}/bin/$(notdir $(1)): go.mod | ||
go install $(1) | ||
endef | ||
|
||
$(foreach l, $(GO_TOOLS), $(eval $(call LIST_RULE, $(l) ))) | ||
|
||
pb/graphql.pb.go: ./pb/graphql.proto all-tools | ||
protoc --go_out=paths=source_relative:. ./pb/graphql.proto | ||
|
||
${GOPATH}/bin/protoc: | ||
./scripts/install-protoc.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/jhump/protoreflect/desc/protoparse" | ||
"github.com/vektah/gqlparser/v2/formatter" | ||
|
||
"github.com/danielvladco/go-proto-gql/pkg/generator" | ||
) | ||
|
||
type arrayFlags []string | ||
|
||
func (i *arrayFlags) String() string { | ||
return "str list" | ||
} | ||
|
||
func (i *arrayFlags) Set(value string) error { | ||
*i = append(*i, value) | ||
return nil | ||
} | ||
|
||
var ( | ||
importPath = arrayFlags{} | ||
fileNames = arrayFlags{} | ||
svc = flag.Bool("svc", false, "") | ||
merge = flag.Bool("merge", false, "") | ||
) | ||
|
||
func main() { | ||
flag.Var(&importPath, "I", "path") | ||
flag.Var(&fileNames, "f", "path") | ||
flag.Parse() | ||
|
||
newFileNames, err := protoparse.ResolveFilenames(importPath, fileNames...) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
descs, err := protoparse.Parser{ImportPaths: importPath}.ParseFiles(newFileNames...) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
gqlDesc, err := generator.NewSchemas(descs, *merge, *svc) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for _, schema := range gqlDesc { | ||
if len(schema.FileDescriptors) < 1 { | ||
log.Fatalf("unexpected number of proto descriptors: %d for gql schema", len(schema.FileDescriptors)) | ||
} | ||
if len(schema.FileDescriptors) > 1 { | ||
if err := generateFile(schema, true); err != nil { | ||
log.Fatal(err) | ||
} | ||
break | ||
} | ||
if err := generateFile(schema, *merge); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
func generateFile(schema *generator.SchemaDescriptor, merge bool) error { | ||
sc, err := os.Create(resolveGraphqlFilename(schema.FileDescriptors[0].GetName(), merge)) | ||
if err != nil { | ||
return err | ||
} | ||
defer sc.Close() | ||
|
||
formatter.NewFormatter(sc).FormatSchema(schema.AsGraphql()) | ||
return nil | ||
} | ||
|
||
func resolveGraphqlFilename(protoFileName string, merge bool) string { | ||
if merge { | ||
gqlFileName := "schema.graphqls" | ||
absProtoFileName, err := filepath.Abs(protoFileName) | ||
if err == nil { | ||
protoDirSlice := strings.Split(filepath.Dir(absProtoFileName), string(filepath.Separator)) | ||
if len(protoDirSlice) > 0 { | ||
gqlFileName = protoDirSlice[len(protoDirSlice)-1] + ".graphqls" | ||
} | ||
} | ||
protoDir, _ := path.Split(protoFileName) | ||
return path.Join(protoDir, gqlFileName) | ||
} | ||
|
||
return strings.TrimSuffix(protoFileName, path.Ext(protoFileName)) + ".graphqls" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.PHONY: generate start | ||
|
||
start: generate | ||
go run ./main.go | ||
|
||
generate: gql/constructs/generated.go gql/options/generated.go | ||
|
||
pb/%.gqlgen.pb.go: pb/%.proto pb/%_grpc.pb.go pb/%.pb.go | ||
protoc --gogql_out=paths=source_relative:. -I . -I ../../ ./pb/$*.proto | ||
|
||
pb/%_grpc.pb.go: pb/%.proto pb/%.pb.go | ||
protoc --go-grpc_out=paths=source_relative:. -I . -I ../../ ./pb/$*.proto | ||
|
||
pb/%.pb.go: pb/%.proto | ||
protoc --go_out=paths=source_relative:. -I . -I ../../ ./pb/$*.proto | ||
|
||
pb/%.graphqls: pb/%.proto | ||
protoc --gql_out=svc=true:. -I . -I ../../ ./pb/$*.proto | ||
|
||
gql/%/generated.go: pb/%.graphqls pb/%.gqlgen.pb.go | ||
gqlgen --config ./gqlgen-$*.yaml | ||
|
Oops, something went wrong.