all related to extension strategy renamed to reference it

This commit is contained in:
Manuel Carmona 2017-04-19 11:24:01 +02:00
parent df60eab1ad
commit f63a25d794
9 changed files with 43 additions and 39 deletions

View File

@ -3,22 +3,14 @@ package generator
import ( import (
"bytes" "bytes"
"io" "io"
"sort"
"strings" "strings"
"text/template" "text/template"
"gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
) )
type languageInfo struct { // Extensions reads from buf and builds extensions_map.go file from extensionsTmplPath.
Type string `yaml:"type,omitempty"` func Extensions(data []byte, extensionsTmplPath, extensionsTmplName, commit string) ([]byte, error) {
Extensions []string `yaml:"extensions,omitempty,flow"`
Interpreters []string `yaml:"interpreters,omitempty,flow"`
Filenames []string `yaml:"filenames,omitempty,flow"`
}
// Languages reads from buf and builds languages.go file from languagesTmplPath.
func Languages(data []byte, languagesTmplPath, languagesTmplName, commit string) ([]byte, error) {
languages := make(map[string]*languageInfo) languages := make(map[string]*languageInfo)
if err := yaml.Unmarshal(data, &languages); err != nil { if err := yaml.Unmarshal(data, &languages); err != nil {
return nil, err return nil, err
@ -28,23 +20,13 @@ func Languages(data []byte, languagesTmplPath, languagesTmplName, commit string)
languagesByExtension := buildExtensionLanguageMap(languages, orderedKeyList) languagesByExtension := buildExtensionLanguageMap(languages, orderedKeyList)
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
if err := executeLanguagesTemplate(buf, languagesByExtension, languagesTmplPath, languagesTmplName, commit); err != nil { if err := executeExtensionsTemplate(buf, languagesByExtension, extensionsTmplPath, extensionsTmplName, commit); err != nil {
return nil, err return nil, err
} }
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func getAlphabeticalOrderedKeys(languages map[string]*languageInfo) []string {
keyList := make([]string, 0)
for lang := range languages {
keyList = append(keyList, lang)
}
sort.Strings(keyList)
return keyList
}
func buildExtensionLanguageMap(languages map[string]*languageInfo, orderedKeyList []string) map[string][]string { func buildExtensionLanguageMap(languages map[string]*languageInfo, orderedKeyList []string) map[string][]string {
extensionLangsMap := make(map[string][]string) extensionLangsMap := make(map[string][]string)
for _, lang := range orderedKeyList { for _, lang := range orderedKeyList {
@ -57,13 +39,13 @@ func buildExtensionLanguageMap(languages map[string]*languageInfo, orderedKeyLis
return extensionLangsMap return extensionLangsMap
} }
func executeLanguagesTemplate(out io.Writer, languagesByExtension map[string][]string, languagesTmplPath, languagesTmpl, commit string) error { func executeExtensionsTemplate(out io.Writer, languagesByExtension map[string][]string, extensionsTmplPath, extensionsTmpl, commit string) error {
fmap := template.FuncMap{ fmap := template.FuncMap{
"getCommit": func() string { return commit }, "getCommit": func() string { return commit },
"formatStringSlice": func(slice []string) string { return `"` + strings.Join(slice, `","`) + `"` }, "formatStringSlice": func(slice []string) string { return `"` + strings.Join(slice, `","`) + `"` },
} }
t := template.Must(template.New(languagesTmpl).Funcs(fmap).ParseFiles(languagesTmplPath)) t := template.Must(template.New(extensionsTmpl).Funcs(fmap).ParseFiles(extensionsTmplPath))
if err := t.Execute(out, languagesByExtension); err != nil { if err := t.Execute(out, languagesByExtension); err != nil {
return err return err
} }

View File

@ -13,10 +13,10 @@ const (
commitTest = "fe8b44ab8a225b1ffa75b983b916ea22fee5b6f7" commitTest = "fe8b44ab8a225b1ffa75b983b916ea22fee5b6f7"
// Languages test // Languages test
ymlTestFile = "test_files/languages.test.yml" extensionsTestFile = "test_files/extensions.test.yml"
langGold = "test_files/languages.gold" extensionsGold = "test_files/extensions.gold"
languagesTestTmplPath = "test_files/languages.test.go.tmpl" extensionsTestTmplPath = "test_files/extensions.test.go.tmpl"
languagesTestTmplName = "languages.test.go.tmpl" extensionsTestTmplName = "extensions.test.go.tmpl"
// Heuristics test // Heuristics test
heuristicsTestFile = "test_files/heuristics.test.rb" heuristicsTestFile = "test_files/heuristics.test.rb"
@ -67,12 +67,12 @@ func TestFromFile(t *testing.T) {
}{ }{
{ {
name: "TestFromFile_Language", name: "TestFromFile_Language",
fileToParse: ymlTestFile, fileToParse: extensionsTestFile,
tmplPath: languagesTestTmplPath, tmplPath: extensionsTestTmplPath,
tmplName: languagesTestTmplName, tmplName: extensionsTestTmplName,
commit: commitTest, commit: commitTest,
generate: Languages, generate: Extensions,
wantOut: langGold, wantOut: extensionsGold,
}, },
{ {
name: "TestFromFile_Heuristics", name: "TestFromFile_Heuristics",

View File

@ -0,0 +1,20 @@
package generator
import "sort"
type languageInfo struct {
Type string `yaml:"type,omitempty"`
Extensions []string `yaml:"extensions,omitempty,flow"`
Interpreters []string `yaml:"interpreters,omitempty,flow"`
Filenames []string `yaml:"filenames,omitempty,flow"`
}
func getAlphabeticalOrderedKeys(languages map[string]*languageInfo) []string {
keyList := make([]string, 0)
for lang := range languages {
keyList = append(keyList, lang)
}
sort.Strings(keyList)
return keyList
}

View File

@ -8,11 +8,13 @@ import (
) )
const ( const (
// languages.go generation // languages info file
languagesYAML = ".linguist/lib/linguist/languages.yml" languagesYAML = ".linguist/lib/linguist/languages.yml"
langFile = "languages.go"
languagesTmplPath = "internal/code-generator/assets/languages.go.tmpl" // extensions_map.go generation
languagesTmpl = "languages.go.tmpl" extensionsFile = "extensions_map.go"
extensionsTmplPath = "internal/code-generator/assets/extensions.go.tmpl"
extensionsTmpl = "extensions.go.tmpl"
// content.go generation // content.go generation
heuristicsRuby = ".linguist/lib/linguist/heuristics.rb" heuristicsRuby = ".linguist/lib/linguist/heuristics.rb"
@ -66,7 +68,7 @@ func main() {
} }
argsList := []*generatorArgs{ argsList := []*generatorArgs{
&generatorArgs{languagesYAML, langFile, languagesTmplPath, languagesTmpl, commit, generator.Languages}, &generatorArgs{languagesYAML, extensionsFile, extensionsTmplPath, extensionsTmpl, commit, generator.Extensions},
&generatorArgs{heuristicsRuby, contentFile, contentTmplPath, contentTmpl, commit, generator.Heuristics}, &generatorArgs{heuristicsRuby, contentFile, contentTmplPath, contentTmpl, commit, generator.Heuristics},
&generatorArgs{vendorYAML, vendorFile, vendorTmplPath, vendorTmpl, commit, generator.Vendor}, &generatorArgs{vendorYAML, vendorFile, vendorTmplPath, vendorTmpl, commit, generator.Vendor},
&generatorArgs{documentationYAML, documentationFile, documentationTmplPath, documentationTmpl, commit, generator.Documentation}, &generatorArgs{documentationYAML, documentationFile, documentationTmplPath, documentationTmpl, commit, generator.Documentation},