From c0c940156f13bf3151fe193e4d3a9433721cf056 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 19 Feb 2024 22:20:10 -0800 Subject: [PATCH] Add contextFile --- context.go | 22 ++++++++---- context_file.go | 79 ++++++++++++++++++++++++++++++++++++++++++ filter_util.go | 4 ++- interface.go | 1 - file.go => raw_file.go | 6 ---- 5 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 context_file.go rename file.go => raw_file.go (97%) diff --git a/context.go b/context.go index 75b7981..8945157 100644 --- a/context.go +++ b/context.go @@ -35,16 +35,20 @@ func (self *Context) CreateFileFromReader(sourcePath string, reader io.Reader) ( return nil, err } - file := &rawFile{ + rf := &rawFile{ relPath: sourcePath, props: make(map[string]FileProp), modTime: time.Now(), size: int64(len(data)), reader: bytes.NewReader(data), - index: self.index, } - return file, nil + cf := &contextFile{ + file: rf, + index: self.index, + } + + return cf, nil } // CreateFileFromAsset creates a new file instance from the provided file path. @@ -65,16 +69,20 @@ func (self *Context) CreateFileFromAsset(sourcePath, dataPath string) (File, err return nil, errors.New("assets must be files") } - file := &rawFile{ + rf := &rawFile{ relPath: sourcePath, props: make(map[string]FileProp), modTime: info.ModTime(), size: info.Size(), dataPath: dataPath, - index: self.index, } - return file, nil + cf := &contextFile{ + file: rf, + index: self.index, + } + + return cf, nil } // DispatchFile causes the file to get passed to the next link in the chain. @@ -90,7 +98,7 @@ func (self *Context) DispatchAndCacheFile(outputFile File, inputFiles ...File) { self.goldsmith.cache.storeFile(self, outputFile, inputFiles) } - self.filesOut <- outputFile + self.DispatchFile(outputFile) } // RetrieveCachedFile looks up file data (excluding the metadata), given an diff --git a/context_file.go b/context_file.go new file mode 100644 index 0000000..d477df9 --- /dev/null +++ b/context_file.go @@ -0,0 +1,79 @@ +package goldsmith + +import ( + "io" + "time" +) + +type contextFile struct { + file File + index int +} + +func (self *contextFile) Path() string { + return self.file.Path() +} + +func (self *contextFile) Dir() string { + return self.file.Dir() +} + +func (self *contextFile) Name() string { + return self.file.Name() +} + +func (self *contextFile) Ext() string { + return self.file.Ext() +} + +func (self *contextFile) Rename(path string) error { + return self.file.Rename(path) +} + +func (self *contextFile) Size() int64 { + return self.file.Size() +} + +func (self *contextFile) ModTime() time.Time { + return self.file.ModTime() +} + +func (self *contextFile) Read(data []byte) (int, error) { + return self.file.Read(data) +} + +func (self *contextFile) WriteTo(writer io.Writer) (int64, error) { + return self.file.WriteTo(writer) +} + +func (self *contextFile) Seek(offset int64, whence int) (int64, error) { + return self.file.Seek(offset, whence) +} + +func (self *contextFile) GoString() string { + return self.file.GoString() +} + +func (self *contextFile) SetProp(name string, value FileProp) { + self.file.SetProp(name, value) +} + +func (self *contextFile) Prop(name string) (FileProp, bool) { + return self.file.Prop(name) +} + +func (self *contextFile) PropOrDef(name string, valueDef FileProp) FileProp { + return self.file.PropOrDef(name, valueDef) +} + +func (self *contextFile) Props() FileProps { + return self.file.Props() +} + +func (self *contextFile) CopyProps(file File) { + self.file.CopyProps(file) +} + +func (self *contextFile) RemoveProp(name string) { + self.file.RemoveProp(name) +} diff --git a/filter_util.go b/filter_util.go index a1a659c..15b63c4 100644 --- a/filter_util.go +++ b/filter_util.go @@ -8,8 +8,10 @@ type filterEntry struct { type filterStack []filterEntry func (self *filterStack) accept(file File) bool { + cf := file.(*contextFile) + for _, entry := range *self { - if entry.index >= file.Index() && !entry.filter.Accept(file) { + if entry.index >= cf.index && !entry.filter.Accept(file) { return false } } diff --git a/interface.go b/interface.go index d1d2f0d..bd8de15 100644 --- a/interface.go +++ b/interface.go @@ -62,5 +62,4 @@ type File interface { RemoveProp(name string) GoString() string - Index() int } diff --git a/file.go b/raw_file.go similarity index 97% rename from file.go rename to raw_file.go index 6b5c2e3..cdda095 100644 --- a/file.go +++ b/raw_file.go @@ -17,8 +17,6 @@ type rawFile struct { dataPath string reader *bytes.Reader - - index int } func (self *rawFile) Path() string { @@ -117,10 +115,6 @@ func (self *rawFile) RemoveProp(name string) { delete(self.props, name) } -func (self *rawFile) Index() int { - return self.index -} - func (self *rawFile) load() error { if self.reader != nil { return nil