130 lines
3.1 KiB
Go
Raw Normal View History

2016-11-04 21:45:25 +01:00
package main
import (
"bytes"
2016-11-07 08:51:13 +01:00
"log"
2016-11-07 09:23:59 +01:00
"os"
2016-11-06 17:20:57 +01:00
"path/filepath"
"text/template"
2016-11-07 09:23:59 +01:00
"time"
2016-11-06 17:20:57 +01:00
2016-11-04 21:45:25 +01:00
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/golang/protobuf/protoc-gen-go/plugin"
2016-11-06 17:20:57 +01:00
"github.com/kr/fs"
"github.com/moul/funcmap"
2016-11-04 21:45:25 +01:00
)
type GenericTemplateBasedEncoder struct {
2016-11-06 17:20:57 +01:00
templateDir string
service *descriptor.ServiceDescriptorProto
file *descriptor.FileDescriptorProto
2016-11-07 08:45:02 +01:00
debug bool
2016-11-04 21:45:25 +01:00
}
type Ast struct {
2016-11-07 09:23:59 +01:00
BuildDate time.Time `json:"build-date"`
BuildHostname string `json:"build-hostname"`
BuildUser string `json:"build-user"`
Debug bool `json:"debug"`
File *descriptor.FileDescriptorProto `json:"file"`
Filename string `json:"filename"`
Service *descriptor.ServiceDescriptorProto `json:"service"`
}
2016-11-07 08:45:02 +01:00
func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool) (e *GenericTemplateBasedEncoder) {
2016-11-04 21:45:25 +01:00
e = &GenericTemplateBasedEncoder{
2016-11-06 17:20:57 +01:00
service: service,
file: file,
templateDir: templateDir,
2016-11-07 08:45:02 +01:00
debug: debug,
2016-11-04 21:45:25 +01:00
}
2016-11-07 08:51:13 +01:00
if debug {
log.Printf("new encoder: file=%q service=%q template-dir=%q", templateDir, service.GetName(), file.GetName())
}
2016-11-04 21:45:25 +01:00
return
}
2016-11-06 17:20:57 +01:00
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
}
2016-11-07 08:51:13 +01:00
if e.debug {
log.Printf("new template: %q", rel)
}
2016-11-06 17:20:57 +01:00
filenames = append(filenames, rel)
}
return filenames, nil
}
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, error) {
fullPath := filepath.Join(e.templateDir, templateFilename)
tmpl, err := template.New(templateFilename).Funcs(funcmap.FuncMap).ParseFiles(fullPath)
if err != nil {
return "", err
}
2016-11-07 09:23:59 +01:00
hostname, _ := os.Hostname()
ast := Ast{
2016-11-07 09:23:59 +01:00
BuildDate: time.Now(),
BuildHostname: hostname,
BuildUser: os.Getenv("USER"),
File: e.file,
Filename: templateFilename,
Service: e.service,
}
buffer := new(bytes.Buffer)
if err := tmpl.Execute(buffer, ast); err != nil {
return "", err
}
return buffer.String(), nil
}
2016-11-04 21:45:25 +01:00
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
2016-11-06 17:20:57 +01:00
files := []*plugin_go.CodeGeneratorResponse_File{}
templates, err := e.templates()
if err != nil {
2016-11-07 08:51:13 +01:00
log.Fatalf("cannot get templates from %q: %v", e.templateDir, err)
2016-11-06 17:20:57 +01:00
}
for _, templateFilename := range templates {
filename := templateFilename[0 : len(templateFilename)-len(".tmpl")]
content, err := e.buildContent(templateFilename)
if err != nil {
panic(err)
}
2016-11-06 17:20:57 +01:00
files = append(files, &plugin_go.CodeGeneratorResponse_File{
2016-11-04 21:45:25 +01:00
Content: &content,
2016-11-06 17:20:57 +01:00
Name: &filename,
})
2016-11-04 21:45:25 +01:00
}
2016-11-06 17:20:57 +01:00
return files
2016-11-04 21:45:25 +01:00
}