-
Notifications
You must be signed in to change notification settings - Fork 53
/
main.go
157 lines (130 loc) · 5.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"io/ioutil"
"log"
"path/filepath"
"github.com/go-enry/go-enry/v2/internal/code-generator/generator"
)
var (
// directories
samplesDir = filepath.Join(".linguist", "samples")
libDir = filepath.Join(".linguist", "lib", "linguist")
assetsDir = filepath.Join("internal", "code-generator", "assets")
// languages info file
languagesYAML = filepath.Join(libDir, "languages.yml")
// extension.go generation
extensionsFile = filepath.Join("data", "extension.go")
extensionsTmplPath = filepath.Join(assetsDir, "extension.go.tmpl")
extensionsTmpl = "extension.go.tmpl"
// content.go generation
heuristicsYAML = filepath.Join(libDir, "heuristics.yml")
contentFile = filepath.Join("data", "content.go")
contentTmplPath = filepath.Join(assetsDir, "content.go.tmpl")
contentTmpl = "content.go.tmpl"
// vendor.go generation
vendorYAML = filepath.Join(libDir, "vendor.yml")
vendorFile = filepath.Join("data", "vendor.go")
vendorTmplPath = filepath.Join(assetsDir, "vendor.go.tmpl")
vendorTmpl = "vendor.go.tmpl"
// documentation.go generation
documentationYAML = filepath.Join(libDir, "documentation.yml")
documentationFile = filepath.Join("data", "documentation.go")
documentationTmplPath = filepath.Join(assetsDir, "documentation.go.tmpl")
documentationTmpl = "documentation.go.tmpl"
// type.go generation
typeFile = filepath.Join("data", "type.go")
typeTmplPath = filepath.Join(assetsDir, "type.go.tmpl")
typeTmpl = "type.go.tmpl"
// interpreter.go generation
interpretersFile = filepath.Join("data", "interpreter.go")
interpretersTmplPath = filepath.Join(assetsDir, "interpreter.go.tmpl")
interpretersTmpl = "interpreter.go.tmpl"
// filename.go generation
filenamesFile = filepath.Join("data", "filename.go")
filenamesTmplPath = filepath.Join(assetsDir, "filename.go.tmpl")
filenamesTmpl = "filename.go.tmpl"
// alias.go generation
aliasesFile = filepath.Join("data", "alias.go")
aliasesTmplPath = filepath.Join(assetsDir, "alias.go.tmpl")
aliasesTmpl = "alias.go.tmpl"
// frequencies.go generation
frequenciesFile = filepath.Join("data", "frequencies.go")
frequenciesTmplPath = filepath.Join(assetsDir, "frequencies.go.tmpl")
frequenciesTmpl = "frequencies.go.tmpl"
// commit.go generation
commitFile = filepath.Join("data", "commit.go")
commitTmplPath = filepath.Join(assetsDir, "commit.go.tmpl")
commitTmpl = "commit.go.tmpl"
// mimeType.go generation
mimeTypeFile = filepath.Join("data", "mimeType.go")
mimeTypeTmplPath = filepath.Join(assetsDir, "mimeType.go.tmpl")
mimeTypeTmpl = "mimeType.go.tmpl"
// colors.go generation
colorsFile = filepath.Join("data", "colors.go")
colorsTmplPath = filepath.Join(assetsDir, "colors.go.tmpl")
colorsTmpl = "colors.go.tmpl"
// groups.go generation
groupsFile = filepath.Join("data", "groups.go")
groupsTmplPath = filepath.Join(assetsDir, "groups.go.tmpl")
groupsTmpl = "groups.go.tmpl"
// id.go generation
idFile = "data/id.go"
idTmplPath = filepath.Join(assetsDir, "id.go.tmpl")
idTmpl = "id.go.tmpl"
// languageInfo.go generation
languageInfoFile = filepath.Join("data", "languageInfo.go")
langaugeInfoTmplPath = filepath.Join(assetsDir, "languageInfo.go.tmpl")
langaugeInfoTmpl = "languageInfo.go.tmpl"
commitPath = filepath.Join(".linguist", ".git", "HEAD")
)
type generatorFiles struct {
generate generator.File
fileToParse string
samplesDir string
outPath string
tmplPath string
tmplName string
commit string
}
func main() {
commit, err := getCommit(commitPath)
if err != nil {
log.Printf("couldn't find commit: %v", err)
}
fileList := []*generatorFiles{
{generator.Extensions, languagesYAML, "", extensionsFile, extensionsTmplPath, extensionsTmpl, commit},
{generator.GenHeuristics, heuristicsYAML, "", contentFile, contentTmplPath, contentTmpl, commit},
{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},
{generator.Colors, languagesYAML, "", colorsFile, colorsTmplPath, colorsTmpl, commit},
{generator.Groups, languagesYAML, "", groupsFile, groupsTmplPath, groupsTmpl, commit},
{generator.ID, languagesYAML, "", idFile, idTmplPath, idTmpl, commit},
{generator.LanguageInfo, languagesYAML, "", languageInfoFile, langaugeInfoTmplPath, langaugeInfoTmpl, commit},
}
for _, file := range fileList {
if err := file.generate(file.fileToParse, file.samplesDir, file.outPath, file.tmplPath, file.tmplName, file.commit); err != nil {
log.Fatalf("failed to generate %q from %q - %+v", file.outPath, file.tmplPath, err)
}
}
}
func getCommit(path string) (string, error) {
commit, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
if string(commit) == "ref: refs/heads/master\n" {
path = filepath.Join(".linguist", ".git", string(commit[5:len(commit)-1]))
commit, err = ioutil.ReadFile(path)
if err != nil {
return "", err
}
}
return string(commit[:len(commit)-1]), nil
}