2017-04-04 11:10:35 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2017-04-12 10:48:02 +00:00
|
|
|
"sort"
|
2017-04-04 11:10:35 +00:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2017-04-10 10:39:35 +00:00
|
|
|
type languageInfo struct {
|
2017-04-11 09:26:23 +00:00
|
|
|
Type string `yaml:"type,omitempty"`
|
|
|
|
Extensions []string `yaml:"extensions,omitempty,flow"`
|
|
|
|
Interpreters []string `yaml:"interpreters,omitempty,flow"`
|
2017-04-10 10:39:35 +00:00
|
|
|
}
|
2017-04-04 11:10:35 +00:00
|
|
|
|
|
|
|
// Languages reads from buf and builds languages.go file from languagesTmplPath.
|
|
|
|
func Languages(data []byte, languagesTmplPath, languagesTmplName, commit string) ([]byte, error) {
|
2017-04-10 10:39:35 +00:00
|
|
|
languages := make(map[string]*languageInfo)
|
|
|
|
if err := yaml.Unmarshal(data, &languages); err != nil {
|
2017-04-04 11:10:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-12 10:48:02 +00:00
|
|
|
orderedKeyList := getAlphabeticalOrderedKeys(languages)
|
2017-04-12 07:10:16 +00:00
|
|
|
languagesByExtension := buildExtensionLanguageMap(languages, orderedKeyList)
|
2017-04-04 11:10:35 +00:00
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
if err := executeLanguagesTemplate(buf, languagesByExtension, languagesTmplPath, languagesTmplName, commit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2017-04-12 10:48:02 +00:00
|
|
|
func getAlphabeticalOrderedKeys(languages map[string]*languageInfo) []string {
|
|
|
|
keyList := make([]string, 0)
|
|
|
|
for lang := range languages {
|
|
|
|
keyList = append(keyList, lang)
|
2017-04-12 07:10:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 10:48:02 +00:00
|
|
|
sort.Strings(keyList)
|
|
|
|
return keyList
|
2017-04-12 07:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildExtensionLanguageMap(languages map[string]*languageInfo, orderedKeyList []string) map[string][]string {
|
2017-04-04 11:10:35 +00:00
|
|
|
extensionLangsMap := make(map[string][]string)
|
2017-04-12 07:10:16 +00:00
|
|
|
for _, lang := range orderedKeyList {
|
|
|
|
langInfo := languages[lang]
|
|
|
|
for _, extension := range langInfo.Extensions {
|
2017-04-10 10:39:35 +00:00
|
|
|
extensionLangsMap[extension] = append(extensionLangsMap[extension], lang)
|
2017-04-04 11:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 09:26:23 +00:00
|
|
|
return extensionLangsMap
|
2017-04-04 11:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func executeLanguagesTemplate(out io.Writer, languagesByExtension map[string][]string, languagesTmplPath, languagesTmpl, commit string) error {
|
|
|
|
fmap := template.FuncMap{
|
|
|
|
"getCommit": func() string { return commit },
|
|
|
|
"formatStringSlice": func(slice []string) string { return `"` + strings.Join(slice, `","`) + `"` },
|
|
|
|
}
|
|
|
|
|
|
|
|
t := template.Must(template.New(languagesTmpl).Funcs(fmap).ParseFiles(languagesTmplPath))
|
|
|
|
if err := t.Execute(out, languagesByExtension); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|