Debian Codegraph @

resources/resource_factories/bundler/bundler.go

raw
1 // Copyright 2019 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // Package bundler contains functions for concatenation etc. of Resource objects.
15 package bundler
16
17 import (
18 "fmt"
19 "io"
20 "path"
21
22 "github.com/gohugoio/hugo/common/hugio"
23 "github.com/gohugoio/hugo/identity"
24 "github.com/gohugoio/hugo/media"
25 "github.com/gohugoio/hugo/resources"
26 "github.com/gohugoio/hugo/resources/resource"
27 )
28
29 // Client contains methods perform concatenation and other bundling related
30 // tasks to Resource objects.
31 type Client struct {
32 rs *resources.Spec
33 }
34
35 // New creates a new Client with the given specification.
36 func New(rs *resources.Spec) *Client {
37 return &Client{rs: rs}
38 }
39
40 type multiReadSeekCloser struct {
41 mr io.Reader
42 sources []hugio.ReadSeekCloser
43 }
44
45 func toReaders(sources []hugio.ReadSeekCloser) []io.Reader {
46 readers := make([]io.Reader, len(sources))
47 for i, r := range sources {
48 readers[i] = r
49 }
50 return readers
51 }
52
53 func newMultiReadSeekCloser(sources ...hugio.ReadSeekCloser) *multiReadSeekCloser {
54 mr := io.MultiReader(toReaders(sources)...)
55 return &multiReadSeekCloser{mr, sources}
56 }
57
58 func (r *multiReadSeekCloser) Read(p []byte) (n int, err error) {
59 return r.mr.Read(p)
60 }
61
62 func (r *multiReadSeekCloser) Seek(offset int64, whence int) (newOffset int64, err error) {
63 for _, s := range r.sources {
64 newOffset, err = s.Seek(offset, whence)
65 if err != nil {
66 return
67 }
68 }
69
70 r.mr = io.MultiReader(toReaders(r.sources)...)
71
72 return
73 }
74
75 func (r *multiReadSeekCloser) Close() error {
76 for _, s := range r.sources {
77 s.Close()
78 }
79 return nil
80 }
81
82 // Concat concatenates the list of Resource objects.
83 func (c *Client) Concat(targetPath string, r resource.Resources) (resource.Resource, error) {
84 targetPath = path.Clean(targetPath)
85 return c.rs.ResourceCache.GetOrCreate(targetPath, func() (resource.Resource, error) {
86 var resolvedm media.Type
87
88 // The given set of resources must be of the same Media Type.
89 // We may improve on that in the future, but then we need to know more.
90 for i, rr := range r {
91 if i > 0 && rr.MediaType().Type != resolvedm.Type {
92 return nil, fmt.Errorf("resources in Concat must be of the same Media Type, got %q and %q", rr.MediaType().Type, resolvedm.Type)
93 }
94 resolvedm = rr.MediaType()
95 }
96
97 idm := c.rs.Cfg.NewIdentityManager()
98
99 // Re-create on structural changes.
100 idm.AddIdentity(identity.StructuralChangeAdd, identity.StructuralChangeRemove)
101
102 // Add the concatenated resources as dependencies to the composite resource
103 // so that we can track changes to the individual resources.
104 idm.AddIdentityForEach(identity.ForEeachIdentityProviderFunc(
105 func(f func(identity.Identity) bool) bool {
106 var terminate bool
107 for _, rr := range r {
108 identity.WalkIdentitiesShallow(rr, func(depth int, id identity.Identity) bool {
109 terminate = f(id)
110 return terminate
111 })
112 if terminate {
113 break
114 }
115 }
116 return terminate
117 },
118 ))
119
120 concatr := func() (hugio.ReadSeekCloser, error) {
121 var rcsources []hugio.ReadSeekCloser
122 for _, s := range r {
123 rcr, ok := s.(resource.ReadSeekCloserResource)
124 if !ok {
125 return nil, fmt.Errorf("resource %T does not implement resource.ReadSeekerCloserResource", s)
126 }
127 rc, err := rcr.ReadSeekCloser()
128 if err != nil {
129 // Close the already opened.
130 for _, rcs := range rcsources {
131 rcs.Close()
132 }
133 return nil, err
134 }
135
136 rcsources = append(rcsources, rc)
137 }
138
139 // Arbitrary JavaScript files require a barrier between them to be safely concatenated together.
140 // Without this, the last line of one file can affect the first line of the next file and change how both files are interpreted.
141 if resolvedm.MainType == media.Builtin.JavascriptType.MainType && resolvedm.SubType == media.Builtin.JavascriptType.SubType {
142 readers := make([]hugio.ReadSeekCloser, 2*len(rcsources)-1)
143 j := 0
144 for i := range rcsources {
145 if i > 0 {
146 readers[j] = hugio.NewReadSeekerNoOpCloserFromString("\n;\n")
147 j++
148 }
149 readers[j] = rcsources[i]
150 j++
151 }
152 return newMultiReadSeekCloser(readers...), nil
153 }
154
155 return newMultiReadSeekCloser(rcsources...), nil
156 }
157
158 composite, err := c.rs.NewResource(
159 resources.ResourceSourceDescriptor{
160 LazyPublish: true,
161 OpenReadSeekCloser: concatr,
162 TargetPath: targetPath,
163 DependencyManager: idm,
164 })
165 if err != nil {
166 return nil, err
167 }
168
169 return composite, nil
170 })
171 }