github.com/neohugo/neohugo@v0.123.8/resources/resource_factories/bundler/bundler.go (about)

     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/neohugo/neohugo/common/hugio"
    23  	"github.com/neohugo/neohugo/identity"
    24  	"github.com/neohugo/neohugo/media"
    25  	"github.com/neohugo/neohugo/resources"
    26  	"github.com/neohugo/neohugo/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("concat")
    98  		// Add the concatenated resources as dependencies to the composite resource
    99  		// so that we can track changes to the individual resources.
   100  		idm.AddIdentityForEach(identity.ForEeachIdentityProviderFunc(
   101  			func(f func(identity.Identity) bool) bool {
   102  				var terminate bool
   103  				for _, rr := range r {
   104  					identity.WalkIdentitiesShallow(rr, func(depth int, id identity.Identity) bool {
   105  						terminate = f(id)
   106  						return terminate
   107  					})
   108  					if terminate {
   109  						break
   110  					}
   111  				}
   112  				return terminate
   113  			},
   114  		))
   115  
   116  		concatr := func() (hugio.ReadSeekCloser, error) {
   117  			var rcsources []hugio.ReadSeekCloser
   118  			for _, s := range r {
   119  				rcr, ok := s.(resource.ReadSeekCloserResource)
   120  				if !ok {
   121  					return nil, fmt.Errorf("resource %T does not implement resource.ReadSeekerCloserResource", s)
   122  				}
   123  				rc, err := rcr.ReadSeekCloser()
   124  				if err != nil {
   125  					// Close the already opened.
   126  					for _, rcs := range rcsources {
   127  						rcs.Close()
   128  					}
   129  					return nil, err
   130  				}
   131  
   132  				rcsources = append(rcsources, rc)
   133  			}
   134  
   135  			// Arbitrary JavaScript files require a barrier between them to be safely concatenated together.
   136  			// Without this, the last line of one file can affect the first line of the next file and change how both files are interpreted.
   137  			if resolvedm.MainType == media.Builtin.JavascriptType.MainType && resolvedm.SubType == media.Builtin.JavascriptType.SubType {
   138  				readers := make([]hugio.ReadSeekCloser, 2*len(rcsources)-1)
   139  				j := 0
   140  				for i := 0; i < len(rcsources); i++ {
   141  					if i > 0 {
   142  						readers[j] = hugio.NewReadSeekerNoOpCloserFromString("\n;\n")
   143  						j++
   144  					}
   145  					readers[j] = rcsources[i]
   146  					j++
   147  				}
   148  				return newMultiReadSeekCloser(readers...), nil
   149  			}
   150  
   151  			return newMultiReadSeekCloser(rcsources...), nil
   152  		}
   153  
   154  		composite, err := c.rs.NewResource(
   155  			resources.ResourceSourceDescriptor{
   156  				LazyPublish:        true,
   157  				OpenReadSeekCloser: concatr,
   158  				TargetPath:         targetPath,
   159  				DependencyManager:  idm,
   160  			})
   161  		if err != nil {
   162  			return nil, err
   163  		}
   164  
   165  		return composite, nil
   166  	})
   167  }