write a canonical header for machine-generated files

Signed-off-by: Denys Smirnov <denys@sourced.tech>
This commit is contained in:
Denys Smirnov
2018-04-28 16:12:03 +03:00
committed by Denys Smirnov
parent 40a21f8e0b
commit 7eafe024af
49 changed files with 167 additions and 742 deletions

View File

@ -2,12 +2,10 @@ package generator
import (
"bytes"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"strings"
"text/template"
yaml "gopkg.in/yaml.v2"
)
// Aliases reads from fileToParse and builds source file from tmplPath. It complies with type File signature.
@ -55,14 +53,5 @@ func convertToAliasKey(s string) (key string) {
}
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
return executeTemplate(out, aliasesTmpl, aliasesTmplPath, commit, nil, languagesByAlias)
}

View File

@ -2,11 +2,9 @@ package generator
import (
"bytes"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"text/template"
yaml "gopkg.in/yaml.v2"
)
// Documentation reads from fileToParse and builds source file from tmplPath. It complies with type File signature.
@ -30,14 +28,5 @@ func Documentation(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit
}
func executeDocumentationTemplate(out io.Writer, regexpList []string, tmplPath, tmplName, commit string) error {
fmap := template.FuncMap{
"getCommit": func() string { return commit },
}
t := template.Must(template.New(tmplName).Funcs(fmap).ParseFiles(tmplPath))
if err := t.Execute(out, regexpList); err != nil {
return err
}
return nil
return executeTemplate(out, tmplName, tmplPath, commit, nil, regexpList)
}

View File

@ -81,14 +81,7 @@ func buildLanguageExtensionsMap(languages map[string]*languageInfo) map[string][
func executeExtensionsTemplate(out io.Writer, extInfo *extensionsInfo, tmplPath, tmplName, 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(tmplName).Funcs(fmap).ParseFiles(tmplPath))
if err := t.Execute(out, extInfo); err != nil {
return err
}
return nil
return executeTemplate(out, tmplName, tmplPath, commit, fmap, extInfo)
}

View File

@ -91,14 +91,7 @@ func buildFilenameLanguageMap(languages map[string]*languageInfo) map[string][]s
func executeFilenamesTemplate(out io.Writer, languagesByFilename map[string][]string, tmplPath, tmplName, 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(tmplName).Funcs(fmap).ParseFiles(tmplPath))
if err := t.Execute(out, languagesByFilename); err != nil {
return err
}
return nil
return executeTemplate(out, tmplName, tmplPath, commit, fmap, languagesByFilename)
}

View File

@ -1,8 +1,12 @@
package generator
import (
"bytes"
"go/format"
"io"
"io/ioutil"
"path/filepath"
"text/template"
)
// File is the function's type that generate source file from a file to be parsed, linguist's samples dir and a template.
@ -13,10 +17,45 @@ func formatedWrite(outPath string, source []byte) error {
if err != nil {
return err
}
if err := ioutil.WriteFile(outPath, formatedSource, 0666); err != nil {
return err
}
return nil
}
func executeTemplate(w io.Writer, name, path, commit string, fmap template.FuncMap, data interface{}) error {
getCommit := func() string {
return commit
}
buf := bytes.NewBuffer(nil)
const headerTmpl = "header.go.tmpl"
headerPath := filepath.Join(filepath.Dir(path), headerTmpl)
h := template.Must(template.New(headerTmpl).Funcs(template.FuncMap{
"getCommit": getCommit,
}).ParseFiles(headerPath))
if err := h.Execute(buf, data); err != nil {
return err
}
if fmap == nil {
fmap = make(template.FuncMap)
}
fmap["getCommit"] = getCommit
t := template.Must(template.New(name).Funcs(fmap).ParseFiles(path))
if err := t.Execute(buf, data); err != nil {
return err
}
src, err := format.Source(buf.Bytes())
if err != nil {
return err
}
_, err = w.Write(src)
return err
}

View File

@ -13,68 +13,71 @@ import (
)
const (
linguistURL = "https://github.com/github/linguist.git"
linguistURL = "https://github.com/github/linguist.git"
linguistClonedEnvVar = "ENRY_TEST_REPO"
commit = "d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68"
samplesDir = "samples"
languagesFile = "lib/linguist/languages.yml"
commit = "d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68"
samplesDir = "samples"
languagesFile = "lib/linguist/languages.yml"
testDir = "test_files"
assetsDir = "../assets"
// Extensions test
extensionGold = "test_files/extension.gold"
extensionTestTmplPath = "../assets/extension.go.tmpl"
extensionGold = testDir + "/extension.gold"
extensionTestTmplPath = assetsDir + "/extension.go.tmpl"
extensionTestTmplName = "extension.go.tmpl"
// Heuristics test
heuristicsTestFile = "lib/linguist/heuristics.rb"
contentGold = "test_files/content.gold"
contentTestTmplPath = "../assets/content.go.tmpl"
contentGold = testDir + "/content.gold"
contentTestTmplPath = assetsDir + "/content.go.tmpl"
contentTestTmplName = "content.go.tmpl"
// Vendor test
vendorTestFile = "lib/linguist/vendor.yml"
vendorGold = "test_files/vendor.gold"
vendorTestTmplPath = "../assets/vendor.go.tmpl"
vendorGold = testDir + "/vendor.gold"
vendorTestTmplPath = assetsDir + "/vendor.go.tmpl"
vendorTestTmplName = "vendor.go.tmpl"
// Documentation test
documentationTestFile = "lib/linguist/documentation.yml"
documentationGold = "test_files/documentation.gold"
documentationTestTmplPath = "../assets/documentation.go.tmpl"
documentationGold = testDir + "/documentation.gold"
documentationTestTmplPath = assetsDir + "/documentation.go.tmpl"
documentationTestTmplName = "documentation.go.tmpl"
// Types test
typeGold = "test_files/type.gold"
typeTestTmplPath = "../assets/type.go.tmpl"
typeGold = testDir + "/type.gold"
typeTestTmplPath = assetsDir + "/type.go.tmpl"
typeTestTmplName = "type.go.tmpl"
// Interpreters test
interpreterGold = "test_files/interpreter.gold"
interpreterTestTmplPath = "../assets/interpreter.go.tmpl"
interpreterGold = testDir + "/interpreter.gold"
interpreterTestTmplPath = assetsDir + "/interpreter.go.tmpl"
interpreterTestTmplName = "interpreter.go.tmpl"
// Filenames test
filenameGold = "test_files/filename.gold"
filenameTestTmplPath = "../assets/filename.go.tmpl"
filenameGold = testDir + "/filename.gold"
filenameTestTmplPath = assetsDir + "/filename.go.tmpl"
filenameTestTmplName = "filename.go.tmpl"
// Aliases test
aliasGold = "test_files/alias.gold"
aliasTestTmplPath = "../assets/alias.go.tmpl"
aliasGold = testDir + "/alias.gold"
aliasTestTmplPath = assetsDir + "/alias.go.tmpl"
aliasTestTmplName = "alias.go.tmpl"
// Frequencies test
frequenciesGold = "test_files/frequencies.gold"
frequenciesTestTmplPath = "../assets/frequencies.go.tmpl"
frequenciesGold = testDir + "/frequencies.gold"
frequenciesTestTmplPath = assetsDir + "/frequencies.go.tmpl"
frequenciesTestTmplName = "frequencies.go.tmpl"
// commit test
commitGold = "test_files/commit.gold"
commitTestTmplPath = "../assets/commit.go.tmpl"
commitGold = testDir + "/commit.gold"
commitTestTmplPath = assetsDir + "/commit.go.tmpl"
commitTestTmplName = "commit.go.tmpl"
// mime test
mimeTypeGold = "test_files/mimeType.gold"
mimeTypeTestTmplPath = "../assets/mimeType.go.tmpl"
mimeTypeGold = testDir + "/mimeType.gold"
mimeTypeTestTmplPath = assetsDir + "/mimeType.go.tmpl"
mimeTypeTestTmplName = "mimeType.go.tmpl"
)

View File

@ -428,7 +428,6 @@ func buildLanguagesHeuristics(langsList [][]string, heuristicsList [][]*heuristi
func executeContentTemplate(out io.Writer, disambiguators []*disambiguator, tmplPath, tmplName, commit string) error {
fmap := template.FuncMap{
"getCommit": func() string { return commit },
"getAllHeuristics": getAllHeuristics,
"returnStringSlice": func(slice []string) string {
if len(slice) == 0 {
@ -440,13 +439,7 @@ func executeContentTemplate(out io.Writer, disambiguators []*disambiguator, tmpl
"returnLanguages": returnLanguages,
"avoidLanguage": avoidLanguage,
}
t := template.Must(template.New(tmplName).Funcs(fmap).ParseFiles(tmplPath))
if err := t.Execute(out, disambiguators); err != nil {
return err
}
return nil
return executeTemplate(out, tmplName, tmplPath, commit, fmap, disambiguators)
}
func getAllHeuristics(disambiguators []*disambiguator) []*heuristic {

View File

@ -47,14 +47,7 @@ func buildInterpreterLanguagesMap(languages map[string]*languageInfo, orderedKey
func executeInterpretersTemplate(out io.Writer, languagesByInterpreter map[string][]string, tmplPath, tmplName, 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(tmplName).Funcs(fmap).ParseFiles(tmplPath))
if err := t.Execute(out, languagesByInterpreter); err != nil {
return err
}
return nil
return executeTemplate(out, tmplName, tmplPath, commit, fmap, languagesByInterpreter)
}

View File

@ -2,16 +2,13 @@ package generator
import (
"bytes"
"text/template"
)
// Commit takes a commit and builds the source file from tmplPath. It complies with type File signature.
func Commit(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error {
buf := &bytes.Buffer{}
t := template.Must(template.New(tmplName).ParseFiles(tmplPath))
if err := t.Execute(buf, commit); err != nil {
if err := executeTemplate(buf, tmplName, tmplPath, commit, nil, nil); err != nil {
return err
}
return formatedWrite(outPath, buf.Bytes())
}

View File

@ -2,11 +2,9 @@ package generator
import (
"bytes"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"text/template"
yaml "gopkg.in/yaml.v2"
)
func MimeType(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error {
@ -42,14 +40,5 @@ func buildLanguageMimeMap(languages map[string]*languageInfo) map[string]string
}
func executeMimeTemplate(out io.Writer, langMimeMap map[string]string, tmplPath, tmplName, commit string) error {
fmap := template.FuncMap{
"getCommit": func() string { return commit },
}
t := template.Must(template.New(tmplName).Funcs(fmap).ParseFiles(tmplPath))
if err := t.Execute(out, langMimeMap); err != nil {
return err
}
return nil
return executeTemplate(out, tmplName, tmplPath, commit, nil, langMimeMap)
}

View File

@ -157,7 +157,6 @@ func getTokens(samples []string) ([]string, error) {
func executeFrequenciesTemplate(out io.Writer, freqs *samplesFrequencies, tmplPath, tmplName, commit string) error {
fmap := template.FuncMap{
"getCommit": func() string { return commit },
"toFloat64": func(num int) string { return fmt.Sprintf("%f", float64(num)) },
"orderKeys": func(m map[string]int) []string {
keys := make([]string, 0, len(m))
@ -187,11 +186,5 @@ func executeFrequenciesTemplate(out io.Writer, freqs *samplesFrequencies, tmplPa
},
"quote": strconv.Quote,
}
t := template.Must(template.New(tmplName).Funcs(fmap).ParseFiles(tmplPath))
if err := t.Execute(out, freqs); err != nil {
return err
}
return nil
return executeTemplate(out, tmplName, tmplPath, commit, fmap, freqs)
}

View File

@ -1,9 +1,8 @@
package data
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/enry.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Code generated by gopkg.in/src-d/enry.v1/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68
package data
// LanguagesByAlias keeps alias for different languages and use the name of the languages as an 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{

View File

@ -1,8 +1,7 @@
package data
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/enry.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Code generated by gopkg.in/src-d/enry.v1/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68
package data
// linguist's commit from which files were generated.
var LinguistCommit = "d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68"

View File

@ -1,9 +1,8 @@
package data
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/enry.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Code generated by gopkg.in/src-d/enry.v1/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68
package data
import "gopkg.in/toqueteos/substring.v1"
type languageMatcher func([]byte) []string

View File

@ -1,9 +1,8 @@
package data
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/enry.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Code generated by gopkg.in/src-d/enry.v1/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68
package data
import "gopkg.in/toqueteos/substring.v1"
var DocumentationMatchers = substring.Or(

View File

@ -1,9 +1,8 @@
package data
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/enry.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Code generated by gopkg.in/src-d/enry.v1/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68
package data
var LanguagesByExtension = map[string][]string{
".1": {"Roff"},
".1in": {"Roff"},

View File

@ -1,9 +1,8 @@
package data
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/enry.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Code generated by gopkg.in/src-d/enry.v1/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68
package data
var LanguagesByFilename = map[string][]string{
".Rprofile": {"R"},
".XCompose": {"XCompose"},

View File

@ -1,9 +1,8 @@
package data
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/enry.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Code generated by gopkg.in/src-d/enry.v1/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68
package data
var LanguagesLogProbabilities = map[string]float64{
"1C Enterprise": -5.724674,
"ABAP": -7.516433,

View File

@ -1,9 +1,8 @@
package data
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/enry.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Code generated by gopkg.in/src-d/enry.v1/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68
package data
var LanguagesByInterpreter = map[string][]string{
"Rscript": {"R"},
"apl": {"APL"},

View File

@ -1,9 +1,8 @@
package data
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/enry.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Code generated by gopkg.in/src-d/enry.v1/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68
package data
var LanguagesMime = map[string]string{
"AGS Script": "text/x-c++src",
"APL": "text/apl",

View File

@ -1,9 +1,8 @@
package data
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/enry.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Code generated by gopkg.in/src-d/enry.v1/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68
package data
var LanguagesType = map[string]int{
"1C Enterprise": 2,
"ABAP": 2,

View File

@ -1,9 +1,8 @@
package data
// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/enry.v1/internal/code-generator
// THIS FILE SHOULD NOT BE EDITED BY HAND
// Code generated by gopkg.in/src-d/enry.v1/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68
package data
import "gopkg.in/toqueteos/substring.v1"
var VendorMatchers = substring.Or(

View File

@ -2,11 +2,9 @@ package generator
import (
"bytes"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"text/template"
yaml "gopkg.in/yaml.v2"
)
var typeToTypeConst = map[string]int{
@ -48,14 +46,5 @@ func buildLanguageTypeMap(languages map[string]*languageInfo) map[string]int {
}
func executeTypesTemplate(out io.Writer, langTypeMap map[string]int, tmplPath, tmplName, commit string) error {
fmap := template.FuncMap{
"getCommit": func() string { return commit },
}
t := template.Must(template.New(tmplName).Funcs(fmap).ParseFiles(tmplPath))
if err := t.Execute(out, langTypeMap); err != nil {
return err
}
return nil
return executeTemplate(out, tmplName, tmplPath, commit, nil, langTypeMap)
}

View File

@ -2,11 +2,9 @@ package generator
import (
"bytes"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"text/template"
yaml "gopkg.in/yaml.v2"
)
// Vendor reads from fileToParse and builds source file from tmplPath. It complies with type File signature.
@ -30,14 +28,5 @@ func Vendor(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string)
}
func executeVendorTemplate(out io.Writer, regexpList []string, tmplPath, tmplName, commit string) error {
fmap := template.FuncMap{
"getCommit": func() string { return commit },
}
t := template.Must(template.New(tmplName).Funcs(fmap).ParseFiles(tmplPath))
if err := t.Execute(out, regexpList); err != nil {
return err
}
return nil
return executeTemplate(out, tmplName, tmplPath, commit, nil, regexpList)
}