mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-11-10 21:42:22 +00:00
35 lines
877 B
Go
35 lines
877 B
Go
|
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
|
||
|
}
|
||
|
|
||
|
formatedSource, err := format.Source(source)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err := ioutil.WriteFile(outPath, formatedSource, 0666); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|