added filenames_map.go generation

languagesByFilename now is a map[string]string
This commit is contained in:
Manuel Carmona 2017-04-19 10:22:46 +02:00
parent f45efec5fb
commit 645bdd7331
10 changed files with 286 additions and 59 deletions

10
filename.go Normal file
View File

@ -0,0 +1,10 @@
package slinguist
func GetLanguageByFilename(filename string) (lang string, safe bool) {
lang, safe = languagesByFilename[filename]
if lang == "" {
lang = OtherLanguage
}
return
}

140
filenames_map.go Normal file
View File

@ -0,0 +1,140 @@
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: dae33dc2b20cddc85d1300435c3be7118a7115a9
var languagesByFilename = map[string]string{
".Rprofile": "R",
".XCompose": "XCompose",
".abbrev_defs": "Emacs Lisp",
".arcconfig": "JSON",
".babelrc": "JSON5",
".bash_history": "Shell",
".bash_logout": "Shell",
".bash_profile": "Shell",
".bashrc": "Shell",
".clang-format": "YAML",
".classpath": "XML",
".emacs": "Emacs Lisp",
".emacs.desktop": "Emacs Lisp",
".factor-boot-rc": "Factor",
".factor-rc": "Factor",
".gclient": "Python",
".gnus": "Emacs Lisp",
".jshintrc": "JSON",
".nvimrc": "Vim script",
".php_cs": "PHP",
".php_cs.dist": "PHP",
".project": "XML",
".pryrc": "Ruby",
".spacemacs": "Emacs Lisp",
".vimrc": "Vim script",
".viper": "Emacs Lisp",
"APKBUILD": "Alpine Abuild",
"App.config": "XML",
"Appraisals": "Ruby",
"BSDmakefile": "Makefile",
"BUCK": "Python",
"BUILD": "Python",
"Berksfile": "Ruby",
"Brewfile": "Ruby",
"Buildfile": "Ruby",
"CMakeLists.txt": "CMake",
"COPYING": "Text",
"COPYRIGHT.regex": "Text",
"Cakefile": "CoffeeScript",
"Cask": "Emacs Lisp",
"Dangerfile": "Ruby",
"Deliverfile": "Ruby",
"Dockerfile": "Dockerfile",
"Emakefile": "Erlang",
"FONTLOG": "Text",
"Fakefile": "Fancy",
"Fastfile": "Ruby",
"GNUmakefile": "Makefile",
"Gemfile": "Ruby",
"Gemfile.lock": "Ruby",
"Guardfile": "Ruby",
"INSTALL": "Text",
"INSTALL.mysql": "Text",
"Jakefile": "JavaScript",
"Jarfile": "Ruby",
"Jenkinsfile": "Groovy",
"Kbuild": "Makefile",
"LICENSE": "Text",
"LICENSE.mysql": "Text",
"Makefile": "Makefile",
"Makefile.am": "Makefile",
"Makefile.boot": "Makefile",
"Makefile.frag": "Makefile",
"Makefile.in": "Makefile",
"Makefile.inc": "Makefile",
"Mavenfile": "Ruby",
"Modulefile": "Puppet",
"NEWS": "Text",
"Notebook": "Jupyter Notebook",
"NuGet.config": "XML",
"Nukefile": "Nu",
"PKGBUILD": "Shell",
"Phakefile": "PHP",
"Podfile": "Ruby",
"Project.ede": "Emacs Lisp",
"Puppetfile": "Ruby",
"README.1ST": "Text",
"README.me": "Text",
"README.mysql": "Text",
"ROOT": "Isabelle ROOT",
"Rexfile": "Perl6",
"SConscript": "Python",
"SConstruct": "Python",
"Settings.StyleCop": "XML",
"Slakefile": "LiveScript",
"Snakefile": "Python",
"Snapfile": "Ruby",
"Thorfile": "Ruby",
"Vagrantfile": "Ruby",
"WORKSPACE": "Python",
"Web.Debug.config": "XML",
"Web.Release.config": "XML",
"Web.config": "XML",
"XCompose": "XCompose",
"_emacs": "Emacs Lisp",
"_vimrc": "Vim script",
"abbrev_defs": "Emacs Lisp",
"ant.xml": "Ant Build System",
"build.xml": "Ant Build System",
"buildfile": "Ruby",
"click.me": "Text",
"composer.lock": "JSON",
"configure.ac": "M4Sugar",
"delete.me": "Text",
"descrip.mmk": "Module Management System",
"descrip.mms": "Module Management System",
"gradlew": "Shell",
"gvimrc": "Vim script",
"keep.me": "Text",
"ld.script": "Linker Script",
"makefile": "Makefile",
"makefile.sco": "Makefile",
"mcmod.info": "JSON",
"meson.build": "Meson",
"meson_options.txt": "Meson",
"mix.lock": "Elixir",
"mkfile": "Makefile",
"mmn": "Roff",
"mmt": "Roff",
"nginx.conf": "Nginx",
"nvimrc": "Vim script",
"packages.config": "XML",
"pom.xml": "Maven POM",
"read.me": "Text",
"rebar.config": "Erlang",
"rebar.config.lock": "Erlang",
"rebar.lock": "Erlang",
"riemann.config": "Clojure",
"test.me": "Text",
"vimrc": "Vim script",
"wscript": "Python",
"xcompose": "XCompose",
}

View File

@ -0,0 +1,11 @@
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 }}
var languagesByFilename = map[string]string{
{{range $filename, $language := . -}}
"{{ $filename }}": {{- printf "%q" $language -}},
{{end -}}
}

View File

@ -0,0 +1,50 @@
package generator
import (
"bytes"
"io"
"text/template"
yaml "gopkg.in/yaml.v2"
)
// Filenames reads from buf and builds filenames_map.go file from filenamesTmplPath.
func Filenames(data []byte, filenamesTmplPath, filenamesTmplName, commit string) ([]byte, error) {
languages := make(map[string]*languageInfo)
if err := yaml.Unmarshal(data, &languages); err != nil {
return nil, err
}
languagesByFilename := buildFilenameLanguageMap(languages)
buf := &bytes.Buffer{}
if err := executeFilenamesTemplate(buf, languagesByFilename, filenamesTmplPath, filenamesTmplName, commit); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func buildFilenameLanguageMap(languages map[string]*languageInfo) map[string]string {
filenameLangMap := make(map[string]string)
for lang, langInfo := range languages {
for _, filename := range langInfo.Filenames {
filenameLangMap[filename] = lang
}
}
return filenameLangMap
}
func executeFilenamesTemplate(out io.Writer, languagesByFilename map[string]string, filenamesTmplPath, filenamesTmpl, commit string) error {
fmap := template.FuncMap{
"getCommit": func() string { return commit },
}
t := template.Must(template.New(filenamesTmpl).Funcs(fmap).ParseFiles(filenamesTmplPath))
if err := t.Execute(out, languagesByFilename); err != nil {
return err
}
return nil
}

View File

@ -47,130 +47,103 @@ const (
interpretersGold = "test_files/interpreters.gold" interpretersGold = "test_files/interpreters.gold"
interpretersTestTmplPath = "test_files/interpreters.test.go.tmpl" interpretersTestTmplPath = "test_files/interpreters.test.go.tmpl"
interpretersTestTmplName = "interpreters.test.go.tmpl" interpretersTestTmplName = "interpreters.test.go.tmpl"
// Filenames test
filenamesTestFile = "test_files/filenames.test.yml"
filenamesGold = "test_files/filenames.gold"
filenamesTestTmplPath = "test_files/filenames.test.go.tmpl"
filenamesTestTmplName = "filenames.test.go.tmpl"
) )
func TestFromFile(t *testing.T) { func TestFromFile(t *testing.T) {
goldLang, err := ioutil.ReadFile(langGold)
assert.NoError(t, err)
goldContent, err := ioutil.ReadFile(contentGold)
assert.NoError(t, err)
goldVendor, err := ioutil.ReadFile(vendorGold)
assert.NoError(t, err)
goldDocumentation, err := ioutil.ReadFile(documentationGold)
assert.NoError(t, err)
goldTypes, err := ioutil.ReadFile(typesGold)
assert.NoError(t, err)
goldInterpreters, err := ioutil.ReadFile(interpretersGold)
assert.NoError(t, err)
outPathLang, err := ioutil.TempFile("/tmp", "generator-test-")
assert.NoError(t, err)
defer os.Remove(outPathLang.Name())
outPathContent, err := ioutil.TempFile("/tmp", "generator-test-")
assert.NoError(t, err)
defer os.Remove(outPathContent.Name())
outPathVendor, err := ioutil.TempFile("/tmp", "generator-test-")
assert.NoError(t, err)
defer os.Remove(outPathVendor.Name())
outPathDocumentation, err := ioutil.TempFile("/tmp", "generator-test-")
assert.NoError(t, err)
defer os.Remove(outPathDocumentation.Name())
outPathTypes, err := ioutil.TempFile("/tmp", "generator-test-")
assert.NoError(t, err)
defer os.Remove(outPathTypes.Name())
outPathInterpreters, err := ioutil.TempFile("/tmp", "generator-test-")
assert.NoError(t, err)
defer os.Remove(outPathInterpreters.Name())
tests := []struct { tests := []struct {
name string name string
fileToParse string fileToParse string
outPath string
tmplPath string tmplPath string
tmplName string tmplName string
commit string commit string
generate Func generate Func
wantOut []byte wantOut string
}{ }{
{ {
name: "TestFromFile_Language", name: "TestFromFile_Language",
fileToParse: ymlTestFile, fileToParse: ymlTestFile,
outPath: outPathLang.Name(),
tmplPath: languagesTestTmplPath, tmplPath: languagesTestTmplPath,
tmplName: languagesTestTmplName, tmplName: languagesTestTmplName,
commit: commitTest, commit: commitTest,
generate: Languages, generate: Languages,
wantOut: goldLang, wantOut: langGold,
}, },
{ {
name: "TestFromFile_Heuristics", name: "TestFromFile_Heuristics",
fileToParse: heuristicsTestFile, fileToParse: heuristicsTestFile,
outPath: outPathContent.Name(),
tmplPath: contentTestTmplPath, tmplPath: contentTestTmplPath,
tmplName: contentTestTmplName, tmplName: contentTestTmplName,
commit: commitTest, commit: commitTest,
generate: Heuristics, generate: Heuristics,
wantOut: goldContent, wantOut: contentGold,
}, },
{ {
name: "TestFromFile_Vendor", name: "TestFromFile_Vendor",
fileToParse: vendorTestFile, fileToParse: vendorTestFile,
outPath: outPathVendor.Name(),
tmplPath: vendorTestTmplPath, tmplPath: vendorTestTmplPath,
tmplName: vendorTestTmplName, tmplName: vendorTestTmplName,
commit: commitTest, commit: commitTest,
generate: Vendor, generate: Vendor,
wantOut: goldVendor, wantOut: vendorGold,
}, },
{ {
name: "TestFromFile_Documentation", name: "TestFromFile_Documentation",
fileToParse: documentationTestFile, fileToParse: documentationTestFile,
outPath: outPathDocumentation.Name(),
tmplPath: documentationTestTmplPath, tmplPath: documentationTestTmplPath,
tmplName: documentationTestTmplName, tmplName: documentationTestTmplName,
commit: commitTest, commit: commitTest,
generate: Documentation, generate: Documentation,
wantOut: goldDocumentation, wantOut: documentationGold,
}, },
{ {
name: "TestFromFile_Types", name: "TestFromFile_Types",
fileToParse: typesTestFile, fileToParse: typesTestFile,
outPath: outPathTypes.Name(),
tmplPath: typesTestTmplPath, tmplPath: typesTestTmplPath,
tmplName: typesTestTmplName, tmplName: typesTestTmplName,
commit: commitTest, commit: commitTest,
generate: Types, generate: Types,
wantOut: goldTypes, wantOut: typesGold,
}, },
{ {
name: "TestFromFile_Interpreters", name: "TestFromFile_Interpreters",
fileToParse: interpretersTestFile, fileToParse: interpretersTestFile,
outPath: outPathInterpreters.Name(),
tmplPath: interpretersTestTmplPath, tmplPath: interpretersTestTmplPath,
tmplName: interpretersTestTmplName, tmplName: interpretersTestTmplName,
commit: commitTest, commit: commitTest,
generate: Interpreters, generate: Interpreters,
wantOut: goldInterpreters, wantOut: interpretersGold,
},
{
name: "TestFromFile_Filenames",
fileToParse: filenamesTestFile,
tmplPath: filenamesTestTmplPath,
tmplName: filenamesTestTmplName,
commit: commitTest,
generate: Filenames,
wantOut: filenamesGold,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
err := FromFile(tt.fileToParse, tt.outPath, tt.tmplPath, tt.tmplName, tt.commit, tt.generate) gold, err := ioutil.ReadFile(tt.wantOut)
assert.NoError(t, err) assert.NoError(t, err)
out, err := ioutil.ReadFile(tt.outPath)
outPath, err := ioutil.TempFile("/tmp", "generator-test-")
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, tt.wantOut, out, fmt.Sprintf("FromFile() = %v, want %v", string(out), string(tt.wantOut))) defer os.Remove(outPath.Name())
err = FromFile(tt.fileToParse, outPath.Name(), tt.tmplPath, tt.tmplName, tt.commit, tt.generate)
assert.NoError(t, err)
out, err := ioutil.ReadFile(outPath.Name())
assert.NoError(t, err)
assert.EqualValues(t, gold, out, fmt.Sprintf("FromFile() = %v, want %v", string(out), string(tt.wantOut)))
}) })
} }
} }

View File

@ -14,6 +14,7 @@ type languageInfo struct {
Type string `yaml:"type,omitempty"` Type string `yaml:"type,omitempty"`
Extensions []string `yaml:"extensions,omitempty,flow"` Extensions []string `yaml:"extensions,omitempty,flow"`
Interpreters []string `yaml:"interpreters,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. // Languages reads from buf and builds languages.go file from languagesTmplPath.

View File

@ -0,0 +1,12 @@
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
var languagesByFilename = map[string]string{
"APKBUILD": "Alpine Abuild",
"CMakeLists.txt": "CMake",
"Cakefile": "CoffeeScript",
"mix.lock": "Elixir",
}

View File

@ -0,0 +1,11 @@
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 }}
var languagesByFilename = map[string]string{
{{range $filename, $language := . -}}
"{{ $filename }}": {{- printf "%q" $language -}},
{{end -}}
}

View File

@ -0,0 +1,13 @@
---
Alpine Abuild:
filenames:
- APKBUILD
CMake:
filenames:
- CMakeLists.txt
CoffeeScript:
filenames:
- Cakefile
Elixir:
filenames:
- mix.lock

View File

@ -42,6 +42,11 @@ const (
interpretersTmplPath = "internal/code-generator/assets/interpreters.go.tmpl" interpretersTmplPath = "internal/code-generator/assets/interpreters.go.tmpl"
interpretersTmpl = "interpreters.go.tmpl" interpretersTmpl = "interpreters.go.tmpl"
// filenames_map.go generation
filenamesFile = "filenames_map.go"
filenamesTmplPath = "internal/code-generator/assets/filenames.go.tmpl"
filenamesTmpl = "filenames.go.tmpl"
commitPath = ".git/refs/heads/master" commitPath = ".git/refs/heads/master"
) )
@ -67,6 +72,7 @@ func main() {
&generatorArgs{documentationYAML, documentationFile, documentationTmplPath, documentationTmpl, commit, generator.Documentation}, &generatorArgs{documentationYAML, documentationFile, documentationTmplPath, documentationTmpl, commit, generator.Documentation},
&generatorArgs{languagesYAML, typeFile, typeTmplPath, typeTmpl, commit, generator.Types}, &generatorArgs{languagesYAML, typeFile, typeTmplPath, typeTmpl, commit, generator.Types},
&generatorArgs{languagesYAML, interpretersFile, interpretersTmplPath, interpretersTmpl, commit, generator.Interpreters}, &generatorArgs{languagesYAML, interpretersFile, interpretersTmplPath, interpretersTmpl, commit, generator.Interpreters},
&generatorArgs{languagesYAML, filenamesFile, filenamesTmplPath, filenamesTmpl, commit, generator.Filenames},
} }
for _, args := range argsList { for _, args := range argsList {