This commit is contained in:
Alex Yatskov 2024-02-19 10:39:05 -08:00
parent ea28b5c8cf
commit 25d629af10
2 changed files with 62 additions and 64 deletions

106
file.go
View File

@ -2,14 +2,17 @@ package goldsmith
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
) )
type Prop interface{} type (
type PropMap map[string]Prop Prop any
PropMap map[string]Prop
)
// File represents in-memory or on-disk files in a chain. // File represents in-memory or on-disk files in a chain.
type File struct { type File struct {
@ -25,19 +28,12 @@ type File struct {
} }
// Rename modifies the file path relative to the source directory. // Rename modifies the file path relative to the source directory.
func (self *File) Rename(path string) { func (self *File) Rename(path string) error {
if filepath.IsAbs(path) {
return fmt.Errorf("unexpected absolute path: %s", path)
}
self.relPath = path self.relPath = path
}
func (self *File) Rewrite(reader io.Reader) error {
data, err := io.ReadAll(reader)
if err != nil {
return err
}
self.reader = bytes.NewReader(data)
self.modTime = time.Now()
self.size = int64(len(data))
return nil return nil
} }
@ -46,16 +42,16 @@ func (self *File) Path() string {
return filepath.ToSlash(self.relPath) return filepath.ToSlash(self.relPath)
} }
// Name returns the base name of the file.
func (self *File) Name() string {
return filepath.Base(self.relPath)
}
// Dir returns the containing directory of the file. // Dir returns the containing directory of the file.
func (self *File) Dir() string { func (self *File) Dir() string {
return filepath.ToSlash(filepath.Dir(self.relPath)) return filepath.ToSlash(filepath.Dir(self.relPath))
} }
// Name returns the base name of the file.
func (self *File) Name() string {
return filepath.Base(self.relPath)
}
// Ext returns the extension of the file. // Ext returns the extension of the file.
func (self *File) Ext() string { func (self *File) Ext() string {
return filepath.Ext(self.relPath) return filepath.Ext(self.relPath)
@ -102,36 +98,60 @@ func (self *File) Seek(offset int64, whence int) (int64, error) {
return self.reader.Seek(offset, whence) return self.reader.Seek(offset, whence)
} }
// Returns value for string formatting. // GoString returns value for string formatting.
func (self *File) GoString() string { func (self *File) GoString() string {
return self.relPath return self.relPath
} }
// RemoveProp deletes the metadata property for the provided name.
func (self *File) RemoveProp(name string) {
delete(self.props, name)
}
// SetProp updates the metadata property for the provided name.
func (self *File) SetProp(name string, value Prop) { func (self *File) SetProp(name string, value Prop) {
self.props[name] = value self.props[name] = value
} }
// Prop returns the metadata property for the provided name.
func (self *File) Prop(name string) (Prop, bool) {
value, ok := self.props[name]
return value, ok
}
// PropOrDef returns the metadata property for the provided name or the default.
func (self *File) PropOrDef(name string, valueDef Prop) Prop {
if value, ok := self.Prop(name); ok {
return value
}
return valueDef
}
// Props returns all of the metadata properties.
func (self *File) Props() PropMap {
return self.props
}
// CopyProps copies all metadata properties from the provided file.
func (self *File) CopyProps(file *File) { func (self *File) CopyProps(file *File) {
for key, value := range file.props { for key, value := range file.props {
self.props[key] = value self.props[key] = value
} }
} }
func (self *File) Prop(name string) (Prop, bool) { func (self *File) load() error {
value, ok := self.props[name] if self.reader != nil {
return value, ok return nil
} }
func (self *File) Props() PropMap { data, err := os.ReadFile(self.dataPath)
return self.props if err != nil {
return err
} }
func (self *File) PropOrDefault(name string, valueDef Prop) Prop { self.reader = bytes.NewReader(data)
if value, ok := self.Prop(name); ok { return nil
return value
}
return valueDef
} }
func (self *File) export(targetDir string) error { func (self *File) export(targetDir string) error {
@ -151,17 +171,10 @@ func (self *File) export(targetDir string) error {
} }
defer fw.Close() defer fw.Close()
if self.reader == nil { if err := self.load(); err != nil {
fr, err := os.Open(self.dataPath)
if err != nil {
return err return err
} }
defer fr.Close()
if _, err := io.Copy(fw, fr); err != nil {
return err
}
} else {
if _, err := self.Seek(0, io.SeekStart); err != nil { if _, err := self.Seek(0, io.SeekStart); err != nil {
return err return err
} }
@ -169,21 +182,6 @@ func (self *File) export(targetDir string) error {
if _, err := self.WriteTo(fw); err != nil { if _, err := self.WriteTo(fw); err != nil {
return err return err
} }
}
return nil return nil
} }
func (self *File) load() error {
if self.reader != nil {
return nil
}
data, err := os.ReadFile(self.dataPath)
if err != nil {
return err
}
self.reader = bytes.NewReader(data)
return nil
}

View File

@ -21,7 +21,7 @@ func (self *fileImporter) Initialize(context *Context) error {
relPath, err := filepath.Rel(self.sourceDir, path) relPath, err := filepath.Rel(self.sourceDir, path)
if err != nil { if err != nil {
return err panic(err)
} }
file, err := context.CreateFileFromAsset(relPath, path) file, err := context.CreateFileFromAsset(relPath, path)