mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-06-19 14:43:05 -03:00
code generation move to internal/code-generator
This commit is contained in:
34
internal/code-generator/generator/generator.go
Normal file
34
internal/code-generator/generator/generator.go
Normal file
@ -0,0 +1,34 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user