]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Handle rebuilds when resources passed to transform.Unmarshal etc. changes
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 19 Feb 2024 10:32:28 +0000 (11:32 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 19 Feb 2024 13:50:23 +0000 (14:50 +0100)
Fixes #12065

hugolib/rebuild_test.go
tpl/internal/go_templates/texttemplate/hugo_template.go
tpl/internal/go_templates/texttemplate/hugo_template_test.go
tpl/tplimpl/template_funcs.go

index d29215a011b5cb01d6294e8117474d41554bc25e..cf98f55da71b08d284272ba970c8ef33f18bb9e1 100644 (file)
@@ -77,6 +77,27 @@ func TestRebuildEditTextFileInLeafBundle(t *testing.T) {
        b.AssertRenderCountContent(1)
 }
 
+func TestRebuiEditUnmarshaledYamlFileInLeafBundle(t *testing.T) {
+       files := `
+-- hugo.toml --
+baseURL = "https://example.com"
+disableLiveReload = true
+disableKinds = ["taxonomy", "term", "sitemap", "robotsTXT", "404", "rss"]
+-- content/mybundle/index.md --
+-- content/mybundle/mydata.yml --
+foo: bar
+-- layouts/_default/single.html --
+MyData: {{ .Resources.Get "mydata.yml" | transform.Unmarshal }}|
+`
+       b := TestRunning(t, files)
+
+       b.AssertFileContent("public/mybundle/index.html", "MyData: map[foo:bar]")
+
+       b.EditFileReplaceAll("content/mybundle/mydata.yml", "bar", "bar edited").Build()
+
+       b.AssertFileContent("public/mybundle/index.html", "MyData: map[foo:bar edited]")
+}
+
 func TestRebuildEditTextFileInHomeBundle(t *testing.T) {
        b := TestRunning(t, rebuildFilesSimple)
        b.AssertFileContent("public/index.html", "Home Content.")
index 4db40ce82cfbb76e8eaa02e1c0fa1a316a47430d..276367a7c386d689a6bab75826318078b38df821 100644 (file)
@@ -44,6 +44,7 @@ type ExecHelper interface {
        GetFunc(ctx context.Context, tmpl Preparer, name string) (reflect.Value, reflect.Value, bool)
        GetMethod(ctx context.Context, tmpl Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value)
        GetMapValue(ctx context.Context, tmpl Preparer, receiver, key reflect.Value) (reflect.Value, bool)
+       OnCalled(ctx context.Context, tmpl Preparer, name string, args []reflect.Value, result reflect.Value)
 }
 
 // Executer executes a given template.
@@ -356,7 +357,14 @@ func (s *state) evalCall(dot, fun reflect.Value, isBuiltin bool, node parse.Node
                s.at(node)
                s.errorf("error calling %s: %w", name, err)
        }
-       return unwrap(v)
+       vv := unwrap(v)
+
+       // Added for Hugo
+       if s.helper != nil {
+               s.helper.OnCalled(s.ctx, s.prep, name, argv, vv)
+       }
+
+       return vv
 }
 
 func isTrue(val reflect.Value) (truth, ok bool) {
index c68b747dd905724bf35ecb4b2343696ced1d1094..920d96fac54cf7e73a181b725230025a98758656 100644 (file)
@@ -64,6 +64,9 @@ func (e *execHelper) GetMethod(ctx context.Context, tmpl Preparer, receiver refl
        return m, reflect.ValueOf("v2")
 }
 
+func (e *execHelper) OnCalled(ctx context.Context, tmpl Preparer, name string, args []reflect.Value, returnValue reflect.Value) {
+}
+
 func TestTemplateExecutor(t *testing.T) {
        c := qt.New(t)
 
index 8997c83d615cd1c1df5fe8c2556127eedbf84907..9d14b9e56b2ace05108fe74fbae56337b6a87a7f 100644 (file)
@@ -150,6 +150,30 @@ func (t *templateExecHelper) GetMethod(ctx context.Context, tmpl texttemplate.Pr
        return fn, zero
 }
 
+func (t *templateExecHelper) OnCalled(ctx context.Context, tmpl texttemplate.Preparer, name string, args []reflect.Value, result reflect.Value) {
+       if !t.running {
+               return
+       }
+
+       // This switch is mostly for speed.
+       switch name {
+       case "Unmarshal":
+       default:
+               return
+       }
+       idm := tpl.Context.GetDependencyManagerInCurrentScope(ctx)
+       if idm == nil {
+               return
+       }
+
+       for _, arg := range args {
+               identity.WalkIdentitiesShallow(arg.Interface(), func(level int, id identity.Identity) bool {
+                       idm.AddIdentity(id)
+                       return false
+               })
+       }
+}
+
 func (t *templateExecHelper) trackDependencies(ctx context.Context, tmpl texttemplate.Preparer, name string, receiver reflect.Value) context.Context {
        if tmpl == nil {
                panic("must provide a template")