2017-04-17 06:19:53 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2017-06-13 11:56:07 +00:00
|
|
|
"io/ioutil"
|
2017-04-17 06:19:53 +00:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2017-06-21 13:18:27 +00:00
|
|
|
// Interpreters reads from fileToParse and builds source file from tmplPath. It complies with type File signature.
|
2017-06-13 11:56:07 +00:00
|
|
|
func Interpreters(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error {
|
|
|
|
data, err := ioutil.ReadFile(fileToParse)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-17 06:19:53 +00:00
|
|
|
languages := make(map[string]*languageInfo)
|
|
|
|
if err := yaml.Unmarshal(data, &languages); err != nil {
|
2017-06-13 11:56:07 +00:00
|
|
|
return err
|
2017-04-17 06:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
orderedKeys := getAlphabeticalOrderedKeys(languages)
|
|
|
|
languagesByInterpreter := buildInterpreterLanguagesMap(languages, orderedKeys)
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2017-06-13 11:56:07 +00:00
|
|
|
if err := executeInterpretersTemplate(buf, languagesByInterpreter, tmplPath, tmplName, commit); err != nil {
|
|
|
|
return err
|
2017-04-17 06:19:53 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
return formatedWrite(outPath, buf.Bytes())
|
2017-04-17 06:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildInterpreterLanguagesMap(languages map[string]*languageInfo, orderedKeys []string) map[string][]string {
|
|
|
|
interpreterLangsMap := make(map[string][]string)
|
|
|
|
for _, lang := range orderedKeys {
|
|
|
|
langInfo := languages[lang]
|
|
|
|
for _, interpreter := range langInfo.Interpreters {
|
|
|
|
interpreterLangsMap[interpreter] = append(interpreterLangsMap[interpreter], lang)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return interpreterLangsMap
|
|
|
|
}
|
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
func executeInterpretersTemplate(out io.Writer, languagesByInterpreter map[string][]string, tmplPath, tmplName, commit string) error {
|
2017-04-17 06:19:53 +00:00
|
|
|
fmap := template.FuncMap{
|
|
|
|
"formatStringSlice": func(slice []string) string { return `"` + strings.Join(slice, `","`) + `"` },
|
|
|
|
}
|
2018-04-28 13:12:03 +00:00
|
|
|
return executeTemplate(out, tmplName, tmplPath, commit, fmap, languagesByInterpreter)
|
2017-04-17 06:19:53 +00:00
|
|
|
}
|