tartrazine/internal/code-generator/generator/documentation.go

44 lines
1.0 KiB
Go
Raw Normal View History

package generator
import (
"bytes"
"io"
2017-06-13 11:56:07 +00:00
"io/ioutil"
2017-05-31 10:07:46 +00:00
"text/template"
yaml "gopkg.in/yaml.v2"
)
2017-06-13 11:56:07 +00:00
// Documentation reads from fileToParse and builds source file from tmplPath. It's comply with type File signature.
func Documentation(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error {
data, err := ioutil.ReadFile(fileToParse)
if err != nil {
return err
}
var regexpList []string
if err := yaml.Unmarshal(data, &regexpList); err != nil {
2017-06-13 11:56:07 +00:00
return err
}
buf := &bytes.Buffer{}
2017-06-13 11:56:07 +00:00
if err := executeDocumentationTemplate(buf, regexpList, tmplPath, tmplName, commit); err != nil {
return err
}
2017-06-13 11:56:07 +00:00
return formatedWrite(outPath, buf.Bytes())
}
2017-06-13 11:56:07 +00:00
func executeDocumentationTemplate(out io.Writer, regexpList []string, tmplPath, tmplName, commit string) error {
fmap := template.FuncMap{
"getCommit": func() string { return commit },
}
2017-06-13 11:56:07 +00:00
t := template.Must(template.New(tmplName).Funcs(fmap).ParseFiles(tmplPath))
if err := t.Execute(out, regexpList); err != nil {
return err
}
return nil
}