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