2016-11-04 23:45:25 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-06 19:29:37 +03:00
|
|
|
"bytes"
|
2016-11-07 10:51:13 +03:00
|
|
|
"log"
|
2018-09-13 18:28:14 +03:00
|
|
|
"net/url"
|
2016-11-07 11:23:59 +03:00
|
|
|
"os"
|
2016-11-06 19:20:57 +03:00
|
|
|
"path/filepath"
|
2016-11-07 12:27:57 +03:00
|
|
|
"strings"
|
2016-11-06 19:29:37 +03:00
|
|
|
"text/template"
|
2016-11-07 11:23:59 +03:00
|
|
|
"time"
|
2016-11-06 19:20:57 +03:00
|
|
|
|
2016-11-04 23:45:25 +03:00
|
|
|
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
|
|
|
"github.com/golang/protobuf/protoc-gen-go/plugin"
|
2017-05-19 11:02:40 +03:00
|
|
|
|
2018-09-12 21:29:07 +03:00
|
|
|
pgghelpers "moul.io/protoc-gen-gotemplate/helpers"
|
2016-11-04 23:45:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type GenericTemplateBasedEncoder struct {
|
2016-12-15 18:25:44 +03:00
|
|
|
templateDir string
|
|
|
|
service *descriptor.ServiceDescriptorProto
|
|
|
|
file *descriptor.FileDescriptorProto
|
2017-05-12 21:46:18 +03:00
|
|
|
enum []*descriptor.EnumDescriptorProto
|
2016-12-15 18:25:44 +03:00
|
|
|
debug bool
|
|
|
|
destinationDir string
|
2016-11-04 23:45:25 +03:00
|
|
|
}
|
|
|
|
|
2016-11-06 19:29:37 +03:00
|
|
|
type Ast struct {
|
2016-12-15 18:25:44 +03:00
|
|
|
BuildDate time.Time `json:"build-date"`
|
|
|
|
BuildHostname string `json:"build-hostname"`
|
|
|
|
BuildUser string `json:"build-user"`
|
|
|
|
GoPWD string `json:"go-pwd,omitempty"`
|
|
|
|
PWD string `json:"pwd"`
|
|
|
|
Debug bool `json:"debug"`
|
|
|
|
DestinationDir string `json:"destination-dir"`
|
|
|
|
File *descriptor.FileDescriptorProto `json:"file"`
|
|
|
|
RawFilename string `json:"raw-filename"`
|
|
|
|
Filename string `json:"filename"`
|
|
|
|
TemplateDir string `json:"template-dir"`
|
|
|
|
Service *descriptor.ServiceDescriptorProto `json:"service"`
|
2017-05-12 21:46:18 +03:00
|
|
|
Enum []*descriptor.EnumDescriptorProto `json:"enum"`
|
2016-11-06 19:29:37 +03:00
|
|
|
}
|
|
|
|
|
2017-01-12 16:46:43 +03:00
|
|
|
func NewGenericServiceTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
|
2016-11-04 23:45:25 +03:00
|
|
|
e = &GenericTemplateBasedEncoder{
|
2016-12-15 18:25:44 +03:00
|
|
|
service: service,
|
|
|
|
file: file,
|
|
|
|
templateDir: templateDir,
|
|
|
|
debug: debug,
|
|
|
|
destinationDir: destinationDir,
|
2017-05-12 21:46:18 +03:00
|
|
|
enum: file.GetEnumType(),
|
2016-11-04 23:45:25 +03:00
|
|
|
}
|
2016-11-07 10:51:13 +03:00
|
|
|
if debug {
|
2016-11-09 20:12:00 +03:00
|
|
|
log.Printf("new encoder: file=%q service=%q template-dir=%q", file.GetName(), service.GetName(), templateDir)
|
2016-11-07 10:51:13 +03:00
|
|
|
}
|
2018-01-12 20:38:10 +03:00
|
|
|
pgghelpers.InitPathMap(file)
|
2016-11-07 10:51:13 +03:00
|
|
|
|
2016-11-04 23:45:25 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-12 16:46:43 +03:00
|
|
|
func NewGenericTemplateBasedEncoder(templateDir string, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
|
|
|
|
e = &GenericTemplateBasedEncoder{
|
|
|
|
service: nil,
|
|
|
|
file: file,
|
|
|
|
templateDir: templateDir,
|
2017-05-12 21:46:18 +03:00
|
|
|
enum: file.GetEnumType(),
|
2017-01-12 16:46:43 +03:00
|
|
|
debug: debug,
|
|
|
|
destinationDir: destinationDir,
|
|
|
|
}
|
|
|
|
if debug {
|
|
|
|
log.Printf("new encoder: file=%q template-dir=%q", file.GetName(), templateDir)
|
|
|
|
}
|
2018-01-12 20:38:10 +03:00
|
|
|
pgghelpers.InitPathMap(file)
|
2017-01-12 16:46:43 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-06 19:20:57 +03:00
|
|
|
func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
|
|
|
|
filenames := []string{}
|
|
|
|
|
2016-12-14 12:33:08 +03:00
|
|
|
err := filepath.Walk(e.templateDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-11-06 19:20:57 +03:00
|
|
|
}
|
2016-12-14 12:33:08 +03:00
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
2016-11-06 19:20:57 +03:00
|
|
|
}
|
2016-12-14 12:33:08 +03:00
|
|
|
if filepath.Ext(path) != ".tmpl" {
|
|
|
|
return nil
|
2016-11-06 19:20:57 +03:00
|
|
|
}
|
2016-12-14 12:33:08 +03:00
|
|
|
rel, err := filepath.Rel(e.templateDir, path)
|
2016-11-06 19:20:57 +03:00
|
|
|
if err != nil {
|
2016-12-14 12:33:08 +03:00
|
|
|
return err
|
2016-11-06 19:20:57 +03:00
|
|
|
}
|
2016-11-07 10:51:13 +03:00
|
|
|
if e.debug {
|
|
|
|
log.Printf("new template: %q", rel)
|
|
|
|
}
|
2018-09-13 18:28:14 +03:00
|
|
|
|
2016-11-06 19:20:57 +03:00
|
|
|
filenames = append(filenames, rel)
|
2016-12-14 12:33:08 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return filenames, err
|
2016-11-06 19:20:57 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 20:38:32 +03:00
|
|
|
func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) {
|
|
|
|
// prepare the ast passed to the template engine
|
2017-12-25 11:17:09 +03:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-07 12:27:57 +03:00
|
|
|
goPwd := ""
|
|
|
|
if os.Getenv("GOPATH") != "" {
|
2017-12-25 11:17:09 +03:00
|
|
|
goPwd, err = filepath.Rel(os.Getenv("GOPATH")+"/src", pwd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-07 12:27:57 +03:00
|
|
|
if strings.Contains(goPwd, "../") {
|
|
|
|
goPwd = ""
|
|
|
|
}
|
|
|
|
}
|
2016-11-06 19:29:37 +03:00
|
|
|
ast := Ast{
|
2016-12-15 18:25:44 +03:00
|
|
|
BuildDate: time.Now(),
|
|
|
|
BuildHostname: hostname,
|
|
|
|
BuildUser: os.Getenv("USER"),
|
|
|
|
PWD: pwd,
|
|
|
|
GoPWD: goPwd,
|
|
|
|
File: e.file,
|
|
|
|
TemplateDir: e.templateDir,
|
|
|
|
DestinationDir: e.destinationDir,
|
|
|
|
RawFilename: templateFilename,
|
|
|
|
Filename: "",
|
|
|
|
Service: e.service,
|
2017-05-12 21:46:18 +03:00
|
|
|
Enum: e.enum,
|
2016-11-09 20:38:32 +03:00
|
|
|
}
|
2016-12-14 13:06:24 +03:00
|
|
|
buffer := new(bytes.Buffer)
|
2018-09-13 18:28:14 +03:00
|
|
|
|
|
|
|
unescaped, err := url.QueryUnescape(templateFilename)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to unescape filepath %q: %v", templateFilename, err)
|
|
|
|
} else {
|
|
|
|
templateFilename = unescaped
|
|
|
|
}
|
|
|
|
|
2017-05-19 11:02:40 +03:00
|
|
|
tmpl, err := template.New("").Funcs(pgghelpers.ProtoHelpersFuncMap).Parse(templateFilename)
|
2016-12-14 13:06:24 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := tmpl.Execute(buffer, ast); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ast.Filename = buffer.String()
|
2016-11-09 20:38:32 +03:00
|
|
|
return &ast, nil
|
|
|
|
}
|
|
|
|
|
2016-12-14 13:06:24 +03:00
|
|
|
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, string, error) {
|
2016-11-09 20:38:32 +03:00
|
|
|
// initialize template engine
|
|
|
|
fullPath := filepath.Join(e.templateDir, templateFilename)
|
|
|
|
templateName := filepath.Base(fullPath)
|
2017-05-19 11:02:40 +03:00
|
|
|
tmpl, err := template.New(templateName).Funcs(pgghelpers.ProtoHelpersFuncMap).ParseFiles(fullPath)
|
2016-11-09 20:38:32 +03:00
|
|
|
if err != nil {
|
2016-12-14 13:06:24 +03:00
|
|
|
return "", "", err
|
2016-11-09 20:38:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ast, err := e.genAst(templateFilename)
|
|
|
|
if err != nil {
|
2016-12-14 13:06:24 +03:00
|
|
|
return "", "", err
|
2016-11-06 19:29:37 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 20:38:32 +03:00
|
|
|
// generate the content
|
2016-11-06 19:29:37 +03:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
if err := tmpl.Execute(buffer, ast); err != nil {
|
2016-12-14 13:06:24 +03:00
|
|
|
return "", "", err
|
2016-11-09 20:38:32 +03:00
|
|
|
}
|
|
|
|
|
2016-12-14 13:06:24 +03:00
|
|
|
return buffer.String(), ast.Filename, nil
|
2016-11-09 20:38:32 +03:00
|
|
|
}
|
|
|
|
|
2016-11-04 23:45:25 +03:00
|
|
|
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
|
2016-11-06 19:20:57 +03:00
|
|
|
templates, err := e.templates()
|
|
|
|
if err != nil {
|
2016-11-07 10:51:13 +03:00
|
|
|
log.Fatalf("cannot get templates from %q: %v", e.templateDir, err)
|
2016-11-06 19:20:57 +03:00
|
|
|
}
|
|
|
|
|
2016-12-14 13:07:05 +03:00
|
|
|
length := len(templates)
|
|
|
|
files := make([]*plugin_go.CodeGeneratorResponse_File, 0, length)
|
|
|
|
errChan := make(chan error, length)
|
|
|
|
resultChan := make(chan *plugin_go.CodeGeneratorResponse_File, length)
|
2016-11-06 19:20:57 +03:00
|
|
|
for _, templateFilename := range templates {
|
2016-12-14 13:07:05 +03:00
|
|
|
go func(tmpl string) {
|
2017-12-25 11:17:09 +03:00
|
|
|
var translatedFilename, content string
|
|
|
|
content, translatedFilename, err = e.buildContent(tmpl)
|
2016-12-14 13:07:05 +03:00
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
filename := translatedFilename[:len(translatedFilename)-len(".tmpl")]
|
|
|
|
|
|
|
|
resultChan <- &plugin_go.CodeGeneratorResponse_File{
|
|
|
|
Content: &content,
|
|
|
|
Name: &filename,
|
|
|
|
}
|
|
|
|
}(templateFilename)
|
|
|
|
}
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
select {
|
|
|
|
case f := <-resultChan:
|
|
|
|
files = append(files, f)
|
|
|
|
case err = <-errChan:
|
2016-11-09 20:38:32 +03:00
|
|
|
}
|
2016-11-04 23:45:25 +03:00
|
|
|
}
|
2016-12-14 13:07:05 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-11-06 19:20:57 +03:00
|
|
|
return files
|
2016-11-04 23:45:25 +03:00
|
|
|
}
|