2017-05-04 13:03:54 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2017-06-13 11:56:07 +00:00
|
|
|
"io/ioutil"
|
2017-05-04 13:03:54 +00:00
|
|
|
"strings"
|
2019-02-14 11:47:45 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
2017-05-04 13:03:54 +00:00
|
|
|
)
|
|
|
|
|
2017-06-21 13:18:27 +00:00
|
|
|
// Aliases reads from fileToParse and builds source file from tmplPath. It complies with type File signature.
|
2017-06-13 11:56:07 +00:00
|
|
|
func Aliases(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error {
|
|
|
|
data, err := ioutil.ReadFile(fileToParse)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-04 13:03:54 +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-05-04 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
orderedLangList := getAlphabeticalOrderedKeys(languages)
|
2019-02-14 11:47:45 +00:00
|
|
|
languageByAlias := buildAliasLanguageMap(languages, orderedLangList)
|
2017-05-04 13:03:54 +00:00
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2019-02-14 11:47:45 +00:00
|
|
|
if err := executeAliasesTemplate(buf, languageByAlias, tmplPath, tmplName, commit); err != nil {
|
2017-06-13 11:56:07 +00:00
|
|
|
return err
|
2017-05-04 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
return formatedWrite(outPath, buf.Bytes())
|
2017-05-04 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildAliasLanguageMap(languages map[string]*languageInfo, orderedLangList []string) map[string]string {
|
|
|
|
aliasLangsMap := make(map[string]string)
|
|
|
|
for _, lang := range orderedLangList {
|
|
|
|
langInfo := languages[lang]
|
|
|
|
key := convertToAliasKey(lang)
|
|
|
|
aliasLangsMap[key] = lang
|
|
|
|
for _, alias := range langInfo.Aliases {
|
|
|
|
key := convertToAliasKey(alias)
|
|
|
|
aliasLangsMap[key] = lang
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return aliasLangsMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertToAliasKey(s string) (key string) {
|
|
|
|
key = strings.Replace(s, ` `, `_`, -1)
|
|
|
|
key = strings.ToLower(key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-14 11:47:45 +00:00
|
|
|
func executeAliasesTemplate(out io.Writer, languageByAlias map[string]string, aliasesTmplPath, aliasesTmpl, commit string) error {
|
|
|
|
return executeTemplate(out, aliasesTmpl, aliasesTmplPath, commit, nil, languageByAlias)
|
2017-05-04 13:03:54 +00:00
|
|
|
}
|