2018-02-17 20:12:41 +02:00
|
|
|
package templater
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-16 20:45:41 +02:00
|
|
|
"strings"
|
2018-02-17 20:12:41 +02:00
|
|
|
"text/template"
|
|
|
|
|
2023-06-15 17:04:03 +02:00
|
|
|
"golang.org/x/exp/maps"
|
|
|
|
|
2020-08-19 10:59:58 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile"
|
2018-02-17 20:12:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Templater is a help struct that allow us to call "replaceX" funcs multiple
|
|
|
|
// times, without having to check for error each time. The first error that
|
|
|
|
// happen will be assigned to r.err, and consecutive calls to funcs will just
|
|
|
|
// return the zero value.
|
|
|
|
type Templater struct {
|
2020-05-16 20:45:41 +02:00
|
|
|
Vars *taskfile.Vars
|
|
|
|
RemoveNoValue bool
|
2018-02-17 20:12:41 +02:00
|
|
|
|
2023-03-30 22:03:59 +02:00
|
|
|
cacheMap map[string]any
|
2019-08-25 22:16:59 +02:00
|
|
|
err error
|
2018-02-17 20:12:41 +02:00
|
|
|
}
|
|
|
|
|
2019-09-14 22:54:41 +02:00
|
|
|
func (r *Templater) ResetCache() {
|
2019-08-25 22:16:59 +02:00
|
|
|
r.cacheMap = r.Vars.ToCacheMap()
|
2019-08-25 19:30:00 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:12:41 +02:00
|
|
|
func (r *Templater) Replace(str string) string {
|
2023-06-15 17:04:03 +02:00
|
|
|
return r.replace(str, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Templater) ReplaceWithExtra(str string, extra map[string]any) string {
|
|
|
|
return r.replace(str, extra)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Templater) replace(str string, extra map[string]any) string {
|
2018-02-17 20:12:41 +02:00
|
|
|
if r.err != nil || str == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
templ, err := template.New("").Funcs(templateFuncs).Parse(str)
|
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-08-25 22:16:59 +02:00
|
|
|
if r.cacheMap == nil {
|
|
|
|
r.cacheMap = r.Vars.ToCacheMap()
|
2018-02-17 20:12:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
2023-06-15 17:04:03 +02:00
|
|
|
if extra == nil {
|
|
|
|
err = templ.Execute(&b, r.cacheMap)
|
|
|
|
} else {
|
|
|
|
// Copy the map to avoid modifying the cached map
|
|
|
|
m := maps.Clone(r.cacheMap)
|
|
|
|
maps.Copy(m, extra)
|
|
|
|
err = templ.Execute(&b, m)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-02-17 20:12:41 +02:00
|
|
|
r.err = err
|
|
|
|
return ""
|
|
|
|
}
|
2020-05-16 20:45:41 +02:00
|
|
|
if r.RemoveNoValue {
|
|
|
|
return strings.ReplaceAll(b.String(), "<no value>", "")
|
|
|
|
}
|
2018-02-17 20:12:41 +02:00
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Templater) ReplaceSlice(strs []string) []string {
|
|
|
|
if r.err != nil || len(strs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
new := make([]string, len(strs))
|
|
|
|
for i, str := range strs {
|
|
|
|
new[i] = r.Replace(str)
|
|
|
|
}
|
|
|
|
return new
|
|
|
|
}
|
|
|
|
|
2023-11-30 03:38:12 +02:00
|
|
|
func (r *Templater) ReplaceGlobs(globs []*taskfile.Glob) []*taskfile.Glob {
|
|
|
|
if r.err != nil || len(globs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
new := make([]*taskfile.Glob, len(globs))
|
|
|
|
for i, g := range globs {
|
|
|
|
new[i] = &taskfile.Glob{
|
|
|
|
Glob: r.Replace(g.Glob),
|
|
|
|
Negate: g.Negate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new
|
|
|
|
}
|
|
|
|
|
2020-03-29 21:54:59 +02:00
|
|
|
func (r *Templater) ReplaceVars(vars *taskfile.Vars) *taskfile.Vars {
|
2023-06-15 17:04:03 +02:00
|
|
|
return r.replaceVars(vars, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Templater) ReplaceVarsWithExtra(vars *taskfile.Vars, extra map[string]any) *taskfile.Vars {
|
|
|
|
return r.replaceVars(vars, extra)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Templater) replaceVars(vars *taskfile.Vars, extra map[string]any) *taskfile.Vars {
|
2021-03-20 16:56:19 +02:00
|
|
|
if r.err != nil || vars.Len() == 0 {
|
2018-02-17 20:12:41 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-29 18:21:21 +02:00
|
|
|
var newVars taskfile.Vars
|
2022-05-15 02:00:15 +02:00
|
|
|
_ = vars.Range(func(k string, v taskfile.Var) error {
|
2023-11-29 18:21:21 +02:00
|
|
|
var newVar taskfile.Var
|
|
|
|
switch value := v.Value.(type) {
|
|
|
|
case string:
|
|
|
|
newVar.Value = r.ReplaceWithExtra(value, extra)
|
|
|
|
}
|
|
|
|
newVar.Live = v.Live
|
|
|
|
newVar.Sh = r.ReplaceWithExtra(v.Sh, extra)
|
|
|
|
newVars.Set(k, newVar)
|
2020-03-29 21:54:59 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2023-11-29 18:21:21 +02:00
|
|
|
return &newVars
|
2018-02-17 20:12:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Templater) Err() error {
|
|
|
|
return r.err
|
|
|
|
}
|