github.com/xzntrc/go-enry/v2@v2.0.0-20230215091818-766cc1d65498/internal/code-generator/main.go (about) 1 package main 2 3 import ( 4 "io/ioutil" 5 "log" 6 "path/filepath" 7 8 "github.com/go-enry/go-enry/v2/internal/code-generator/generator" 9 ) 10 11 var ( 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 = filepath.Join(assetsDir, "id.go.tmpl") 91 idTmpl = "id.go.tmpl" 92 93 // languageInfo.go generation 94 languageInfoFile = filepath.Join("data", "languageInfo.go") 95 langaugeInfoTmplPath = filepath.Join(assetsDir, "languageInfo.go.tmpl") 96 langaugeInfoTmpl = "languageInfo.go.tmpl" 97 98 commitPath = filepath.Join(".linguist", ".git", "HEAD") 99 ) 100 101 type generatorFiles struct { 102 generate generator.File 103 fileToParse string 104 samplesDir string 105 outPath string 106 tmplPath string 107 tmplName string 108 commit string 109 } 110 111 func main() { 112 commit, err := getCommit(commitPath) 113 if err != nil { 114 log.Printf("couldn't find commit: %v", err) 115 } 116 117 fileList := []*generatorFiles{ 118 {generator.Extensions, languagesYAML, "", extensionsFile, extensionsTmplPath, extensionsTmpl, commit}, 119 {generator.GenHeuristics, heuristicsYAML, "", contentFile, contentTmplPath, contentTmpl, commit}, 120 {generator.Vendor, vendorYAML, "", vendorFile, vendorTmplPath, vendorTmpl, commit}, 121 {generator.Documentation, documentationYAML, "", documentationFile, documentationTmplPath, documentationTmpl, commit}, 122 {generator.Types, languagesYAML, "", typeFile, typeTmplPath, typeTmpl, commit}, 123 {generator.Interpreters, languagesYAML, "", interpretersFile, interpretersTmplPath, interpretersTmpl, commit}, 124 {generator.Filenames, languagesYAML, samplesDir, filenamesFile, filenamesTmplPath, filenamesTmpl, commit}, 125 {generator.Aliases, languagesYAML, "", aliasesFile, aliasesTmplPath, aliasesTmpl, commit}, 126 {generator.Frequencies, "", samplesDir, frequenciesFile, frequenciesTmplPath, frequenciesTmpl, commit}, 127 {generator.Commit, "", "", commitFile, commitTmplPath, commitTmpl, commit}, 128 {generator.MimeType, languagesYAML, "", mimeTypeFile, mimeTypeTmplPath, mimeTypeTmpl, commit}, 129 {generator.Colors, languagesYAML, "", colorsFile, colorsTmplPath, colorsTmpl, commit}, 130 {generator.Groups, languagesYAML, "", groupsFile, groupsTmplPath, groupsTmpl, commit}, 131 {generator.ID, languagesYAML, "", idFile, idTmplPath, idTmpl, commit}, 132 {generator.LanguageInfo, languagesYAML, "", languageInfoFile, langaugeInfoTmplPath, langaugeInfoTmpl, commit}, 133 } 134 135 for _, file := range fileList { 136 if err := file.generate(file.fileToParse, file.samplesDir, file.outPath, file.tmplPath, file.tmplName, file.commit); err != nil { 137 log.Fatalf("error generating template %q to %q: %+v", file.tmplPath, file.outPath, err) 138 } 139 } 140 } 141 142 func getCommit(path string) (string, error) { 143 commit, err := ioutil.ReadFile(path) 144 if err != nil { 145 return "", err 146 } 147 148 if string(commit) == "ref: refs/heads/master\n" { 149 path = filepath.Join(".linguist", ".git", string(commit[5:len(commit)-1])) 150 commit, err = ioutil.ReadFile(path) 151 if err != nil { 152 return "", err 153 } 154 } 155 156 return string(commit[:len(commit)-1]), nil 157 }