From 99d310ac7936f966d24bfe92275b2685749306ea Mon Sep 17 00:00:00 2001 From: gfanton Date: Tue, 17 Jan 2017 11:48:50 +0100 Subject: [PATCH 1/2] Concatenate if file already exist --- main.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 9f1a6e6..87d74e9 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "github.com/golang/protobuf/proto" "github.com/golang/protobuf/protoc-gen-go/generator" + "github.com/golang/protobuf/protoc-gen-go/plugin" ) func main() { @@ -71,17 +72,32 @@ func main() { } } + tmplMap := make(map[string]*plugin_go.CodeGeneratorResponse_File) + concatOrAppend := func(file *plugin_go.CodeGeneratorResponse_File) { + if val, ok := tmplMap[*file.Name]; ok { + *val.Content += *file.Content + } else { + tmplMap[*file.Name] = file + g.Response.File = append(g.Response.File, file) + } + } + // Generate the encoders for _, file := range g.Request.GetProtoFile() { if all { encoder := NewGenericTemplateBasedEncoder(templateDir, file, debug, destinationDir) - g.Response.File = append(g.Response.File, encoder.Files()...) + for _, tmpl := range encoder.Files() { + concatOrAppend(tmpl) + } + continue } for _, service := range file.GetService() { encoder := NewGenericServiceTemplateBasedEncoder(templateDir, service, file, debug, destinationDir) - g.Response.File = append(g.Response.File, encoder.Files()...) + for _, tmpl := range encoder.Files() { + concatOrAppend(tmpl) + } } } From fee245f5d4ddf788676fb5b7d9578ead318c8377 Mon Sep 17 00:00:00 2001 From: gfanton Date: Tue, 17 Jan 2017 16:07:38 +0100 Subject: [PATCH 2/2] Add concat example --- examples/concat/Makefile | 13 +++++++++++++ examples/concat/output/concat.txt | 3 +++ examples/concat/output/static.txt | 1 + examples/concat/proto/Eric.proto | 2 ++ examples/concat/proto/Francis.proto | 2 ++ examples/concat/proto/arnold.proto | 2 ++ examples/concat/templates/concat.txt.tmpl | 1 + examples/concat/templates/static.txt.tmpl | 1 + 8 files changed, 25 insertions(+) create mode 100644 examples/concat/Makefile create mode 100644 examples/concat/output/concat.txt create mode 100644 examples/concat/output/static.txt create mode 100644 examples/concat/proto/Eric.proto create mode 100644 examples/concat/proto/Francis.proto create mode 100644 examples/concat/proto/arnold.proto create mode 100644 examples/concat/templates/concat.txt.tmpl create mode 100644 examples/concat/templates/static.txt.tmpl diff --git a/examples/concat/Makefile b/examples/concat/Makefile new file mode 100644 index 0000000..60bc04f --- /dev/null +++ b/examples/concat/Makefile @@ -0,0 +1,13 @@ +.PHONY: build +build: + mkdir -p output + protoc -I. --gotemplate_out=template_dir=templates,debug=true,all=true:output proto/*.proto + + +.PHONY: re +re: clean build + + +.PHONY: clean +clean: + rm -rf output diff --git a/examples/concat/output/concat.txt b/examples/concat/output/concat.txt new file mode 100644 index 0000000..83e9930 --- /dev/null +++ b/examples/concat/output/concat.txt @@ -0,0 +1,3 @@ +I'm Eric +I'm Francis +I'm Arnold diff --git a/examples/concat/output/static.txt b/examples/concat/output/static.txt new file mode 100644 index 0000000..410455f --- /dev/null +++ b/examples/concat/output/static.txt @@ -0,0 +1 @@ +This is static text.This is static text.This is static text. \ No newline at end of file diff --git a/examples/concat/proto/Eric.proto b/examples/concat/proto/Eric.proto new file mode 100644 index 0000000..ce20a4b --- /dev/null +++ b/examples/concat/proto/Eric.proto @@ -0,0 +1,2 @@ +syntax = "proto3"; +package Eric; diff --git a/examples/concat/proto/Francis.proto b/examples/concat/proto/Francis.proto new file mode 100644 index 0000000..c381e5c --- /dev/null +++ b/examples/concat/proto/Francis.proto @@ -0,0 +1,2 @@ +syntax = "proto3"; +package Francis; diff --git a/examples/concat/proto/arnold.proto b/examples/concat/proto/arnold.proto new file mode 100644 index 0000000..f284210 --- /dev/null +++ b/examples/concat/proto/arnold.proto @@ -0,0 +1,2 @@ +syntax = "proto3"; +package Arnold; diff --git a/examples/concat/templates/concat.txt.tmpl b/examples/concat/templates/concat.txt.tmpl new file mode 100644 index 0000000..308309e --- /dev/null +++ b/examples/concat/templates/concat.txt.tmpl @@ -0,0 +1 @@ +I'm {{.File.Package}} diff --git a/examples/concat/templates/static.txt.tmpl b/examples/concat/templates/static.txt.tmpl new file mode 100644 index 0000000..82165d2 --- /dev/null +++ b/examples/concat/templates/static.txt.tmpl @@ -0,0 +1 @@ +This is static text. \ No newline at end of file