diff --git a/internal/code-generator/assets/interpreters.go.tmpl b/internal/code-generator/assets/interpreters.go.tmpl new file mode 100644 index 0000000..f6fb2d4 --- /dev/null +++ b/internal/code-generator/assets/interpreters.go.tmpl @@ -0,0 +1,11 @@ +package slinguist + +// CODE GENERATED AUTOMATICALLY WITH github.com/src-d/simple-linguist/cli/slinguist-generate +// THIS FILE SHOULD NOT BE EDITED BY HAND +// Extracted from github/linguist commit: {{ getCommit }} + +var languagesByInterpreter = map[string][]string{ + {{range $interpreter, $languages := . -}} + "{{ $interpreter }}": { {{- $languages | formatStringSlice -}} }, + {{end -}} +} diff --git a/internal/code-generator/generator/generator_test.go b/internal/code-generator/generator/generator_test.go index 524d805..84a0c8c 100644 --- a/internal/code-generator/generator/generator_test.go +++ b/internal/code-generator/generator/generator_test.go @@ -41,6 +41,12 @@ const ( typesGold = "test_files/type.gold" typesTestTmplPath = "test_files/type.test.go.tmpl" typesTestTmplName = "type.test.go.tmpl" + + // Interpreters test + interpretersTestFile = "test_files/interpreters.test.yml" + interpretersGold = "test_files/interpreters.gold" + interpretersTestTmplPath = "test_files/interpreters.test.go.tmpl" + interpretersTestTmplName = "interpreters.test.go.tmpl" ) func TestFromFile(t *testing.T) { @@ -59,6 +65,9 @@ func TestFromFile(t *testing.T) { 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()) @@ -79,6 +88,10 @@ func TestFromFile(t *testing.T) { 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 @@ -139,6 +152,16 @@ func TestFromFile(t *testing.T) { generate: Types, wantOut: goldTypes, }, + { + name: "TestFromFile_Interpreters", + fileToParse: interpretersTestFile, + outPath: outPathInterpreters.Name(), + tmplPath: interpretersTestTmplPath, + tmplName: interpretersTestTmplName, + commit: commitTest, + generate: Interpreters, + wantOut: goldInterpreters, + }, } for _, tt := range tests { diff --git a/internal/code-generator/generator/interpreters.go b/internal/code-generator/generator/interpreters.go new file mode 100644 index 0000000..61313ff --- /dev/null +++ b/internal/code-generator/generator/interpreters.go @@ -0,0 +1,54 @@ +package generator + +import ( + "bytes" + "io" + "strings" + "text/template" + + "gopkg.in/yaml.v2" +) + +// Interpreters reads from buf and builds interpreters_map.go file from interpretersTmplPath. +func Interpreters(data []byte, interpretersTmplPath, interpretersTmplName, commit string) ([]byte, error) { + languages := make(map[string]*languageInfo) + if err := yaml.Unmarshal(data, &languages); err != nil { + return nil, err + } + + orderedKeys := getAlphabeticalOrderedKeys(languages) + languagesByInterpreter := buildInterpreterLanguagesMap(languages, orderedKeys) + + buf := &bytes.Buffer{} + if err := executeInterpretersTemplate(buf, languagesByInterpreter, interpretersTmplPath, interpretersTmplName, commit); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} + +func buildInterpreterLanguagesMap(languages map[string]*languageInfo, orderedKeys []string) map[string][]string { + interpreterLangsMap := make(map[string][]string) + for _, lang := range orderedKeys { + langInfo := languages[lang] + for _, interpreter := range langInfo.Interpreters { + interpreterLangsMap[interpreter] = append(interpreterLangsMap[interpreter], lang) + } + } + + return interpreterLangsMap +} + +func executeInterpretersTemplate(out io.Writer, languagesByInterpreter map[string][]string, interpretersTmplPath, interpretersTmpl, 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(interpretersTmpl).Funcs(fmap).ParseFiles(interpretersTmplPath)) + if err := t.Execute(out, languagesByInterpreter); err != nil { + return err + } + + return nil +} diff --git a/internal/code-generator/generator/test_files/interpreters.gold b/internal/code-generator/generator/test_files/interpreters.gold new file mode 100644 index 0000000..e8494dd --- /dev/null +++ b/internal/code-generator/generator/test_files/interpreters.gold @@ -0,0 +1,16 @@ +package slinguist + +// CODE GENERATED AUTOMATICALLY WITH github.com/src-d/simple-linguist/cli/slinguist-generate +// THIS FILE SHOULD NOT BE EDITED BY HAND +// Extracted from github/linguist commit: fe8b44ab8a225b1ffa75b983b916ea22fee5b6f7 + +var languagesByInterpreter = map[string][]string{ + "bash": {"Shell"}, + "nush": {"Nu"}, + "python": {"Python"}, + "python2": {"Python"}, + "python3": {"Python"}, + "rc": {"Shell"}, + "sh": {"Shell"}, + "zsh": {"Shell"}, +} diff --git a/internal/code-generator/generator/test_files/interpreters.test.go.tmpl b/internal/code-generator/generator/test_files/interpreters.test.go.tmpl new file mode 100644 index 0000000..f6fb2d4 --- /dev/null +++ b/internal/code-generator/generator/test_files/interpreters.test.go.tmpl @@ -0,0 +1,11 @@ +package slinguist + +// CODE GENERATED AUTOMATICALLY WITH github.com/src-d/simple-linguist/cli/slinguist-generate +// THIS FILE SHOULD NOT BE EDITED BY HAND +// Extracted from github/linguist commit: {{ getCommit }} + +var languagesByInterpreter = map[string][]string{ + {{range $interpreter, $languages := . -}} + "{{ $interpreter }}": { {{- $languages | formatStringSlice -}} }, + {{end -}} +} diff --git a/internal/code-generator/generator/test_files/interpreters.test.yml b/internal/code-generator/generator/test_files/interpreters.test.yml new file mode 100644 index 0000000..0921bbd --- /dev/null +++ b/internal/code-generator/generator/test_files/interpreters.test.yml @@ -0,0 +1,15 @@ +--- +Nu: + interpreters: + - nush +Shell: + interpreters: + - bash + - rc + - sh + - zsh +Python: + interpreters: + - python + - python2 + - python3 diff --git a/internal/code-generator/main.go b/internal/code-generator/main.go index 91b7fdf..8a060d0 100644 --- a/internal/code-generator/main.go +++ b/internal/code-generator/main.go @@ -66,6 +66,7 @@ func main() { &generatorArgs{vendorYAML, vendorFile, vendorTmplPath, vendorTmpl, commit, generator.Vendor}, &generatorArgs{documentationYAML, documentationFile, documentationTmplPath, documentationTmpl, commit, generator.Documentation}, &generatorArgs{languagesYAML, typeFile, typeTmplPath, typeTmpl, commit, generator.Types}, + &generatorArgs{languagesYAML, interpretersFile, interpretersTmplPath, interpretersTmpl, commit, generator.Interpreters}, } for _, args := range argsList { diff --git a/interpreters_map.go b/interpreters_map.go new file mode 100644 index 0000000..7c9a2dc --- /dev/null +++ b/interpreters_map.go @@ -0,0 +1,98 @@ +package slinguist + +// CODE GENERATED AUTOMATICALLY WITH github.com/src-d/simple-linguist/cli/slinguist-generate +// THIS FILE SHOULD NOT BE EDITED BY HAND +// Extracted from github/linguist commit: dae33dc2b20cddc85d1300435c3be7118a7115a9 + +var languagesByInterpreter = map[string][]string{ + "Rscript": {"R"}, + "apl": {"APL"}, + "aplx": {"APL"}, + "awk": {"Awk"}, + "bash": {"Shell"}, + "bigloo": {"Scheme"}, + "boolector": {"SMT"}, + "ccl": {"Common Lisp"}, + "chicken": {"Scheme"}, + "clisp": {"Common Lisp"}, + "coffee": {"CoffeeScript"}, + "crystal": {"Crystal"}, + "csi": {"Scheme"}, + "cvc4": {"SMT"}, + "dart": {"Dart"}, + "dtrace": {"DTrace"}, + "dyalog": {"APL"}, + "ecl": {"Common Lisp"}, + "elixir": {"Elixir"}, + "escript": {"Erlang"}, + "fish": {"fish"}, + "gawk": {"Awk"}, + "gn": {"GN"}, + "gnuplot": {"Gnuplot"}, + "gosh": {"Scheme"}, + "groovy": {"Groovy"}, + "guile": {"Scheme"}, + "instantfpc": {"Pascal"}, + "io": {"Io"}, + "ioke": {"Ioke"}, + "jconsole": {"J"}, + "jruby": {"Ruby"}, + "julia": {"Julia"}, + "lisp": {"Common Lisp"}, + "lsl": {"LSL"}, + "lua": {"Lua", "Terra"}, + "macruby": {"Ruby"}, + "make": {"Makefile"}, + "mathsat5": {"SMT"}, + "mawk": {"Awk"}, + "mmi": {"Mercury"}, + "moon": {"MoonScript"}, + "nawk": {"Awk"}, + "newlisp": {"NewLisp"}, + "node": {"JavaScript"}, + "nush": {"Nu"}, + "ocaml": {"OCaml", "Reason"}, + "ocamlrun": {"OCaml"}, + "ocamlscript": {"OCaml"}, + "openrc-run": {"OpenRC runscript"}, + "opensmt": {"SMT"}, + "osascript": {"AppleScript"}, + "parrot": {"Parrot Assembly", "Parrot Internal Representation"}, + "perl": {"Perl"}, + "perl6": {"Perl6"}, + "php": {"PHP"}, + "picolisp": {"PicoLisp"}, + "pike": {"Pike"}, + "pil": {"PicoLisp"}, + "python": {"Python"}, + "python2": {"Python"}, + "python3": {"Python"}, + "qmake": {"QMake"}, + "r6rs": {"Scheme"}, + "racket": {"Racket"}, + "rake": {"Ruby"}, + "rbx": {"Ruby"}, + "rc": {"Shell"}, + "regina": {"REXX"}, + "rexx": {"REXX"}, + "ruby": {"Ruby"}, + "rune": {"E"}, + "runhaskell": {"Haskell"}, + "sbcl": {"Common Lisp"}, + "scala": {"Scala"}, + "sclang": {"SuperCollider"}, + "scsynth": {"SuperCollider"}, + "sh": {"Shell"}, + "smt-rat": {"SMT"}, + "smtinterpol": {"SMT"}, + "stp": {"SMT"}, + "swipl": {"Prolog"}, + "tcc": {"C"}, + "tclsh": {"Tcl"}, + "verit": {"SMT"}, + "wish": {"Tcl"}, + "yap": {"Prolog"}, + "yices2": {"SMT"}, + "z3": {"SMT"}, + "zsh": {"Shell"}, +}