Debian Codegraph @

stack/regen.go

raw
1 // Copyright 2017 Marc-Antoine Ruel. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 //go:build tools
6
7 package main
8
9 import (
10 "bytes"
11 "encoding/base64"
12 "fmt"
13 "os"
14 "regexp"
15 "strconv"
16 "text/template"
17 )
18
19 const content = `// Code generated by regen.go. DO NOT EDIT.
20
21 package stack
22
23 import (
24 "html/template"
25 )
26
27 const indexHTML = {{.IndexHTML}}
28
29 // favicon is the bomb emoji U+1F4A3 in Noto Emoji as a 128x128 base64 encoded
30 // PNG.
31 //
32 // See README.md for license and how to retrieve it.
33 const favicon template.HTML = "{{.Favicon}}"
34 `
35
36 // loadGoroutines returns "goroutines.tpl" slightly processed for density.
37 func loadGoroutines() ([]byte, error) {
38 htmlRaw, err := os.ReadFile("goroutines.tpl")
39 if err != nil {
40 return nil, err
41 }
42 // Strip out leading whitespace.
43 re := regexp.MustCompile("(\\n[ \\t]*)+")
44 htmlRaw = re.ReplaceAll(htmlRaw, []byte("\n"))
45 return htmlRaw, nil
46 }
47
48 func mainImpl() error {
49 htmlRaw, err := loadGoroutines()
50 if err != nil {
51 return err
52 }
53
54 // See README.md how to generate it.
55 iconRaw, err := os.ReadFile("emoji_u1f4a3_64.gif")
56 if err != nil {
57 return err
58 }
59
60 t, err := template.New("t").Parse(content)
61 if err != nil {
62 return err
63 }
64 data := map[string]string{
65 "IndexHTML": strconv.Quote(string(htmlRaw)),
66 "Favicon": base64.StdEncoding.EncodeToString(iconRaw),
67 }
68 b := bytes.Buffer{}
69 if err := t.Execute(&b, data); err != nil {
70 return err
71 }
72 return os.WriteFile("data.go", b.Bytes(), 0666)
73 }
74
75 func main() {
76 if err := mainImpl(); err != nil {
77 fmt.Fprintf(os.Stderr, "Failed: %s\n", err)
78 os.Exit(1)
79 }
80 }