Iterate over files in templates dir
This commit is contained in:
parent
ef33066c3c
commit
39a1644678
59
encoder.go
59
encoder.go
@ -1,32 +1,73 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||||
"github.com/golang/protobuf/protoc-gen-go/plugin"
|
"github.com/golang/protobuf/protoc-gen-go/plugin"
|
||||||
|
"github.com/kr/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GenericTemplateBasedEncoder struct {
|
type GenericTemplateBasedEncoder struct {
|
||||||
|
templateDir string
|
||||||
service *descriptor.ServiceDescriptorProto
|
service *descriptor.ServiceDescriptorProto
|
||||||
file *descriptor.FileDescriptorProto
|
file *descriptor.FileDescriptorProto
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGenericTemplateBasedEncoder(service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto) (e *GenericTemplateBasedEncoder) {
|
func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto) (e *GenericTemplateBasedEncoder) {
|
||||||
e = &GenericTemplateBasedEncoder{
|
e = &GenericTemplateBasedEncoder{
|
||||||
service: service,
|
service: service,
|
||||||
file: file,
|
file: file,
|
||||||
|
templateDir: templateDir,
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
|
||||||
|
filenames := []string{}
|
||||||
|
|
||||||
|
walker := fs.Walk(e.templateDir)
|
||||||
|
for walker.Step() {
|
||||||
|
if err := walker.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if walker.Stat().IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if filepath.Ext(walker.Path()) != ".tmpl" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
rel, err := filepath.Rel(e.templateDir, walker.Path())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
filenames = append(filenames, rel)
|
||||||
|
}
|
||||||
|
|
||||||
|
return filenames, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
|
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
|
||||||
//log.Printf("file: %v\n", e.file)
|
files := []*plugin_go.CodeGeneratorResponse_File{}
|
||||||
//log.Printf("service: %v\n", e.service)
|
|
||||||
var content string = "hello world"
|
templates, err := e.templates()
|
||||||
var fileName string = "test.txt"
|
if err != nil {
|
||||||
return []*plugin_go.CodeGeneratorResponse_File{
|
panic(err)
|
||||||
&plugin_go.CodeGeneratorResponse_File{
|
}
|
||||||
|
|
||||||
|
for _, templateFilename := range templates {
|
||||||
|
filename := templateFilename[0 : len(templateFilename)-len(".tmpl")]
|
||||||
|
|
||||||
|
content := "hello world"
|
||||||
|
files = append(files, &plugin_go.CodeGeneratorResponse_File{
|
||||||
Content: &content,
|
Content: &content,
|
||||||
Name: &fileName,
|
Name: &filename,
|
||||||
},
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return files
|
||||||
}
|
}
|
||||||
|
2
main.go
2
main.go
@ -29,7 +29,7 @@ func main() {
|
|||||||
// Generate the clients
|
// Generate the clients
|
||||||
for _, file := range g.Request.GetProtoFile() {
|
for _, file := range g.Request.GetProtoFile() {
|
||||||
for _, service := range file.GetService() {
|
for _, service := range file.GetService() {
|
||||||
encoder := NewGenericTemplateBasedEncoder(service, file)
|
encoder := NewGenericTemplateBasedEncoder("templates", service, file)
|
||||||
g.Response.File = append(g.Response.File, encoder.Files()...)
|
g.Response.File = append(g.Response.File, encoder.Files()...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user