Added all the necessary to do GetLanguageByAlias functionality works

This commit is contained in:
Manuel Carmona
2017-05-04 15:03:54 +02:00
parent 6f3ad6d30d
commit 45314b4903
11 changed files with 864 additions and 8 deletions

View File

@ -0,0 +1,13 @@
package slinguist
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/simple-linguist.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Extracted from github/linguist commit: {{ getCommit }}
// languagesByAlias keeps alias for different languages and use the name of the languages as a alias too. All the
// keys (alias or not) are written in lower case and the whitespaces has been replaced by underscores.
var languagesByAlias = map[string]string{
{{range $alias, $language := . -}}
"{{ $alias }}": {{ printf "%q" $language -}},
{{end -}}
}

View File

@ -0,0 +1,63 @@
package generator
import (
"bytes"
"io"
"text/template"
"strings"
yaml "gopkg.in/yaml.v2"
)
// Aliases reads from buf and builds aliases_map.go file from aliasesTmplPath.
func Aliases(data []byte, aliasesTmplPath, aliasesTmplName, commit string) ([]byte, error) {
languages := make(map[string]*languageInfo)
if err := yaml.Unmarshal(data, &languages); err != nil {
return nil, err
}
orderedLangList := getAlphabeticalOrderedKeys(languages)
languagesByAlias := buildAliasLanguageMap(languages, orderedLangList)
buf := &bytes.Buffer{}
if err := executeAliasesTemplate(buf, languagesByAlias, aliasesTmplPath, aliasesTmplName, commit); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func buildAliasLanguageMap(languages map[string]*languageInfo, orderedLangList []string) map[string]string {
aliasLangsMap := make(map[string]string)
for _, lang := range orderedLangList {
langInfo := languages[lang]
key := convertToAliasKey(lang)
aliasLangsMap[key] = lang
for _, alias := range langInfo.Aliases {
key := convertToAliasKey(alias)
aliasLangsMap[key] = lang
}
}
return aliasLangsMap
}
func convertToAliasKey(s string) (key string) {
key = strings.Replace(s, ` `, `_`, -1)
key = strings.ToLower(key)
return
}
func executeAliasesTemplate(out io.Writer, languagesByAlias map[string]string, aliasesTmplPath, aliasesTmpl, commit string) error {
fmap := template.FuncMap{
"getCommit": func() string { return commit },
}
t := template.Must(template.New(aliasesTmpl).Funcs(fmap).ParseFiles(aliasesTmplPath))
if err := t.Execute(out, languagesByAlias); err != nil {
return err
}
return nil
}

View File

@ -53,6 +53,12 @@ const (
filenamesGold = "test_files/filenames.gold"
filenamesTestTmplPath = "../assets/filenames.go.tmpl"
filenamesTestTmplName = "filenames.go.tmpl"
// Aliases test
aliasesTestFile = "test_files/aliases.test.yml"
aliasesGold = "test_files/aliases.gold"
aliasesTestTmplPath = "../assets/aliases.go.tmpl"
aliasesTestTmplName = "aliases.go.tmpl"
)
func TestFromFile(t *testing.T) {
@ -128,6 +134,15 @@ func TestFromFile(t *testing.T) {
generate: Filenames,
wantOut: filenamesGold,
},
{
name: "TestFromFile_Aliases",
fileToParse: aliasesTestFile,
tmplPath: aliasesTestTmplPath,
tmplName: aliasesTestTmplName,
commit: commitTest,
generate: Aliases,
wantOut: aliasesGold,
},
}
for _, tt := range tests {

View File

@ -4,6 +4,7 @@ import "sort"
type languageInfo struct {
Type string `yaml:"type,omitempty"`
Aliases []string `yaml:"aliases,omitempty"`
Extensions []string `yaml:"extensions,omitempty,flow"`
Interpreters []string `yaml:"interpreters,omitempty,flow"`
Filenames []string `yaml:"filenames,omitempty,flow"`

View File

@ -0,0 +1,42 @@
package slinguist
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/simple-linguist.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Extracted from github/linguist commit: fe8b44ab8a225b1ffa75b983b916ea22fee5b6f7
// languagesByAlias keeps alias for different languages and use the name of the languages as a alias too. All the
// keys (alias or not) are written in lower case and the whitespaces has been replaced by underscores.
var languagesByAlias = map[string]string{
"asp": "ASP",
"aspx": "ASP",
"aspx-vb": "ASP",
"au3": "AutoIt",
"autoit": "AutoIt",
"autoit3": "AutoIt",
"autoitscript": "AutoIt",
"bat": "Batchfile",
"batch": "Batchfile",
"batchfile": "Batchfile",
"bsdmake": "Makefile",
"c++": "C++",
"cpp": "C++",
"django": "HTML+Django",
"dosbatch": "Batchfile",
"go": "Go",
"golang": "Go",
"gradle": "Gradle",
"html+django": "HTML+Django",
"html+django/jinja": "HTML+Django",
"html+jinja": "HTML+Django",
"htmldjango": "HTML+Django",
"make": "Makefile",
"makefile": "Makefile",
"mf": "Makefile",
"njk": "HTML+Django",
"nunjucks": "HTML+Django",
"obj-c": "Objective-C",
"objc": "Objective-C",
"objective-c": "Objective-C",
"objectivec": "Objective-C",
"winbatch": "Batchfile",
}

View File

@ -0,0 +1,42 @@
---
ASP:
aliases:
- aspx
- aspx-vb
AutoIt:
aliases:
- au3
- AutoIt3
- AutoItScript
Batchfile:
aliases:
- bat
- batch
- dosbatch
- winbatch
C++:
aliases:
- cpp
Go:
aliases:
- golang
Gradle:
type: data
HTML+Django:
aliases:
- django
- html+django/jinja
- html+jinja
- htmldjango
- njk
- nunjucks
Makefile:
aliases:
- bsdmake
- make
- mf
Objective-C:
aliases:
- obj-c
- objc
- objectivec

View File

@ -35,9 +35,9 @@ const (
documentationTmpl = "documentation.go.tmpl"
// type.go generation
typeFile = "type.go"
typeTmplPath = "internal/code-generator/assets/type.go.tmpl"
typeTmpl = "type.go.tmpl"
typeFile = "types_map.go"
typeTmplPath = "internal/code-generator/assets/types.go.tmpl"
typeTmpl = "types.go.tmpl"
// interpreters_map.go generation
interpretersFile = "interpreters_map.go"
@ -49,6 +49,11 @@ const (
filenamesTmplPath = "internal/code-generator/assets/filenames.go.tmpl"
filenamesTmpl = "filenames.go.tmpl"
// aliases_map.go generation
aliasesFile = "aliases_map.go"
aliasesTmplPath = "internal/code-generator/assets/aliases.go.tmpl"
aliasesTmpl = "aliases.go.tmpl"
commitPath = ".git/refs/heads/master"
)
@ -75,6 +80,7 @@ func main() {
&generatorArgs{languagesYAML, typeFile, typeTmplPath, typeTmpl, commit, generator.Types},
&generatorArgs{languagesYAML, interpretersFile, interpretersTmplPath, interpretersTmpl, commit, generator.Interpreters},
&generatorArgs{languagesYAML, filenamesFile, filenamesTmplPath, filenamesTmpl, commit, generator.Filenames},
&generatorArgs{languagesYAML, aliasesFile, aliasesTmplPath, aliasesTmpl, commit, generator.Aliases},
}
for _, args := range argsList {