Fix rebuild with resources.Concat

Fixes #12017
This commit is contained in:
Bjørn Erik Pedersen
2024-02-09 13:52:36 +02:00
parent 21d9057dbf
commit 639073e4fe
18 changed files with 229 additions and 120 deletions

View File

@@ -171,7 +171,7 @@ func (fd *ResourceSourceDescriptor) init(r *Spec) error {
fd.MediaType = mediaType
if fd.DependencyManager == nil {
fd.DependencyManager = identity.NopManager
fd.DependencyManager = r.Cfg.NewIdentityManager("resource")
}
return nil

View File

@@ -39,7 +39,7 @@ func newResourceCache(rs *Spec, memCache *dynacache.Cache) *ResourceCache {
cacheResources: dynacache.GetOrCreatePartition[string, resource.Resources](
memCache,
"/ress",
dynacache.OptionsPartition{ClearWhen: dynacache.ClearOnChange, Weight: 40},
dynacache.OptionsPartition{ClearWhen: dynacache.ClearOnRebuild, Weight: 40},
),
cacheResourceTransformation: dynacache.GetOrCreatePartition[string, *resourceAdapterInner](
memCache,

View File

@@ -20,6 +20,7 @@ import (
"path"
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/resources"
"github.com/gohugoio/hugo/resources/resource"
@@ -86,13 +87,32 @@ func (c *Client) Concat(targetPath string, r resource.Resources) (resource.Resou
// The given set of resources must be of the same Media Type.
// We may improve on that in the future, but then we need to know more.
for i, r := range r {
if i > 0 && r.MediaType().Type != resolvedm.Type {
return nil, fmt.Errorf("resources in Concat must be of the same Media Type, got %q and %q", r.MediaType().Type, resolvedm.Type)
for i, rr := range r {
if i > 0 && rr.MediaType().Type != resolvedm.Type {
return nil, fmt.Errorf("resources in Concat must be of the same Media Type, got %q and %q", rr.MediaType().Type, resolvedm.Type)
}
resolvedm = r.MediaType()
resolvedm = rr.MediaType()
}
idm := c.rs.Cfg.NewIdentityManager("concat")
// Add the concatenated resources as dependencies to the composite resource
// so that we can track changes to the individual resources.
idm.AddIdentityForEach(identity.ForEeachIdentityProviderFunc(
func(f func(identity.Identity) bool) bool {
var terminate bool
for _, rr := range r {
identity.WalkIdentitiesShallow(rr, func(depth int, id identity.Identity) bool {
terminate = f(id)
return terminate
})
if terminate {
break
}
}
return terminate
},
))
concatr := func() (hugio.ReadSeekCloser, error) {
var rcsources []hugio.ReadSeekCloser
for _, s := range r {
@@ -136,6 +156,7 @@ func (c *Client) Concat(targetPath string, r resource.Resources) (resource.Resou
LazyPublish: true,
OpenReadSeekCloser: concatr,
TargetPath: targetPath,
DependencyManager: idm,
})
if err != nil {
return nil, err

View File

@@ -63,13 +63,6 @@ func (c *Client) Copy(r resource.Resource, targetPath string) (resource.Resource
})
}
func (c *Client) newDependencyManager() identity.Manager {
if c.rs.Cfg.Running() {
return identity.NewManager("resources")
}
return identity.NopManager
}
// Get creates a new Resource by opening the given pathname in the assets filesystem.
func (c *Client) Get(pathname string) (resource.Resource, error) {
pathname = path.Clean(pathname)
@@ -79,7 +72,8 @@ func (c *Client) Get(pathname string) (resource.Resource, error) {
// The resource file will not be read before it gets used (e.g. in .Content),
// so we need to check that the file exists here.
filename := filepath.FromSlash(pathname)
if _, err := c.rs.BaseFs.Assets.Fs.Stat(filename); err != nil {
fi, err := c.rs.BaseFs.Assets.Fs.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
@@ -87,14 +81,16 @@ func (c *Client) Get(pathname string) (resource.Resource, error) {
return nil, err
}
pi := fi.(hugofs.FileMetaInfo).Meta().PathInfo
return c.rs.NewResource(resources.ResourceSourceDescriptor{
LazyPublish: true,
OpenReadSeekCloser: func() (hugio.ReadSeekCloser, error) {
return c.rs.BaseFs.Assets.Fs.Open(filename)
},
GroupIdentity: identity.StringIdentity(key),
DependencyManager: c.newDependencyManager(),
TargetPath: pathname,
Path: pi,
GroupIdentity: pi,
TargetPath: pathname,
})
})
}