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

39 lines
977 B
Go
Raw Normal View History

2017-04-04 11:10:35 +00:00
package generator
import (
"go/format"
"io/ioutil"
)
// Func is the function's type that generate the files from templates.
type Func func(dataToParse []byte, templatePath string, template string, commit string) ([]byte, error)
// FromFile read data to parse from a file named fileToParse and write the generated source code to a file named outPath. The generated
// source code is formated with gofmt and tagged with commit.
func FromFile(fileToParse, outPath, tmplPath, tmplName, commit string, generate Func) error {
buf, err := ioutil.ReadFile(fileToParse)
if err != nil {
return err
}
source, err := generate(buf, tmplPath, tmplName, commit)
if err != nil {
return err
}
return formatedWrite(outPath, source)
2017-05-25 10:33:26 +00:00
}
func formatedWrite(outPath string, source []byte) error {
2017-04-04 11:10:35 +00:00
formatedSource, err := format.Source(source)
if err != nil {
return err
}
if err := ioutil.WriteFile(outPath, formatedSource, 0666); err != nil {
return err
}
return nil
}