Merge pull request #44 from gfanton/feature/concat
Concatenate if file already exist
This commit is contained in:
		
							
								
								
									
										13
									
								
								examples/concat/Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								examples/concat/Makefile
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||
							
								
								
									
										3
									
								
								examples/concat/output/concat.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								examples/concat/output/concat.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| I'm Eric | ||||
| I'm Francis | ||||
| I'm Arnold | ||||
							
								
								
									
										1
									
								
								examples/concat/output/static.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								examples/concat/output/static.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| This is static text.This is static text.This is static text. | ||||
							
								
								
									
										2
									
								
								examples/concat/proto/Eric.proto
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								examples/concat/proto/Eric.proto
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| syntax = "proto3"; | ||||
| package Eric; | ||||
							
								
								
									
										2
									
								
								examples/concat/proto/Francis.proto
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								examples/concat/proto/Francis.proto
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| syntax = "proto3"; | ||||
| package Francis; | ||||
							
								
								
									
										2
									
								
								examples/concat/proto/arnold.proto
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								examples/concat/proto/arnold.proto
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| syntax = "proto3"; | ||||
| package Arnold; | ||||
							
								
								
									
										1
									
								
								examples/concat/templates/concat.txt.tmpl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								examples/concat/templates/concat.txt.tmpl
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| I'm {{.File.Package}} | ||||
							
								
								
									
										1
									
								
								examples/concat/templates/static.txt.tmpl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								examples/concat/templates/static.txt.tmpl
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| This is static text. | ||||
							
								
								
									
										20
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								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) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user