2017-04-04 11:10:35 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2017-06-13 11:56:07 +00:00
|
|
|
"io/ioutil"
|
2017-04-04 11:10:35 +00:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2017-04-19 09:24:01 +00:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2017-04-04 11:10:35 +00:00
|
|
|
)
|
|
|
|
|
2017-05-29 08:05:16 +00:00
|
|
|
type extensionsInfo struct {
|
|
|
|
LanguagesByExtension map[string][]string
|
|
|
|
ExtensionsByLanguage map[string][]string
|
|
|
|
}
|
|
|
|
|
2017-06-21 13:18:27 +00:00
|
|
|
// Extensions reads from fileToParse and builds source file from tmplPath. It complies with type File signature.
|
2017-06-13 11:56:07 +00:00
|
|
|
func Extensions(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error {
|
|
|
|
data, err := ioutil.ReadFile(fileToParse)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-10 10:39:35 +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-04 11:10:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
extensionsToLower(languages)
|
2017-05-29 08:05:16 +00:00
|
|
|
extInfo := &extensionsInfo{}
|
2017-04-12 10:48:02 +00:00
|
|
|
orderedKeyList := getAlphabeticalOrderedKeys(languages)
|
2017-05-29 08:05:16 +00:00
|
|
|
extInfo.LanguagesByExtension = buildExtensionLanguageMap(languages, orderedKeyList)
|
|
|
|
extInfo.ExtensionsByLanguage = buildLanguageExtensionsMap(languages)
|
2017-04-04 11:10:35 +00:00
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2017-06-13 11:56:07 +00:00
|
|
|
if err := executeExtensionsTemplate(buf, extInfo, tmplPath, tmplName, commit); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatedWrite(outPath, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func extensionsToLower(languages map[string]*languageInfo) {
|
|
|
|
for _, info := range languages {
|
|
|
|
info.Extensions = stringSliceToLower(info.Extensions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringSliceToLower(slice []string) []string {
|
|
|
|
toLower := make([]string, 0, len(slice))
|
|
|
|
for _, s := range slice {
|
|
|
|
toLower = append(toLower, strings.ToLower(s))
|
2017-04-04 11:10:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
return toLower
|
2017-04-04 11:10:35 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-29 08:05:16 +00:00
|
|
|
func buildLanguageExtensionsMap(languages map[string]*languageInfo) map[string][]string {
|
|
|
|
langExtensionMap := make(map[string][]string, len(languages))
|
|
|
|
for lang, info := range languages {
|
|
|
|
if len(info.Extensions) > 0 {
|
|
|
|
langExtensionMap[lang] = info.Extensions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return langExtensionMap
|
|
|
|
}
|
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
func executeExtensionsTemplate(out io.Writer, extInfo *extensionsInfo, tmplPath, tmplName, commit string) error {
|
2017-04-04 11:10:35 +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, extInfo)
|
2017-04-04 11:10:35 +00:00
|
|
|
}
|