|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "gopkg.in/yaml.v3" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "text/template" |
| 11 | +) |
| 12 | + |
| 13 | +type Language struct { |
| 14 | + Type string `yaml:"type"` |
| 15 | + TmScope string `yaml:"tm_scope,omitempty"` |
| 16 | + AceMode string `yaml:"ace_mode"` |
| 17 | + CodemirrorMode string `yaml:"codemirror_mode,omitempty"` |
| 18 | + CodemirrorMimeType string `yaml:"codemirror_mime_type,omitempty"` |
| 19 | + Color string `yaml:"color,omitempty"` |
| 20 | + Aliases []string `yaml:"aliases,omitempty"` |
| 21 | + Extensions []string `yaml:"extensions"` |
| 22 | + Filenames []string `yaml:"filenames,omitempty"` |
| 23 | + Interpreters []string `yaml:"interpreters,omitempty"` |
| 24 | + Group string `yaml:"group,omitempty"` |
| 25 | + LanguageID int64 `yaml:"language_id"` |
| 26 | +} |
| 27 | + |
| 28 | +type Languages map[string]Language |
| 29 | + |
| 30 | +var languageTemplate = `// Code generated with lang-gen. DO NOT EDIT. |
| 31 | +
|
| 32 | +package filetree |
| 33 | +
|
| 34 | +type Language struct { |
| 35 | + Type string ` + "`yaml:\"type\"`" + ` |
| 36 | + TmScope string ` + "`yaml:\"tm_scope,omitempty\"`" + ` |
| 37 | + AceMode string ` + "`yaml:\"ace_mode\"`" + ` |
| 38 | + CodemirrorMode string ` + "`yaml:\"codemirror_mode,omitempty\"`" + ` |
| 39 | + CodemirrorMimeType string ` + "`yaml:\"codemirror_mime_type,omitempty\"`" + ` |
| 40 | + Color string ` + "`yaml:\"color,omitempty\"`" + ` |
| 41 | + Aliases []string ` + "`yaml:\"aliases,omitempty\"`" + ` |
| 42 | + Extensions []string ` + "`yaml:\"extensions\"`" + ` |
| 43 | + Filenames []string ` + "`yaml:\"filenames,omitempty\"`" + ` |
| 44 | + Interpreters []string ` + "`yaml:\"interpreters,omitempty\"`" + ` |
| 45 | + Group string ` + "`yaml:\"group,omitempty\"`" + ` |
| 46 | + LanguageID int64 ` + "`yaml:\"language_id\"`" + ` |
| 47 | +} |
| 48 | +
|
| 49 | +var languages = map[string]Language{ |
| 50 | + {{- range $key, $value := .}} |
| 51 | + "{{ $key }}": { |
| 52 | + Type: "{{ $value.Type }}", |
| 53 | + TmScope: "{{ $value.TmScope }}", |
| 54 | + AceMode: "{{ $value.AceMode }}", |
| 55 | + CodemirrorMode: "{{ $value.CodemirrorMode }}", |
| 56 | + CodemirrorMimeType: "{{ $value.CodemirrorMimeType }}", |
| 57 | + Color: "{{ $value.Color }}", |
| 58 | + Aliases: []string{ {{ join "\", \"" $value.Aliases "\"" }} }, |
| 59 | + Extensions: []string{ {{ join "\", \"" $value.Extensions "\"" }} }, |
| 60 | + Filenames: []string{ {{ join "\", \"" $value.Filenames "\"" }} }, |
| 61 | + Interpreters: []string{ {{ join "\", \"" $value.Interpreters "\"" }} }, |
| 62 | + Group: "{{ $value.Group }}", |
| 63 | + LanguageID: {{ $value.LanguageID }}, |
| 64 | + }, |
| 65 | + {{- end}} |
| 66 | +} |
| 67 | +` |
| 68 | + |
| 69 | +// join function will join the string slice with a comma |
| 70 | +func join(sep string, s []string, surroundingStr string) string { |
| 71 | + if len(s) == 0 { |
| 72 | + return "" |
| 73 | + } |
| 74 | + if surroundingStr == "" { |
| 75 | + return strings.Join(s, sep) |
| 76 | + } |
| 77 | + return surroundingStr + strings.Join(s, sep) + surroundingStr |
| 78 | +} |
| 79 | + |
| 80 | +func main() { |
| 81 | + |
| 82 | + var languages Languages |
| 83 | + |
| 84 | + // read the file from web |
| 85 | + client := &http.Client{} |
| 86 | + resp, err := client.Get("https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml") |
| 87 | + |
| 88 | + if err != nil { |
| 89 | + log.Fatalf("error: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + defer resp.Body.Close() |
| 93 | + |
| 94 | + data, err := io.ReadAll(resp.Body) |
| 95 | + |
| 96 | + if err != nil { |
| 97 | + log.Fatalf("error: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + err = yaml.Unmarshal(data, &languages) |
| 101 | + if err != nil { |
| 102 | + log.Fatalf("error: %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + // write the data to a go file using the template |
| 106 | + // so that we can embed the data in the binary |
| 107 | + |
| 108 | + // create a file |
| 109 | + file, err := os.Create("pkg/filetree/languages.go") |
| 110 | + if err != nil { |
| 111 | + log.Fatalf("error: %v", err) |
| 112 | + } |
| 113 | + defer file.Close() |
| 114 | + |
| 115 | + // use the data to render the template to disk |
| 116 | + tmpl, err := template.New("languages").Funcs(template.FuncMap{"join": join}).Parse(languageTemplate) |
| 117 | + if err != nil { |
| 118 | + log.Fatalf("error: %v", err) |
| 119 | + } |
| 120 | + |
| 121 | + err = tmpl.Execute(file, languages) |
| 122 | + if err != nil { |
| 123 | + log.Fatalf("error: %v", err) |
| 124 | + } |
| 125 | +} |
0 commit comments