2016-11-04 21:45:25 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-06 17:29:37 +01:00
|
|
|
"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"
|
2016-11-07 10:27:57 +01:00
|
|
|
"strings"
|
2016-11-06 17:29:37 +01:00
|
|
|
"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:29:37 +01:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2016-11-06 17:29:37 +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"`
|
2016-12-14 11:04:34 +01:00
|
|
|
GoPWD string `json:"go-pwd,omitempty"`
|
2016-11-07 10:27:57 +01:00
|
|
|
PWD string `json:"pwd"`
|
2016-11-07 09:23:59 +01:00
|
|
|
Debug bool `json:"debug"`
|
|
|
|
File *descriptor.FileDescriptorProto `json:"file"`
|
2016-11-09 18:38:32 +01:00
|
|
|
RawFilename string `json:"raw-filename"`
|
2016-11-07 09:23:59 +01:00
|
|
|
Filename string `json:"filename"`
|
|
|
|
Service *descriptor.ServiceDescriptorProto `json:"service"`
|
2016-11-06 17:29:37 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-11-09 18:12:00 +01:00
|
|
|
log.Printf("new encoder: file=%q service=%q template-dir=%q", file.GetName(), service.GetName(), templateDir)
|
2016-11-07 08:51:13 +01:00
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
|
2016-12-14 10:33:08 +01:00
|
|
|
err := filepath.Walk(e.templateDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-11-06 17:20:57 +01:00
|
|
|
}
|
2016-12-14 10:33:08 +01:00
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
2016-11-06 17:20:57 +01:00
|
|
|
}
|
2016-12-14 10:33:08 +01:00
|
|
|
if filepath.Ext(path) != ".tmpl" {
|
|
|
|
return nil
|
2016-11-06 17:20:57 +01:00
|
|
|
}
|
2016-12-14 10:33:08 +01:00
|
|
|
rel, err := filepath.Rel(e.templateDir, path)
|
2016-11-06 17:20:57 +01:00
|
|
|
if err != nil {
|
2016-12-14 10:33:08 +01:00
|
|
|
return err
|
2016-11-06 17:20:57 +01:00
|
|
|
}
|
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)
|
2016-12-14 10:33:08 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return filenames, err
|
2016-11-06 17:20:57 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 18:38:32 +01:00
|
|
|
func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) {
|
|
|
|
// prepare the ast passed to the template engine
|
2016-11-07 09:23:59 +01:00
|
|
|
hostname, _ := os.Hostname()
|
2016-11-07 10:27:57 +01:00
|
|
|
pwd, _ := os.Getwd()
|
|
|
|
goPwd := ""
|
|
|
|
if os.Getenv("GOPATH") != "" {
|
|
|
|
goPwd, _ = filepath.Rel(os.Getenv("GOPATH")+"/src", pwd)
|
|
|
|
if strings.Contains(goPwd, "../") {
|
|
|
|
goPwd = ""
|
|
|
|
}
|
|
|
|
}
|
2016-11-06 17:29:37 +01:00
|
|
|
ast := Ast{
|
2016-11-07 09:23:59 +01:00
|
|
|
BuildDate: time.Now(),
|
|
|
|
BuildHostname: hostname,
|
|
|
|
BuildUser: os.Getenv("USER"),
|
2016-11-07 10:27:57 +01:00
|
|
|
PWD: pwd,
|
|
|
|
GoPWD: goPwd,
|
2016-11-07 09:23:59 +01:00
|
|
|
File: e.file,
|
2016-11-09 18:38:32 +01:00
|
|
|
RawFilename: templateFilename,
|
2016-12-14 11:06:24 +01:00
|
|
|
Filename: "",
|
|
|
|
Service: e.service,
|
2016-11-09 18:38:32 +01:00
|
|
|
}
|
2016-12-14 11:06:24 +01:00
|
|
|
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()
|
2016-11-09 18:38:32 +01:00
|
|
|
return &ast, nil
|
|
|
|
}
|
|
|
|
|
2016-12-14 11:06:24 +01:00
|
|
|
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, string, error) {
|
2016-11-09 18:38:32 +01:00
|
|
|
// initialize template engine
|
|
|
|
fullPath := filepath.Join(e.templateDir, templateFilename)
|
|
|
|
templateName := filepath.Base(fullPath)
|
|
|
|
tmpl, err := template.New(templateName).Funcs(funcmap.FuncMap).ParseFiles(fullPath)
|
|
|
|
if err != nil {
|
2016-12-14 11:06:24 +01:00
|
|
|
return "", "", err
|
2016-11-09 18:38:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ast, err := e.genAst(templateFilename)
|
|
|
|
if err != nil {
|
2016-12-14 11:06:24 +01:00
|
|
|
return "", "", err
|
2016-11-06 17:29:37 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 18:38:32 +01:00
|
|
|
// generate the content
|
2016-11-06 17:29:37 +01:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
if err := tmpl.Execute(buffer, ast); err != nil {
|
2016-12-14 11:06:24 +01:00
|
|
|
return "", "", err
|
2016-11-09 18:38:32 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 11:06:24 +01:00
|
|
|
return buffer.String(), ast.Filename, nil
|
2016-11-09 18:38:32 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-11-06 17:29:37 +01:00
|
|
|
content, err := e.buildContent(templateFilename)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2016-11-09 18:38:32 +01:00
|
|
|
translatedFilename, err := e.translateString(templateFilename, templateFilename)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
filename := translatedFilename[0 : len(translatedFilename)-len(".tmpl")]
|
|
|
|
|
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
|
|
|
}
|