commit
14d2af18a7
128
encoder.go
128
encoder.go
@ -11,7 +11,6 @@ import (
|
|||||||
|
|
||||||
"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"
|
|
||||||
"github.com/moul/funcmap"
|
"github.com/moul/funcmap"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,7 +25,7 @@ type Ast struct {
|
|||||||
BuildDate time.Time `json:"build-date"`
|
BuildDate time.Time `json:"build-date"`
|
||||||
BuildHostname string `json:"build-hostname"`
|
BuildHostname string `json:"build-hostname"`
|
||||||
BuildUser string `json:"build-user"`
|
BuildUser string `json:"build-user"`
|
||||||
GoPWD string `json:"go-pwd",omitempty`
|
GoPWD string `json:"go-pwd,omitempty"`
|
||||||
PWD string `json:"pwd"`
|
PWD string `json:"pwd"`
|
||||||
Debug bool `json:"debug"`
|
Debug bool `json:"debug"`
|
||||||
File *descriptor.FileDescriptorProto `json:"file"`
|
File *descriptor.FileDescriptorProto `json:"file"`
|
||||||
@ -53,32 +52,27 @@ func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.Serv
|
|||||||
func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
|
func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
|
||||||
filenames := []string{}
|
filenames := []string{}
|
||||||
|
|
||||||
walker := fs.Walk(e.templateDir)
|
err := filepath.Walk(e.templateDir, func(path string, info os.FileInfo, err error) error {
|
||||||
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 {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
|
}
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if filepath.Ext(path) != ".tmpl" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
rel, err := filepath.Rel(e.templateDir, path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
if e.debug {
|
if e.debug {
|
||||||
log.Printf("new template: %q", rel)
|
log.Printf("new template: %q", rel)
|
||||||
}
|
}
|
||||||
|
|
||||||
filenames = append(filenames, rel)
|
filenames = append(filenames, rel)
|
||||||
}
|
return nil
|
||||||
|
})
|
||||||
return filenames, nil
|
return filenames, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) {
|
func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) {
|
||||||
@ -100,84 +94,78 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err
|
|||||||
GoPWD: goPwd,
|
GoPWD: goPwd,
|
||||||
File: e.file,
|
File: e.file,
|
||||||
RawFilename: templateFilename,
|
RawFilename: templateFilename,
|
||||||
// Filename: "",
|
Filename: "",
|
||||||
Service: e.service,
|
Service: e.service,
|
||||||
}
|
}
|
||||||
|
buffer := new(bytes.Buffer)
|
||||||
|
tmpl, err := template.New("").Funcs(funcmap.FuncMap).Parse(templateFilename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := tmpl.Execute(buffer, ast); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ast.Filename = buffer.String()
|
||||||
return &ast, nil
|
return &ast, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, error) {
|
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, string, error) {
|
||||||
// initialize template engine
|
// initialize template engine
|
||||||
fullPath := filepath.Join(e.templateDir, templateFilename)
|
fullPath := filepath.Join(e.templateDir, templateFilename)
|
||||||
templateName := filepath.Base(fullPath)
|
templateName := filepath.Base(fullPath)
|
||||||
tmpl, err := template.New(templateName).Funcs(funcmap.FuncMap).ParseFiles(fullPath)
|
tmpl, err := template.New(templateName).Funcs(funcmap.FuncMap).ParseFiles(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
ast, err := e.genAst(templateFilename)
|
ast, err := e.genAst(templateFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
|
||||||
|
|
||||||
// translate the filename
|
|
||||||
ast.Filename, err = e.translateString(templateFilename, templateFilename)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate the content
|
// generate the content
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
if err := tmpl.Execute(buffer, ast); err != nil {
|
if err := tmpl.Execute(buffer, ast); err != nil {
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return buffer.String(), nil
|
return buffer.String(), ast.Filename, nil
|
||||||
}
|
|
||||||
|
|
||||||
func (e *GenericTemplateBasedEncoder) translateString(input string, templateFilename string) (string, error) {
|
|
||||||
buffer := new(bytes.Buffer)
|
|
||||||
tmpl, err := template.New("").Funcs(funcmap.FuncMap).Parse(input)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
ast, err := e.genAst(templateFilename)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := tmpl.Execute(buffer, ast); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return buffer.String(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
|
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
|
||||||
files := []*plugin_go.CodeGeneratorResponse_File{}
|
|
||||||
|
|
||||||
templates, err := e.templates()
|
templates, err := e.templates()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("cannot get templates from %q: %v", e.templateDir, err)
|
log.Fatalf("cannot get templates from %q: %v", e.templateDir, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
length := len(templates)
|
||||||
|
files := make([]*plugin_go.CodeGeneratorResponse_File, 0, length)
|
||||||
|
errChan := make(chan error, length)
|
||||||
|
resultChan := make(chan *plugin_go.CodeGeneratorResponse_File, length)
|
||||||
for _, templateFilename := range templates {
|
for _, templateFilename := range templates {
|
||||||
content, err := e.buildContent(templateFilename)
|
go func(tmpl string) {
|
||||||
if err != nil {
|
content, translatedFilename, err := e.buildContent(tmpl)
|
||||||
panic(err)
|
if err != nil {
|
||||||
}
|
errChan <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
filename := translatedFilename[:len(translatedFilename)-len(".tmpl")]
|
||||||
|
|
||||||
translatedFilename, err := e.translateString(templateFilename, templateFilename)
|
resultChan <- &plugin_go.CodeGeneratorResponse_File{
|
||||||
if err != nil {
|
Content: &content,
|
||||||
panic(err)
|
Name: &filename,
|
||||||
}
|
}
|
||||||
filename := translatedFilename[0 : len(translatedFilename)-len(".tmpl")]
|
}(templateFilename)
|
||||||
|
}
|
||||||
files = append(files, &plugin_go.CodeGeneratorResponse_File{
|
for i := 0; i < length; i++ {
|
||||||
Content: &content,
|
select {
|
||||||
Name: &filename,
|
case f := <-resultChan:
|
||||||
})
|
files = append(files, f)
|
||||||
|
case err = <-errChan:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
6
main.go
6
main.go
@ -43,9 +43,11 @@ func main() {
|
|||||||
templateDir = parts[1]
|
templateDir = parts[1]
|
||||||
break
|
break
|
||||||
case "debug":
|
case "debug":
|
||||||
if parts[1] == "true" {
|
switch strings.ToLower(parts[1]) {
|
||||||
|
case "true", "t":
|
||||||
debug = true
|
debug = true
|
||||||
} else {
|
case "false", "f":
|
||||||
|
default:
|
||||||
log.Printf("Err: invalid value for debug: %q", parts[1])
|
log.Printf("Err: invalid value for debug: %q", parts[1])
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
Loading…
x
Reference in New Issue
Block a user