/
templates.go
69 lines (52 loc) · 1.7 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"bytes"
_ "embed"
"os"
"path/filepath"
"text/template"
"github.com/streamingfast/cli"
)
//go:embed templates/application/goreleaser.yaml.gotmpl
var goreleaserAppTmpl []byte
//go:embed templates/library/goreleaser.yaml.gotmpl
var goreleaserLibTmpl []byte
//go:embed templates/CHANGELOG.md.gotmpl
var changelogTmpl []byte
//go:embed templates/sfreleaser-golang.yaml.gotmpl
var sfreleaserGolangYamlTmpl []byte
//go:embed templates/sfreleaser-rust.yaml.gotmpl
var sfreleaserRustYamlTmpl []byte
func getInstallTemplateModel(global *GlobalModel) map[string]any {
return map[string]any{
"global": global,
}
}
func getReleaseTemplateModel(global *GlobalModel, release *ReleaseModel) map[string]any {
return map[string]any{
"global": global,
"release": release,
}
}
func renderGoreleaserFile(global *GlobalModel, release *ReleaseModel, github *GitHubReleaseModel) {
goreleaserTemplate := goreleaserAppTmpl
if global.Variant == VariantLibrary {
goreleaserTemplate = goreleaserLibTmpl
}
renderTemplate(github.GoreleaseConfigPath, true, goreleaserTemplate, getReleaseTemplateModel(global, release))
}
func renderTemplate(file string, overwrite bool, tmplContent []byte, model map[string]any) (wrote string) {
if !cli.FileExists(file) || overwrite {
tmpl, err := template.New(file).Funcs(templateFuncs).Parse(string(tmplContent))
cli.NoError(err, "Unable to instantiate template")
buffer := bytes.NewBuffer(nil)
tmpl.Execute(buffer, model)
directory := filepath.Dir(file)
if !cli.DirectoryExists(directory) {
cli.NoError(os.MkdirAll(directory, os.ModePerm), "Making directories for template file %q", file)
}
cli.WriteFile(file, buffer.String())
return file
}
return ""
}