Debian Codegraph @

core/templatecache.go

raw
1 package core
2
3 import (
4 "io"
5 "strings"
6 "text/template"
7 "time"
8
9 "github.com/Masterminds/sprig/v3"
10 )
11
12 // Batch data is a common structure for templates when generating change fragments.
13 type BatchData struct {
14 // Time of the change
15 Time time.Time
16 // Version of the change, will include "v" prefix if used
17 Version string
18 // Version of the release without the "v" prefix if used
19 VersionNoPrefix string
20 // Previous released version
21 PreviousVersion string
22 // Major value of the version
23 Major int
24 // Minor value of the version
25 Minor int
26 // Patch value of the version
27 Patch int
28 // Prerelease value of the version
29 Prerelease string
30 // Metadata value of the version
31 Metadata string
32 // Changes included in the batch
33 Changes []Change
34 // Env vars configured by the system.
35 // See [envPrefix](#config-envprefix) for configuration.
36 Env map[string]string
37 }
38
39 // Component data stores data related to writing component headers.
40 type ComponentData struct {
41 // Name of the component
42 Component string `required:"true"`
43 // Env vars configured by the system.
44 // See [envPrefix](#config-envprefix) for configuration.
45 Env map[string]string
46 }
47
48 // Kind data stores data related to writing kind headers.
49 type KindData struct {
50 // Name of the kind
51 Kind string `required:"true"`
52 // Env vars configured by the system.
53 // See [envPrefix](#config-envprefix) for configuration.
54 Env map[string]string
55 }
56
57 // Template cache handles running all the templates for change fragments.
58 // Included options include the default [go template](https://golang.org/pkg/text/template/)
59 // and [sprig functions](https://masterminds.github.io/sprig/) for formatting.
60 // Additionally, custom template functions are listed below for working with changes.
61 // example: yaml
62 // format: |
63 // ### Contributors
64 // {{- range (customs .Changes "Author" | uniq) }}
65 // * [{{.}}](https://github.com/{{.}})
66 // {{- end}}
67 type TemplateCache struct {
68 cache map[string]*template.Template
69 }
70
71 func NewTemplateCache() *TemplateCache {
72 return &TemplateCache{
73 cache: make(map[string]*template.Template),
74 }
75 }
76
77 func (tc *TemplateCache) Load(text string) (*template.Template, error) {
78 cachedTemplate, ok := tc.cache[text]
79 if ok {
80 return cachedTemplate, nil
81 }
82
83 templ, err := template.New(text).Funcs(tc.buildFuncMap()).Parse(text)
84 if err != nil {
85 // do not save our template if it had an error
86 return nil, err
87 }
88
89 tc.cache[text] = templ
90
91 return templ, err
92 }
93
94 func (tc *TemplateCache) Execute(text string, wr io.Writer, data any) error {
95 templ, err := tc.Load(text)
96 if err != nil {
97 return err
98 }
99
100 return templ.Execute(wr, data)
101 }
102
103 func (tc *TemplateCache) ExecuteString(text string, data any) (string, error) {
104 sb := strings.Builder{}
105
106 templ, err := tc.Load(text)
107 if err != nil {
108 return "", err
109 }
110
111 err = templ.Execute(&sb, data)
112 if err != nil {
113 return "", err
114 }
115
116 return sb.String(), nil
117 }
118
119 func (tc *TemplateCache) buildFuncMap() map[string]any {
120 funcs := sprig.TxtFuncMap()
121
122 funcs["count"] = tc.Count
123 funcs["components"] = tc.Components
124 funcs["kinds"] = tc.Kinds
125 funcs["bodies"] = tc.Bodies
126 funcs["times"] = tc.Times
127 funcs["customs"] = tc.Customs
128
129 return funcs
130 }
131
132 // Count will return the number of occurrences of a string in a slice.
133 // example: yaml
134 // format: "{{ kinds .Changes | count \"added\" }} kinds"
135 func (tc *TemplateCache) Count(value string, items []string) (int, error) {
136 count := 0
137
138 for _, i := range items {
139 if i == value {
140 count++
141 }
142 }
143
144 return count, nil
145 }
146
147 // Components will return all the components from the provided changes.
148 // example: yaml
149 // format: "{{components .Changes }} components"
150 func (tc *TemplateCache) Components(changes []Change) ([]string, error) {
151 comps := make([]string, len(changes))
152
153 for i, c := range changes {
154 comps[i] = c.Component
155 }
156
157 return comps, nil
158 }
159
160 // Kinds will return all the kindsi from the provided changes.
161 // example: yaml
162 // format: "{{ kinds .Changes }} kinds"
163 func (tc *TemplateCache) Kinds(changes []Change) ([]string, error) {
164 kinds := make([]string, len(changes))
165
166 for i, c := range changes {
167 kinds[i] = c.Kind
168 }
169
170 return kinds, nil
171 }
172
173 // Bodies will return all the bodies from the provided changes.
174 // example: yaml
175 // format: "{{ bodies .Changes }} bodies"
176 func (tc *TemplateCache) Bodies(changes []Change) ([]string, error) {
177 bodies := make([]string, len(changes))
178
179 for i, c := range changes {
180 bodies[i] = c.Body
181 }
182
183 return bodies, nil
184 }
185
186 // Times will return all the times from the provided changes.
187 // example: yaml
188 // format: "{{ times .Changes }} times"
189 func (tc *TemplateCache) Times(changes []Change) ([]time.Time, error) {
190 times := make([]time.Time, len(changes))
191
192 for i, c := range changes {
193 times[i] = c.Time
194 }
195
196 return times, nil
197 }
198
199 // Customs will return all the values from the custom map by a key.
200 // If a key is missing from a change, it will be an empty string.
201 // example: yaml
202 // format: "{{ customs .Changes \"Author\" }} authors"
203 func (tc *TemplateCache) Customs(changes []Change, key string) ([]string, error) {
204 values := make([]string, len(changes))
205
206 for i, c := range changes {
207 values[i] = c.Custom[key]
208 }
209
210 return values, nil
211 }