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 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) + } } }