Added utils.go generation

This commit is contained in:
Manuel Carmona
2017-04-06 17:31:17 +02:00
parent dae33dc2b2
commit 13e7886a02
11 changed files with 643 additions and 215 deletions

View File

@ -0,0 +1,37 @@
package generator
import (
"bytes"
"html/template"
"io"
yaml "gopkg.in/yaml.v2"
)
// Vendor reads from buf and builds utils.go file from utilsTmplPath.
func Vendor(data []byte, uitlsTmplPath, utilsTmplName, commit string) ([]byte, error) {
var regexpList []string
if err := yaml.Unmarshal(data, &regexpList); err != nil {
return nil, err
}
buf := &bytes.Buffer{}
if err := executeUtilsTemplate(buf, regexpList, uitlsTmplPath, utilsTmplName, commit); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func executeUtilsTemplate(out io.Writer, regexpList []string, languagesTmplPath, languagesTmpl, commit string) error {
fmap := template.FuncMap{
"getCommit": func() string { return commit },
}
t := template.Must(template.New(languagesTmpl).Funcs(fmap).ParseFiles(languagesTmplPath))
if err := t.Execute(out, regexpList); err != nil {
return err
}
return nil
}