2019-02-14 11:47:45 +00:00
|
|
|
// Package generator provides facilities to generate Go code for the
|
|
|
|
// package data in enry from YAML files describing supported languages in Linguist.
|
2017-04-04 11:10:35 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
2020-03-21 19:16:52 +00:00
|
|
|
"fmt"
|
2017-04-04 11:10:35 +00:00
|
|
|
"go/format"
|
2018-04-28 13:12:03 +00:00
|
|
|
"io"
|
2017-04-04 11:10:35 +00:00
|
|
|
"io/ioutil"
|
2018-04-28 13:12:03 +00:00
|
|
|
"path/filepath"
|
2020-03-21 19:16:52 +00:00
|
|
|
"strings"
|
2018-04-28 13:12:03 +00:00
|
|
|
"text/template"
|
2017-04-04 11:10:35 +00:00
|
|
|
)
|
|
|
|
|
2019-02-14 11:47:45 +00:00
|
|
|
// File is a common type for all generator functions.
|
|
|
|
// It generates Go source code file based on template in tmplPath,
|
|
|
|
// by parsing the data in fileToParse and linguist's samplesDir
|
|
|
|
// saving results to an outFile.
|
2017-06-13 11:56:07 +00:00
|
|
|
type File func(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error
|
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 {
|
2023-02-16 16:47:44 +00:00
|
|
|
err = fmt.Errorf("'go fmt' fails on %v", err)
|
|
|
|
// write un-formatter source to simplify debugging
|
|
|
|
formatedSource = source
|
2017-04-04 11:10:35 +00:00
|
|
|
}
|
2023-02-16 16:47:44 +00:00
|
|
|
|
2017-04-04 11:10:35 +00:00
|
|
|
if err := ioutil.WriteFile(outPath, formatedSource, 0666); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-16 16:47:44 +00:00
|
|
|
return err
|
2017-04-04 11:10:35 +00:00
|
|
|
}
|
2018-04-28 13:12:03 +00:00
|
|
|
|
|
|
|
func executeTemplate(w io.Writer, name, path, commit string, fmap template.FuncMap, data interface{}) error {
|
|
|
|
getCommit := func() string {
|
|
|
|
return commit
|
|
|
|
}
|
2020-03-21 19:16:52 +00:00
|
|
|
// stringVal returns escaped string that can be directly placed into go code.
|
|
|
|
// for value test`s it would return `test`+"`"+`s`
|
|
|
|
stringVal := func(val string) string {
|
|
|
|
val = strings.ReplaceAll(val, "`", "`+\"`\"+`")
|
|
|
|
return fmt.Sprintf("`%s`", val)
|
|
|
|
}
|
2022-12-25 10:31:49 +00:00
|
|
|
if fmap == nil {
|
|
|
|
fmap = make(template.FuncMap)
|
|
|
|
}
|
|
|
|
fmap["getCommit"] = getCommit
|
|
|
|
fmap["stringVal"] = stringVal
|
2022-12-25 10:58:23 +00:00
|
|
|
fmap["isRE2"] = isRE2
|
2018-04-28 13:12:03 +00:00
|
|
|
|
|
|
|
const headerTmpl = "header.go.tmpl"
|
|
|
|
headerPath := filepath.Join(filepath.Dir(path), headerTmpl)
|
|
|
|
|
2022-12-25 10:31:49 +00:00
|
|
|
h := template.Must(template.New(headerTmpl).Funcs(fmap).ParseFiles(headerPath))
|
2023-02-16 16:47:44 +00:00
|
|
|
if err := h.Execute(w, data); err != nil {
|
2018-04-28 13:12:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t := template.Must(template.New(name).Funcs(fmap).ParseFiles(path))
|
2023-02-16 16:47:44 +00:00
|
|
|
return t.Execute(w, data)
|
2018-04-28 13:12:03 +00:00
|
|
|
}
|