From f63a25d794a0fc4661ca674df6f566b2ec662cec Mon Sep 17 00:00:00 2001 From: Manuel Carmona Date: Wed, 19 Apr 2017 11:24:01 +0200 Subject: [PATCH] all related to extension strategy renamed to reference it --- languages.go => extensions_map.go | 0 .../{languages.go.tmpl => extensions.go.tmpl} | 0 .../generator/{langs.go => extensions.go} | 30 ++++--------------- .../generator/generator_test.go | 18 +++++------ internal/code-generator/generator/langinfo.go | 20 +++++++++++++ .../{languages.gold => extensions.gold} | 0 ...s.test.go.tmpl => extensions.test.go.tmpl} | 0 ...languages.test.yml => extensions.test.yml} | 0 internal/code-generator/main.go | 14 +++++---- 9 files changed, 43 insertions(+), 39 deletions(-) rename languages.go => extensions_map.go (100%) rename internal/code-generator/assets/{languages.go.tmpl => extensions.go.tmpl} (100%) rename internal/code-generator/generator/{langs.go => extensions.go} (51%) create mode 100644 internal/code-generator/generator/langinfo.go rename internal/code-generator/generator/test_files/{languages.gold => extensions.gold} (100%) rename internal/code-generator/generator/test_files/{languages.test.go.tmpl => extensions.test.go.tmpl} (100%) rename internal/code-generator/generator/test_files/{languages.test.yml => extensions.test.yml} (100%) diff --git a/languages.go b/extensions_map.go similarity index 100% rename from languages.go rename to extensions_map.go diff --git a/internal/code-generator/assets/languages.go.tmpl b/internal/code-generator/assets/extensions.go.tmpl similarity index 100% rename from internal/code-generator/assets/languages.go.tmpl rename to internal/code-generator/assets/extensions.go.tmpl diff --git a/internal/code-generator/generator/langs.go b/internal/code-generator/generator/extensions.go similarity index 51% rename from internal/code-generator/generator/langs.go rename to internal/code-generator/generator/extensions.go index f69e670..3ea7f35 100644 --- a/internal/code-generator/generator/langs.go +++ b/internal/code-generator/generator/extensions.go @@ -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 } diff --git a/internal/code-generator/generator/generator_test.go b/internal/code-generator/generator/generator_test.go index 46e8fe1..0487cca 100644 --- a/internal/code-generator/generator/generator_test.go +++ b/internal/code-generator/generator/generator_test.go @@ -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", diff --git a/internal/code-generator/generator/langinfo.go b/internal/code-generator/generator/langinfo.go new file mode 100644 index 0000000..e61d335 --- /dev/null +++ b/internal/code-generator/generator/langinfo.go @@ -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 +} diff --git a/internal/code-generator/generator/test_files/languages.gold b/internal/code-generator/generator/test_files/extensions.gold similarity index 100% rename from internal/code-generator/generator/test_files/languages.gold rename to internal/code-generator/generator/test_files/extensions.gold diff --git a/internal/code-generator/generator/test_files/languages.test.go.tmpl b/internal/code-generator/generator/test_files/extensions.test.go.tmpl similarity index 100% rename from internal/code-generator/generator/test_files/languages.test.go.tmpl rename to internal/code-generator/generator/test_files/extensions.test.go.tmpl diff --git a/internal/code-generator/generator/test_files/languages.test.yml b/internal/code-generator/generator/test_files/extensions.test.yml similarity index 100% rename from internal/code-generator/generator/test_files/languages.test.yml rename to internal/code-generator/generator/test_files/extensions.test.yml diff --git a/internal/code-generator/main.go b/internal/code-generator/main.go index 5dd29d9..59a784d 100644 --- a/internal/code-generator/main.go +++ b/internal/code-generator/main.go @@ -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},