From 645bdd73313eefea60a288bc3457dc62da772918 Mon Sep 17 00:00:00 2001 From: Manuel Carmona Date: Wed, 19 Apr 2017 10:22:46 +0200 Subject: [PATCH] added filenames_map.go generation languagesByFilename now is a map[string]string --- filename.go | 10 ++ filenames_map.go | 140 ++++++++++++++++++ .../code-generator/assets/filenames.go.tmpl | 11 ++ .../code-generator/generator/filenames.go | 50 +++++++ .../generator/generator_test.go | 91 ++++-------- internal/code-generator/generator/langs.go | 1 + .../generator/test_files/filenames.gold | 12 ++ .../test_files/filenames.test.go.tmpl | 11 ++ .../generator/test_files/filenames.test.yml | 13 ++ internal/code-generator/main.go | 6 + 10 files changed, 286 insertions(+), 59 deletions(-) create mode 100644 filename.go create mode 100644 filenames_map.go create mode 100644 internal/code-generator/assets/filenames.go.tmpl create mode 100644 internal/code-generator/generator/filenames.go create mode 100644 internal/code-generator/generator/test_files/filenames.gold create mode 100644 internal/code-generator/generator/test_files/filenames.test.go.tmpl create mode 100644 internal/code-generator/generator/test_files/filenames.test.yml diff --git a/filename.go b/filename.go new file mode 100644 index 0000000..49a88f4 --- /dev/null +++ b/filename.go @@ -0,0 +1,10 @@ +package slinguist + +func GetLanguageByFilename(filename string) (lang string, safe bool) { + lang, safe = languagesByFilename[filename] + if lang == "" { + lang = OtherLanguage + } + + return +} diff --git a/filenames_map.go b/filenames_map.go new file mode 100644 index 0000000..e198c80 --- /dev/null +++ b/filenames_map.go @@ -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", +} diff --git a/internal/code-generator/assets/filenames.go.tmpl b/internal/code-generator/assets/filenames.go.tmpl new file mode 100644 index 0000000..f0a7952 --- /dev/null +++ b/internal/code-generator/assets/filenames.go.tmpl @@ -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 -}} +} diff --git a/internal/code-generator/generator/filenames.go b/internal/code-generator/generator/filenames.go new file mode 100644 index 0000000..17f8482 --- /dev/null +++ b/internal/code-generator/generator/filenames.go @@ -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 +} diff --git a/internal/code-generator/generator/generator_test.go b/internal/code-generator/generator/generator_test.go index 84a0c8c..46e8fe1 100644 --- a/internal/code-generator/generator/generator_test.go +++ b/internal/code-generator/generator/generator_test.go @@ -47,130 +47,103 @@ const ( interpretersGold = "test_files/interpreters.gold" interpretersTestTmplPath = "test_files/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) { - 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 { name string fileToParse string - outPath string tmplPath string tmplName string commit string generate Func - wantOut []byte + wantOut string }{ { name: "TestFromFile_Language", fileToParse: ymlTestFile, - outPath: outPathLang.Name(), tmplPath: languagesTestTmplPath, tmplName: languagesTestTmplName, commit: commitTest, generate: Languages, - wantOut: goldLang, + wantOut: langGold, }, { name: "TestFromFile_Heuristics", fileToParse: heuristicsTestFile, - outPath: outPathContent.Name(), tmplPath: contentTestTmplPath, tmplName: contentTestTmplName, commit: commitTest, generate: Heuristics, - wantOut: goldContent, + wantOut: contentGold, }, { name: "TestFromFile_Vendor", fileToParse: vendorTestFile, - outPath: outPathVendor.Name(), tmplPath: vendorTestTmplPath, tmplName: vendorTestTmplName, commit: commitTest, generate: Vendor, - wantOut: goldVendor, + wantOut: vendorGold, }, { name: "TestFromFile_Documentation", fileToParse: documentationTestFile, - outPath: outPathDocumentation.Name(), tmplPath: documentationTestTmplPath, tmplName: documentationTestTmplName, commit: commitTest, generate: Documentation, - wantOut: goldDocumentation, + wantOut: documentationGold, }, { name: "TestFromFile_Types", fileToParse: typesTestFile, - outPath: outPathTypes.Name(), tmplPath: typesTestTmplPath, tmplName: typesTestTmplName, commit: commitTest, generate: Types, - wantOut: goldTypes, + wantOut: typesGold, }, { name: "TestFromFile_Interpreters", fileToParse: interpretersTestFile, - outPath: outPathInterpreters.Name(), tmplPath: interpretersTestTmplPath, tmplName: interpretersTestTmplName, commit: commitTest, 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 { 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) - out, err := ioutil.ReadFile(tt.outPath) + + outPath, err := ioutil.TempFile("/tmp", "generator-test-") 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))) }) } } diff --git a/internal/code-generator/generator/langs.go b/internal/code-generator/generator/langs.go index 99ec8d5..f69e670 100644 --- a/internal/code-generator/generator/langs.go +++ b/internal/code-generator/generator/langs.go @@ -14,6 +14,7 @@ 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. diff --git a/internal/code-generator/generator/test_files/filenames.gold b/internal/code-generator/generator/test_files/filenames.gold new file mode 100644 index 0000000..f3ac3b5 --- /dev/null +++ b/internal/code-generator/generator/test_files/filenames.gold @@ -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", +} diff --git a/internal/code-generator/generator/test_files/filenames.test.go.tmpl b/internal/code-generator/generator/test_files/filenames.test.go.tmpl new file mode 100644 index 0000000..f0a7952 --- /dev/null +++ b/internal/code-generator/generator/test_files/filenames.test.go.tmpl @@ -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 -}} +} diff --git a/internal/code-generator/generator/test_files/filenames.test.yml b/internal/code-generator/generator/test_files/filenames.test.yml new file mode 100644 index 0000000..30113db --- /dev/null +++ b/internal/code-generator/generator/test_files/filenames.test.yml @@ -0,0 +1,13 @@ +--- +Alpine Abuild: + filenames: + - APKBUILD +CMake: + filenames: + - CMakeLists.txt +CoffeeScript: + filenames: + - Cakefile +Elixir: + filenames: + - mix.lock diff --git a/internal/code-generator/main.go b/internal/code-generator/main.go index 8a060d0..5dd29d9 100644 --- a/internal/code-generator/main.go +++ b/internal/code-generator/main.go @@ -42,6 +42,11 @@ const ( interpretersTmplPath = "internal/code-generator/assets/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" ) @@ -67,6 +72,7 @@ func main() { &generatorArgs{documentationYAML, documentationFile, documentationTmplPath, documentationTmpl, commit, generator.Documentation}, &generatorArgs{languagesYAML, typeFile, typeTmplPath, typeTmpl, commit, generator.Types}, &generatorArgs{languagesYAML, interpretersFile, interpretersTmplPath, interpretersTmpl, commit, generator.Interpreters}, + &generatorArgs{languagesYAML, filenamesFile, filenamesTmplPath, filenamesTmpl, commit, generator.Filenames}, } for _, args := range argsList {