mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-08-29 17:57:30 +00:00
Rearranged code
This commit is contained in:
@@ -9,18 +9,25 @@ import (
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// Extensions reads from buf and builds extensions_map.go file from extensionsTmplPath.
|
||||
type extensionsInfo struct {
|
||||
LanguagesByExtension map[string][]string
|
||||
ExtensionsByLanguage map[string][]string
|
||||
}
|
||||
|
||||
// Extensions reads from buf and builds source file from extensionsTmplPath.
|
||||
func Extensions(data []byte, extensionsTmplPath, extensionsTmplName, commit string) ([]byte, error) {
|
||||
languages := make(map[string]*languageInfo)
|
||||
if err := yaml.Unmarshal(data, &languages); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
extInfo := &extensionsInfo{}
|
||||
orderedKeyList := getAlphabeticalOrderedKeys(languages)
|
||||
languagesByExtension := buildExtensionLanguageMap(languages, orderedKeyList)
|
||||
extInfo.LanguagesByExtension = buildExtensionLanguageMap(languages, orderedKeyList)
|
||||
extInfo.ExtensionsByLanguage = buildLanguageExtensionsMap(languages)
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if err := executeExtensionsTemplate(buf, languagesByExtension, extensionsTmplPath, extensionsTmplName, commit); err != nil {
|
||||
if err := executeExtensionsTemplate(buf, extInfo, extensionsTmplPath, extensionsTmplName, commit); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -39,14 +46,25 @@ func buildExtensionLanguageMap(languages map[string]*languageInfo, orderedKeyLis
|
||||
return extensionLangsMap
|
||||
}
|
||||
|
||||
func executeExtensionsTemplate(out io.Writer, languagesByExtension map[string][]string, extensionsTmplPath, extensionsTmpl, commit string) error {
|
||||
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
|
||||
}
|
||||
|
||||
func executeExtensionsTemplate(out io.Writer, extInfo *extensionsInfo, extensionsTmplPath, extensionsTmpl, 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(extensionsTmpl).Funcs(fmap).ParseFiles(extensionsTmplPath))
|
||||
if err := t.Execute(out, languagesByExtension); err != nil {
|
||||
if err := t.Execute(out, extInfo); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user