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 (
"bytes"
"io"
"sort"
"strings"
"text/template"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)
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"`
}
// Languages reads from buf and builds languages.go file from languagesTmplPath.
func Languages(data []byte, languagesTmplPath, languagesTmplName, commit string) ([]byte, error) {
// Extensions reads from buf and builds extensions_map.go 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
@ -28,23 +20,13 @@ func Languages(data []byte, languagesTmplPath, languagesTmplName, commit string)
languagesByExtension := buildExtensionLanguageMap(languages, orderedKeyList)
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 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 {
extensionLangsMap := make(map[string][]string)
for _, lang := range orderedKeyList {
@ -57,13 +39,13 @@ func buildExtensionLanguageMap(languages map[string]*languageInfo, orderedKeyLis
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{
"getCommit": func() string { return commit },
"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 {
return err
}

View File

@ -13,10 +13,10 @@ const (
commitTest = "fe8b44ab8a225b1ffa75b983b916ea22fee5b6f7"
// Languages test
ymlTestFile = "test_files/languages.test.yml"
langGold = "test_files/languages.gold"
languagesTestTmplPath = "test_files/languages.test.go.tmpl"
languagesTestTmplName = "languages.test.go.tmpl"
extensionsTestFile = "test_files/extensions.test.yml"
extensionsGold = "test_files/extensions.gold"
extensionsTestTmplPath = "test_files/extensions.test.go.tmpl"
extensionsTestTmplName = "extensions.test.go.tmpl"
// Heuristics test
heuristicsTestFile = "test_files/heuristics.test.rb"
@ -67,12 +67,12 @@ func TestFromFile(t *testing.T) {
}{
{
name: "TestFromFile_Language",
fileToParse: ymlTestFile,
tmplPath: languagesTestTmplPath,
tmplName: languagesTestTmplName,
fileToParse: extensionsTestFile,
tmplPath: extensionsTestTmplPath,
tmplName: extensionsTestTmplName,
commit: commitTest,
generate: Languages,
wantOut: langGold,
generate: Extensions,
wantOut: extensionsGold,
},
{
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 (
// languages.go generation
languagesYAML = ".linguist/lib/linguist/languages.yml"
langFile = "languages.go"
languagesTmplPath = "internal/code-generator/assets/languages.go.tmpl"
languagesTmpl = "languages.go.tmpl"
// languages info file
languagesYAML = ".linguist/lib/linguist/languages.yml"
// extensions_map.go generation
extensionsFile = "extensions_map.go"
extensionsTmplPath = "internal/code-generator/assets/extensions.go.tmpl"
extensionsTmpl = "extensions.go.tmpl"
// content.go generation
heuristicsRuby = ".linguist/lib/linguist/heuristics.rb"
@ -66,7 +68,7 @@ func main() {
}
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{vendorYAML, vendorFile, vendorTmplPath, vendorTmpl, commit, generator.Vendor},
&generatorArgs{documentationYAML, documentationFile, documentationTmplPath, documentationTmpl, commit, generator.Documentation},