Add contextFile

This commit is contained in:
Alex Yatskov 2024-02-19 22:20:10 -08:00
parent 2f95fdba2d
commit c0c940156f
5 changed files with 97 additions and 15 deletions

View File

@ -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),
}
cf := &contextFile{
file: rf,
index: self.index,
}
return file, nil
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,
}
cf := &contextFile{
file: rf,
index: self.index,
}
return file, nil
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

79
context_file.go Normal file
View File

@ -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)
}

View File

@ -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
}
}

View File

@ -62,5 +62,4 @@ type File interface {
RemoveProp(name string)
GoString() string
Index() int
}

View File

@ -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