2017-04-04 11:10:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2020-03-25 13:00:24 +00:00
|
|
|
"path/filepath"
|
2017-04-04 11:10:35 +00:00
|
|
|
|
2020-03-19 16:31:29 +00:00
|
|
|
"github.com/go-enry/go-enry/v2/internal/code-generator/generator"
|
2017-04-04 11:10:35 +00:00
|
|
|
)
|
|
|
|
|
2020-03-25 13:00:24 +00:00
|
|
|
var (
|
|
|
|
// directories
|
|
|
|
samplesDir = filepath.Join(".linguist", "samples")
|
|
|
|
libDir = filepath.Join(".linguist", "lib", "linguist")
|
|
|
|
assetsDir = filepath.Join("internal", "code-generator", "assets")
|
2017-04-19 09:24:01 +00:00
|
|
|
|
2020-03-25 13:00:24 +00:00
|
|
|
// languages info file
|
|
|
|
languagesYAML = filepath.Join(libDir, "languages.yml")
|
2017-06-13 11:56:07 +00:00
|
|
|
|
2017-05-29 08:05:16 +00:00
|
|
|
// extension.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
extensionsFile = filepath.Join("data", "extension.go")
|
|
|
|
extensionsTmplPath = filepath.Join(assetsDir, "extension.go.tmpl")
|
2017-06-12 11:42:20 +00:00
|
|
|
extensionsTmpl = "extension.go.tmpl"
|
2017-04-04 11:10:35 +00:00
|
|
|
|
2017-04-11 10:10:05 +00:00
|
|
|
// content.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
heuristicsYAML = filepath.Join(libDir, "heuristics.yml")
|
|
|
|
contentFile = filepath.Join("data", "content.go")
|
|
|
|
contentTmplPath = filepath.Join(assetsDir, "content.go.tmpl")
|
2017-04-04 11:10:35 +00:00
|
|
|
contentTmpl = "content.go.tmpl"
|
|
|
|
|
2017-05-29 08:05:16 +00:00
|
|
|
// vendor.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
vendorYAML = filepath.Join(libDir, "vendor.yml")
|
|
|
|
vendorFile = filepath.Join("data", "vendor.go")
|
|
|
|
vendorTmplPath = filepath.Join(assetsDir, "vendor.go.tmpl")
|
2017-04-07 07:25:49 +00:00
|
|
|
vendorTmpl = "vendor.go.tmpl"
|
2017-04-06 15:31:17 +00:00
|
|
|
|
2017-05-29 08:05:16 +00:00
|
|
|
// documentation.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
documentationYAML = filepath.Join(libDir, "documentation.yml")
|
|
|
|
documentationFile = filepath.Join("data", "documentation.go")
|
|
|
|
documentationTmplPath = filepath.Join(assetsDir, "documentation.go.tmpl")
|
2017-04-10 09:30:23 +00:00
|
|
|
documentationTmpl = "documentation.go.tmpl"
|
|
|
|
|
2017-04-11 10:10:05 +00:00
|
|
|
// type.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
typeFile = filepath.Join("data", "type.go")
|
|
|
|
typeTmplPath = filepath.Join(assetsDir, "type.go.tmpl")
|
2017-06-12 11:42:20 +00:00
|
|
|
typeTmpl = "type.go.tmpl"
|
2017-04-11 09:26:23 +00:00
|
|
|
|
2017-05-29 08:05:16 +00:00
|
|
|
// interpreter.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
interpretersFile = filepath.Join("data", "interpreter.go")
|
|
|
|
interpretersTmplPath = filepath.Join(assetsDir, "interpreter.go.tmpl")
|
2017-06-12 11:42:20 +00:00
|
|
|
interpretersTmpl = "interpreter.go.tmpl"
|
2017-04-17 06:14:46 +00:00
|
|
|
|
2017-05-29 08:05:16 +00:00
|
|
|
// filename.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
filenamesFile = filepath.Join("data", "filename.go")
|
|
|
|
filenamesTmplPath = filepath.Join(assetsDir, "filename.go.tmpl")
|
2017-06-12 11:42:20 +00:00
|
|
|
filenamesTmpl = "filename.go.tmpl"
|
2017-04-19 08:22:46 +00:00
|
|
|
|
2017-05-29 08:05:16 +00:00
|
|
|
// alias.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
aliasesFile = filepath.Join("data", "alias.go")
|
|
|
|
aliasesTmplPath = filepath.Join(assetsDir, "alias.go.tmpl")
|
2017-06-12 11:42:20 +00:00
|
|
|
aliasesTmpl = "alias.go.tmpl"
|
2017-05-04 13:03:54 +00:00
|
|
|
|
2017-05-25 10:33:26 +00:00
|
|
|
// frequencies.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
frequenciesFile = filepath.Join("data", "frequencies.go")
|
|
|
|
frequenciesTmplPath = filepath.Join(assetsDir, "frequencies.go.tmpl")
|
2017-05-25 10:33:26 +00:00
|
|
|
frequenciesTmpl = "frequencies.go.tmpl"
|
|
|
|
|
2017-06-21 13:18:27 +00:00
|
|
|
// commit.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
commitFile = filepath.Join("data", "commit.go")
|
|
|
|
commitTmplPath = filepath.Join(assetsDir, "commit.go.tmpl")
|
2017-06-21 13:18:27 +00:00
|
|
|
commitTmpl = "commit.go.tmpl"
|
|
|
|
|
2017-07-11 08:52:46 +00:00
|
|
|
// mimeType.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
mimeTypeFile = filepath.Join("data", "mimeType.go")
|
|
|
|
mimeTypeTmplPath = filepath.Join(assetsDir, "mimeType.go.tmpl")
|
2017-07-11 08:52:46 +00:00
|
|
|
mimeTypeTmpl = "mimeType.go.tmpl"
|
2017-07-10 10:50:52 +00:00
|
|
|
|
2019-07-19 20:28:57 +00:00
|
|
|
// colors.go generation
|
2020-03-25 13:00:24 +00:00
|
|
|
colorsFile = filepath.Join("data", "colors.go")
|
|
|
|
colorsTmplPath = filepath.Join(assetsDir, "colors.go.tmpl")
|
2019-07-19 20:28:57 +00:00
|
|
|
colorsTmpl = "colors.go.tmpl"
|
|
|
|
|
2020-03-21 13:37:39 +00:00
|
|
|
// groups.go generation
|
|
|
|
groupsFile = filepath.Join("data", "groups.go")
|
|
|
|
groupsTmplPath = filepath.Join(assetsDir, "groups.go.tmpl")
|
|
|
|
groupsTmpl = "groups.go.tmpl"
|
|
|
|
|
2020-03-25 13:00:24 +00:00
|
|
|
commitPath = filepath.Join(".linguist", ".git", "HEAD")
|
2017-04-04 11:10:35 +00:00
|
|
|
)
|
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
type generatorFiles struct {
|
|
|
|
generate generator.File
|
2017-04-11 09:26:23 +00:00
|
|
|
fileToParse string
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir string
|
2017-04-11 09:26:23 +00:00
|
|
|
outPath string
|
|
|
|
tmplPath string
|
|
|
|
tmplName string
|
|
|
|
commit string
|
|
|
|
}
|
|
|
|
|
2017-04-04 11:10:35 +00:00
|
|
|
func main() {
|
|
|
|
commit, err := getCommit(commitPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("couldn't find commit: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
fileList := []*generatorFiles{
|
2018-04-28 13:12:03 +00:00
|
|
|
{generator.Extensions, languagesYAML, "", extensionsFile, extensionsTmplPath, extensionsTmpl, commit},
|
2019-02-14 11:47:45 +00:00
|
|
|
{generator.GenHeuristics, heuristicsYAML, "", contentFile, contentTmplPath, contentTmpl, commit},
|
2018-04-28 13:12:03 +00:00
|
|
|
{generator.Vendor, vendorYAML, "", vendorFile, vendorTmplPath, vendorTmpl, commit},
|
|
|
|
{generator.Documentation, documentationYAML, "", documentationFile, documentationTmplPath, documentationTmpl, commit},
|
|
|
|
{generator.Types, languagesYAML, "", typeFile, typeTmplPath, typeTmpl, commit},
|
|
|
|
{generator.Interpreters, languagesYAML, "", interpretersFile, interpretersTmplPath, interpretersTmpl, commit},
|
|
|
|
{generator.Filenames, languagesYAML, samplesDir, filenamesFile, filenamesTmplPath, filenamesTmpl, commit},
|
|
|
|
{generator.Aliases, languagesYAML, "", aliasesFile, aliasesTmplPath, aliasesTmpl, commit},
|
|
|
|
{generator.Frequencies, "", samplesDir, frequenciesFile, frequenciesTmplPath, frequenciesTmpl, commit},
|
|
|
|
{generator.Commit, "", "", commitFile, commitTmplPath, commitTmpl, commit},
|
|
|
|
{generator.MimeType, languagesYAML, "", mimeTypeFile, mimeTypeTmplPath, mimeTypeTmpl, commit},
|
2019-07-19 20:28:57 +00:00
|
|
|
{generator.Colors, languagesYAML, "", colorsFile, colorsTmplPath, colorsTmpl, commit},
|
2020-03-21 13:37:39 +00:00
|
|
|
{generator.Groups, languagesYAML, "", groupsFile, groupsTmplPath, groupsTmpl, commit},
|
2017-04-06 15:31:17 +00:00
|
|
|
}
|
2017-04-10 09:30:23 +00:00
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
for _, file := range fileList {
|
|
|
|
if err := file.generate(file.fileToParse, file.samplesDir, file.outPath, file.tmplPath, file.tmplName, file.commit); err != nil {
|
2017-04-11 09:26:23 +00:00
|
|
|
log.Println(err)
|
|
|
|
}
|
2017-04-10 09:30:23 +00:00
|
|
|
}
|
2017-04-04 11:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCommit(path string) (string, error) {
|
|
|
|
commit, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-06-19 09:20:24 +00:00
|
|
|
if string(commit) == "ref: refs/heads/master\n" {
|
2020-03-25 18:27:46 +00:00
|
|
|
path = filepath.Join(".linguist", ".git", string(commit[5:len(commit)-1]))
|
2017-06-21 13:18:27 +00:00
|
|
|
commit, err = ioutil.ReadFile(path)
|
2017-06-19 09:20:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 13:18:27 +00:00
|
|
|
return string(commit[:len(commit)-1]), nil
|
2017-04-04 11:10:35 +00:00
|
|
|
}
|