Squashed commit of the following:

commit 106991bfd59bb95977045399f7b4e5f4e7addc56
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sat Jan 8 11:19:21 2022 -0800

    Rename property functions

commit 07f6033d4e86df257806af16de002ce8fad3d67f
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sat Jan 8 11:16:46 2022 -0800

    Update rewrite

commit ea2dc0b4d223d54832752f0efd3eb46d81d17b50
Author: Alex Yatskov <alex@foosoft.net>
Date:   Fri Jan 7 22:47:17 2022 -0800

    Add more property methods

commit e2f7f8dc7ebc5b668539e8233e323a7c256eced2
Author: Alex Yatskov <alex@foosoft.net>
Date:   Fri Jan 7 22:39:23 2022 -0800

    Add step indexing

commit a7ed2a0b1c95ed9fbb0bbd87c779cf51a92f0b5f
Author: Alex Yatskov <alex@foosoft.net>
Date:   Fri Jan 7 20:50:19 2022 -0800

    Use self

commit 7ecc01e508c06680dcb6b6ecd4265a7f72e2c933
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sun Aug 22 12:33:09 2021 -0700

    Cleanup

commit 87dd28a4c14b88bea061ca913f68e272cca787f9
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sun Aug 22 12:09:41 2021 -0700

    Cleanup

commit 129130128d2a50f119c46f70e7cb70ef421892ff
Merge: 629fce0 e751d70
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sat Aug 21 12:18:09 2021 -0700

    Merge branch 'master' into dev

commit 629fce06a8fca6810ec772f558d9ffabda016d01
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sun Jun 27 19:34:53 2021 -0700

    Abstract metadata
This commit is contained in:
Alex Yatskov 2022-01-08 18:08:36 -08:00
parent e751d70f27
commit e408fa9335
8 changed files with 296 additions and 279 deletions

View File

@ -13,8 +13,8 @@ type cache struct {
baseDir string baseDir string
} }
func (cache *cache) retrieveFile(context *Context, outputPath string, inputFiles []*File) (*File, error) { func (self *cache) retrieveFile(context *Context, outputPath string, inputFiles []*File) (*File, error) {
cachePath, err := cache.buildCachePath(context, outputPath, inputFiles) cachePath, err := self.buildCachePath(context, outputPath, inputFiles)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -31,13 +31,13 @@ func (cache *cache) retrieveFile(context *Context, outputPath string, inputFiles
return outputFile, nil return outputFile, nil
} }
func (cache *cache) storeFile(context *Context, outputFile *File, inputFiles []*File) error { func (self *cache) storeFile(context *Context, outputFile *File, inputFiles []*File) error {
cachePath, err := cache.buildCachePath(context, outputFile.Path(), inputFiles) cachePath, err := self.buildCachePath(context, outputFile.Path(), inputFiles)
if err != nil { if err != nil {
return err return err
} }
if err := os.MkdirAll(cache.baseDir, 0755); err != nil { if err := os.MkdirAll(self.baseDir, 0755); err != nil {
return err return err
} }
@ -67,28 +67,20 @@ func (cache *cache) storeFile(context *Context, outputFile *File, inputFiles []*
return nil return nil
} }
func (cache *cache) buildCachePath(context *Context, outputPath string, inputFiles []*File) (string, error) { func (self *cache) buildCachePath(context *Context, outputPath string, inputFiles []*File) (string, error) {
uintBuff := make([]byte, 4)
binary.LittleEndian.PutUint32(uintBuff, context.chainHash)
hasher := crc32.NewIEEE() hasher := crc32.NewIEEE()
hasher.Write(uintBuff)
hasher.Write([]byte(outputPath)) hasher.Write([]byte(outputPath))
sort.Sort(filesByPath(inputFiles)) sort.Sort(filesByPath(inputFiles))
for _, inputFile := range inputFiles { for _, inputFile := range inputFiles {
fileHash, err := inputFile.hash() modTimeBuff := make([]byte, 8)
if err != nil { binary.LittleEndian.PutUint64(modTimeBuff, uint64(inputFile.ModTime().UnixNano()))
return "", err
}
binary.LittleEndian.PutUint32(uintBuff, fileHash)
hasher.Write(uintBuff)
hasher.Write([]byte(inputFile.Path())) hasher.Write([]byte(inputFile.Path()))
hasher.Write(modTimeBuff)
} }
cachePath := filepath.Join(cache.baseDir, fmt.Sprintf( cachePath := filepath.Join(self.baseDir, fmt.Sprintf(
"gs_%.8x%s", "gs_%.8x%s",
hasher.Sum32(), hasher.Sum32(),
filepath.Ext(outputPath), filepath.Ext(outputPath),

View File

@ -3,6 +3,8 @@ package goldsmith
import ( import (
"bytes" "bytes"
"errors" "errors"
"io"
"io/ioutil"
"os" "os"
"runtime" "runtime"
"sync" "sync"
@ -15,30 +17,38 @@ type Context struct {
goldsmith *Goldsmith goldsmith *Goldsmith
plugin Plugin plugin Plugin
chainHash uint32
filtersExt filterStack filtersExt filterStack
filtersInt filterStack filtersInt filterStack
threads int threads int
index int
filesIn chan *File filesIn chan *File
filesOut chan *File filesOut chan *File
} }
// CreateFileFrom data creates a new file instance from the provided data buffer. // CreateFileFrom data creates a new file instance from the provided data buffer.
func (*Context) CreateFileFromData(sourcePath string, data []byte) *File { func (self *Context) CreateFileFromReader(sourcePath string, reader io.Reader) (*File, error) {
return &File{ data, err := ioutil.ReadAll(reader)
sourcePath: sourcePath, if err != nil {
Meta: make(map[string]interface{}), return nil, err
reader: bytes.NewReader(data),
size: int64(len(data)),
modTime: time.Now(),
} }
file := &File{
relPath: sourcePath,
props: make(map[string]Prop),
modTime: time.Now(),
size: int64(len(data)),
reader: bytes.NewReader(data),
index: self.index,
}
return file, nil
} }
// CreateFileFromAsset creates a new file instance from the provided file path. // CreateFileFromAsset creates a new file instance from the provided file path.
func (*Context) CreateFileFromAsset(sourcePath, dataPath string) (*File, error) { func (self *Context) CreateFileFromAsset(sourcePath, dataPath string) (*File, error) {
info, err := os.Stat(dataPath) info, err := os.Stat(dataPath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -48,70 +58,74 @@ func (*Context) CreateFileFromAsset(sourcePath, dataPath string) (*File, error)
} }
file := &File{ file := &File{
sourcePath: sourcePath, relPath: sourcePath,
dataPath: dataPath, props: make(map[string]Prop),
Meta: make(map[string]interface{}),
size: info.Size(),
modTime: info.ModTime(), modTime: info.ModTime(),
size: info.Size(),
dataPath: dataPath,
index: self.index,
} }
return file, nil return file, nil
} }
// DispatchFile causes the file to get passed to the next link in the chain. // DispatchFile causes the file to get passed to the next link in the chain.
func (context *Context) DispatchFile(file *File) { func (self *Context) DispatchFile(file *File) {
context.filesOut <- file self.filesOut <- file
} }
// DispatchAndCacheFile caches the file data (excluding the metadata), taking // DispatchAndCacheFile caches the file data (excluding the metadata), taking
// dependencies on any input files that are needed to generate it, and then // dependencies on any input files that are needed to generate it, and then
// passes it to the next link in the chain. // passes it to the next link in the chain.
func (context *Context) DispatchAndCacheFile(outputFile *File, inputFiles ...*File) { func (self *Context) DispatchAndCacheFile(outputFile *File, inputFiles ...*File) {
if context.goldsmith.cache != nil { if self.goldsmith.cache != nil {
context.goldsmith.cache.storeFile(context, outputFile, inputFiles) self.goldsmith.cache.storeFile(self, outputFile, inputFiles)
} }
context.filesOut <- outputFile self.filesOut <- outputFile
} }
// RetrieveCachedFile looks up file data (excluding the metadata), given an // RetrieveCachedFile looks up file data (excluding the metadata), given an
// output path and any input files that are needed to generate it. The function // output path and any input files that are needed to generate it. The function
// will return nil if the desired file is not found in the cache. // will return nil if the desired file is not found in the cache.
func (context *Context) RetrieveCachedFile(outputPath string, inputFiles ...*File) *File { func (self *Context) RetrieveCachedFile(outputPath string, inputFiles ...*File) *File {
var outputFile *File var outputFile *File
if context.goldsmith.cache != nil { if self.goldsmith.cache != nil {
outputFile, _ = context.goldsmith.cache.retrieveFile(context, outputPath, inputFiles) outputFile, _ = self.goldsmith.cache.retrieveFile(self, outputPath, inputFiles)
} }
return outputFile return outputFile
} }
// Specify internal filter(s) that exclude files from being processed. // Specify internal filter(s) that exclude files from being processed.
func (context *Context) Filter(filters ...Filter) *Context { func (self *Context) Filter(filters ...Filter) *Context {
context.filtersInt = filters for _, filter := range filters {
return context self.filtersInt.push(filter, self.index)
}
return self
} }
// Specify the maximum number of threads used for processing. // Specify the maximum number of threads used for processing.
func (context *Context) Threads(threads int) *Context { func (self *Context) Threads(threads int) *Context {
context.threads = threads self.threads = threads
return context return self
} }
func (context *Context) step() { func (self *Context) step() {
defer close(context.filesOut) defer close(self.filesOut)
if initializer, ok := context.plugin.(Initializer); ok { if initializer, ok := self.plugin.(Initializer); ok {
if err := initializer.Initialize(context); err != nil { if err := initializer.Initialize(self); err != nil {
context.goldsmith.fault(context.plugin.Name(), nil, err) self.goldsmith.fault(self.plugin.Name(), nil, err)
return return
} }
} }
if context.filesIn != nil { if self.filesIn != nil {
processor, _ := context.plugin.(Processor) processor, _ := self.plugin.(Processor)
threads := context.threads threads := self.threads
if threads < 1 { if threads < 1 {
threads = runtime.NumCPU() threads = runtime.NumCPU()
} }
@ -121,26 +135,27 @@ func (context *Context) step() {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
for inputFile := range context.filesIn { for inputFile := range self.filesIn {
if processor != nil && context.filtersInt.accept(inputFile) && context.filtersExt.accept(inputFile) { if processor != nil && self.filtersInt.accept(inputFile) && self.filtersExt.accept(inputFile) {
if _, err := inputFile.Seek(0, os.SEEK_SET); err != nil { if _, err := inputFile.Seek(0, os.SEEK_SET); err != nil {
context.goldsmith.fault("core", inputFile, err) self.goldsmith.fault("core", inputFile, err)
} }
if err := processor.Process(context, inputFile); err != nil { if err := processor.Process(self, inputFile); err != nil {
context.goldsmith.fault(context.plugin.Name(), inputFile, err) self.goldsmith.fault(self.plugin.Name(), inputFile, err)
} }
} else { } else {
context.filesOut <- inputFile self.filesOut <- inputFile
} }
} }
}() }()
} }
wg.Wait() wg.Wait()
} }
if finalizer, ok := context.plugin.(Finalizer); ok { if finalizer, ok := self.plugin.(Finalizer); ok {
if err := finalizer.Finalize(context); err != nil { if err := finalizer.Finalize(self); err != nil {
context.goldsmith.fault(context.plugin.Name(), nil, err) self.goldsmith.fault(self.plugin.Name(), nil, err)
} }
} }
} }

235
file.go
View File

@ -2,106 +2,144 @@ package goldsmith
import ( import (
"bytes" "bytes"
"hash/crc32"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"time" "time"
) )
type Prop interface{}
type 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 {
sourcePath string relPath string
dataPath string props map[string]Prop
Meta map[string]interface{}
hashValue uint32
hashValid bool
reader *bytes.Reader
size int64
modTime time.Time modTime time.Time
size int64
dataPath string
reader *bytes.Reader
index int
} }
// Rename modifies the file path relative to the source directory. // Rename modifies the file path relative to the source directory.
func (file *File) Rename(path string) { func (self *File) Rename(path string) {
file.sourcePath = path self.relPath = path
}
func (self *File) Rewrite(reader io.Reader) error {
data, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
self.reader = bytes.NewReader(data)
self.modTime = time.Now()
self.size = int64(len(data))
return nil
} }
// Path returns the file path relative to the source directory. // Path returns the file path relative to the source directory.
func (file *File) Path() string { func (self *File) Path() string {
return file.sourcePath return self.relPath
} }
// Name returns the base name of the file. // Name returns the base name of the file.
func (file *File) Name() string { func (self *File) Name() string {
return path.Base(file.sourcePath) return path.Base(self.relPath)
} }
// Dir returns the containing directory of the file. // Dir returns the containing directory of the file.
func (file *File) Dir() string { func (self *File) Dir() string {
return path.Dir(file.sourcePath) return path.Dir(self.relPath)
} }
// Ext returns the extension of the file. // Ext returns the extension of the file.
func (file *File) Ext() string { func (self *File) Ext() string {
return path.Ext(file.sourcePath) return path.Ext(self.relPath)
} }
// Size returns the file length in bytes. // Size returns the file length in bytes.
func (file *File) Size() int64 { func (self *File) Size() int64 {
return file.size return self.size
} }
// ModTime returns the time of the file's last modification. // ModTime returns the time of the file's last modification.
func (file *File) ModTime() time.Time { func (self *File) ModTime() time.Time {
return file.modTime return self.modTime
} }
// Read reads file data into the provided buffer. // Read reads file data into the provided buffer.
func (file *File) Read(data []byte) (int, error) { func (self *File) Read(data []byte) (int, error) {
if err := file.load(); err != nil { if err := self.load(); err != nil {
return 0, err return 0, err
} }
return file.reader.Read(data) return self.reader.Read(data)
} }
// Write writes file data into the provided writer. // Write writes file data into the provided writer.
func (file *File) WriteTo(writer io.Writer) (int64, error) { func (self *File) WriteTo(writer io.Writer) (int64, error) {
if err := file.load(); err != nil { if err := self.load(); err != nil {
return 0, err return 0, err
} }
return file.reader.WriteTo(writer) return self.reader.WriteTo(writer)
} }
// Seek updates the file pointer to the desired position. // Seek updates the file pointer to the desired position.
func (file *File) Seek(offset int64, whence int) (int64, error) { func (self *File) Seek(offset int64, whence int) (int64, error) {
if file.reader == nil && offset == 0 && (whence == os.SEEK_SET || whence == os.SEEK_CUR) { if self.reader == nil && offset == 0 && (whence == os.SEEK_SET || whence == os.SEEK_CUR) {
return 0, nil return 0, nil
} }
if err := file.load(); err != nil { if err := self.load(); err != nil {
return 0, err return 0, err
} }
return file.reader.Seek(offset, whence) return self.reader.Seek(offset, whence)
} }
// Returns value for string formatting. // Returns value for string formatting.
func (file *File) GoString() string { func (self *File) GoString() string {
return file.sourcePath return self.relPath
} }
func (file *File) export(targetDir string) error { func (self *File) SetProp(name string, value Prop) {
targetPath := filepath.Join(targetDir, file.sourcePath) self.props[name] = value
}
if targetInfo, err := os.Stat(targetPath); err == nil && !targetInfo.ModTime().Before(file.ModTime()) { func (self *File) CopyProps(file *File) {
for key, value := range file.props {
self.props[key] = value
}
}
func (self *File) Prop(name string) (Prop, bool) {
value, ok := self.props[name]
return value, ok
}
func (self *File) Props() PropMap {
return self.props
}
func (self *File) PropOrDefault(name string, valueDef Prop) Prop {
if value, ok := self.Prop(name); ok {
return value
}
return valueDef
}
func (self *File) export(targetDir string) error {
targetPath := filepath.Join(targetDir, self.relPath)
if targetInfo, err := os.Stat(targetPath); err == nil && !targetInfo.ModTime().Before(self.ModTime()) {
return nil return nil
} }
@ -115,8 +153,8 @@ func (file *File) export(targetDir string) error {
} }
defer fw.Close() defer fw.Close()
if file.reader == nil { if self.reader == nil {
fr, err := os.Open(file.dataPath) fr, err := os.Open(self.dataPath)
if err != nil { if err != nil {
return err return err
} }
@ -126,11 +164,11 @@ func (file *File) export(targetDir string) error {
return err return err
} }
} else { } else {
if _, err := file.Seek(0, os.SEEK_SET); err != nil { if _, err := self.Seek(0, os.SEEK_SET); err != nil {
return err return err
} }
if _, err := file.WriteTo(fw); err != nil { if _, err := self.WriteTo(fw); err != nil {
return err return err
} }
} }
@ -138,115 +176,16 @@ func (file *File) export(targetDir string) error {
return nil return nil
} }
func (file *File) load() error { func (self *File) load() error {
if file.reader != nil { if self.reader != nil {
return nil return nil
} }
data, err := ioutil.ReadFile(file.dataPath) data, err := ioutil.ReadFile(self.dataPath)
if err != nil { if err != nil {
return err return err
} }
file.reader = bytes.NewReader(data) self.reader = bytes.NewReader(data)
return nil return nil
} }
func (file *File) hash() (uint32, error) {
if file.hashValid {
return file.hashValue, nil
}
if err := file.load(); err != nil {
return 0, err
}
offset, err := file.Seek(0, os.SEEK_CUR)
if err != nil {
return 0, err
}
if _, err := file.Seek(0, os.SEEK_SET); err != nil {
return 0, err
}
hasher := crc32.NewIEEE()
if _, err := io.Copy(hasher, file.reader); err != nil {
return 0, err
}
if _, err := file.Seek(offset, os.SEEK_SET); err != nil {
return 0, err
}
file.hashValue = hasher.Sum32()
file.hashValid = true
return file.hashValue, nil
}
type filesByPath []*File
func (file filesByPath) Len() int {
return len(file)
}
func (file filesByPath) Swap(i, j int) {
file[i], file[j] = file[j], file[i]
}
func (file filesByPath) Less(i, j int) bool {
return strings.Compare(file[i].Path(), file[j].Path()) < 0
}
type fileInfo struct {
os.FileInfo
path string
}
func cleanPath(path string) string {
if filepath.IsAbs(path) {
var err error
if path, err = filepath.Rel("/", path); err != nil {
panic(err)
}
}
return filepath.Clean(path)
}
func scanDir(rootDir string, infos chan fileInfo) {
defer close(infos)
filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err == nil {
infos <- fileInfo{FileInfo: info, path: path}
}
return err
})
}
type filterStack []Filter
func (filters *filterStack) accept(file *File) bool {
for _, filter := range *filters {
if !filter.Accept(file) {
return false
}
}
return true
}
func (filters *filterStack) push(filter Filter) {
*filters = append(*filters, filter)
}
func (filters *filterStack) pop() {
count := len(*filters)
if count == 0 {
panic("attempted to pop empty filter stack")
}
*filters = (*filters)[:count-1]
}

49
file_util.go Normal file
View File

@ -0,0 +1,49 @@
package goldsmith
import (
"os"
"path/filepath"
"strings"
)
type filesByPath []*File
func (self filesByPath) Len() int {
return len(self)
}
func (self filesByPath) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
func (self filesByPath) Less(i, j int) bool {
return strings.Compare(self[i].Path(), self[j].Path()) < 0
}
type fileInfo struct {
os.FileInfo
path string
}
func cleanPath(path string) string {
if filepath.IsAbs(path) {
var err error
if path, err = filepath.Rel("/", path); err != nil {
panic(err)
}
}
return filepath.Clean(path)
}
func scanDir(rootDir string, infos chan fileInfo) {
defer close(infos)
filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err == nil {
infos <- fileInfo{FileInfo: info, path: path}
}
return err
})
}

31
filter_util.go Normal file
View File

@ -0,0 +1,31 @@
package goldsmith
type filterEntry struct {
filter Filter
index int
}
type filterStack []filterEntry
func (self *filterStack) accept(file *File) bool {
for _, entry := range *self {
if entry.index >= file.index && !entry.filter.Accept(file) {
return false
}
}
return true
}
func (self *filterStack) push(filter Filter, index int) {
*self = append(*self, filterEntry{filter, index})
}
func (self *filterStack) pop() {
count := len(*self)
if count == 0 {
panic("attempted to pop empty filter stack")
}
*self = (*self)[:count-1]
}

View File

@ -3,8 +3,6 @@ package goldsmith
import ( import (
"fmt" "fmt"
"hash"
"hash/crc32"
"sync" "sync"
) )
@ -14,11 +12,11 @@ type Goldsmith struct {
targetDir string targetDir string
contexts []*Context contexts []*Context
contextHasher hash.Hash32
cache *cache cache *cache
filters filterStack filters filterStack
clean bool clean bool
index int
errors []error errors []error
mutex sync.Mutex mutex sync.Mutex
@ -26,80 +24,77 @@ type Goldsmith struct {
// Begin starts a chain, reading the files located in the source directory as input. // Begin starts a chain, reading the files located in the source directory as input.
func Begin(sourceDir string) *Goldsmith { func Begin(sourceDir string) *Goldsmith {
goldsmith := &Goldsmith{ goldsmith := &Goldsmith{sourceDir: sourceDir}
sourceDir: sourceDir,
contextHasher: crc32.NewIEEE(),
}
goldsmith.Chain(&loader{}) goldsmith.Chain(&loader{})
return goldsmith return goldsmith
} }
// Cache enables caching in cacheDir for the remainder of the chain. // Cache enables caching in cacheDir for the remainder of the chain.
func (goldsmith *Goldsmith) Cache(cacheDir string) *Goldsmith { func (self *Goldsmith) Cache(cacheDir string) *Goldsmith {
goldsmith.cache = &cache{cacheDir} self.cache = &cache{cacheDir}
return goldsmith return self
} }
// Clean enables or disables removal of leftover files in the target directory. // Clean enables or disables removal of leftover files in the target directory.
func (goldsmith *Goldsmith) Clean(clean bool) *Goldsmith { func (self *Goldsmith) Clean(clean bool) *Goldsmith {
goldsmith.clean = clean self.clean = clean
return goldsmith return self
} }
// Chain links a plugin instance into the chain. // Chain links a plugin instance into the chain.
func (goldsmith *Goldsmith) Chain(plugin Plugin) *Goldsmith { func (self *Goldsmith) Chain(plugin Plugin) *Goldsmith {
goldsmith.contextHasher.Write([]byte(plugin.Name()))
context := &Context{ context := &Context{
goldsmith: goldsmith, goldsmith: self,
plugin: plugin, plugin: plugin,
chainHash: goldsmith.contextHasher.Sum32(), filtersExt: append(filterStack(nil), self.filters...),
index: self.index,
filesOut: make(chan *File), filesOut: make(chan *File),
} }
context.filtersExt = append(context.filtersExt, goldsmith.filters...) if len(self.contexts) > 0 {
context.filesIn = self.contexts[len(self.contexts)-1].filesOut
if len(goldsmith.contexts) > 0 {
context.filesIn = goldsmith.contexts[len(goldsmith.contexts)-1].filesOut
} }
goldsmith.contexts = append(goldsmith.contexts, context) self.contexts = append(self.contexts, context)
return goldsmith self.index++
return self
} }
// FilterPush pushes a filter instance on the chain's filter stack. // FilterPush pushes a filter instance on the chain's filter stack.
func (goldsmith *Goldsmith) FilterPush(filter Filter) *Goldsmith { func (self *Goldsmith) FilterPush(filter Filter) *Goldsmith {
goldsmith.filters.push(filter) self.filters.push(filter, self.index)
return goldsmith self.index++
return self
} }
// FilterPop pops a filter instance from the chain's filter stack. // FilterPop pops a filter instance from the chain's filter stack.
func (goldsmith *Goldsmith) FilterPop() *Goldsmith { func (self *Goldsmith) FilterPop() *Goldsmith {
goldsmith.filters.pop() self.filters.pop()
return goldsmith self.index++
return self
} }
// End stops a chain, writing all recieved files to targetDir as output. // End stops a chain, writing all recieved files to targetDir as output.
func (goldsmith *Goldsmith) End(targetDir string) []error { func (self *Goldsmith) End(targetDir string) []error {
goldsmith.targetDir = targetDir self.targetDir = targetDir
goldsmith.Chain(&saver{clean: goldsmith.clean}) self.Chain(&saver{clean: self.clean})
for _, context := range goldsmith.contexts { for _, context := range self.contexts {
go context.step() go context.step()
} }
context := goldsmith.contexts[len(goldsmith.contexts)-1] context := self.contexts[len(self.contexts)-1]
for range context.filesOut { for range context.filesOut {
} }
return goldsmith.errors return self.errors
} }
func (goldsmith *Goldsmith) fault(name string, file *File, err error) { func (self *Goldsmith) fault(name string, file *File, err error) {
goldsmith.mutex.Lock() self.mutex.Lock()
defer goldsmith.mutex.Unlock() defer self.mutex.Unlock()
var faultError error var faultError error
if file == nil { if file == nil {
@ -108,5 +103,5 @@ func (goldsmith *Goldsmith) fault(name string, file *File, err error) {
faultError = fmt.Errorf("[%s@%v]: %w", name, file, err) faultError = fmt.Errorf("[%s@%v]: %w", name, file, err)
} }
goldsmith.errors = append(goldsmith.errors, faultError) self.errors = append(self.errors, faultError)
} }

View File

@ -9,22 +9,18 @@ func (*loader) Name() string {
} }
func (*loader) Initialize(context *Context) error { func (*loader) Initialize(context *Context) error {
infos := make(chan fileInfo) scannedInfo := make(chan fileInfo)
go scanDir(context.goldsmith.sourceDir, infos) go scanDir(context.goldsmith.sourceDir, scannedInfo)
for info := range infos { for info := range scannedInfo {
if info.IsDir() { if info.IsDir() {
continue continue
} }
relPath, _ := filepath.Rel(context.goldsmith.sourceDir, info.path) relPath, _ := filepath.Rel(context.goldsmith.sourceDir, info.path)
file, err := context.CreateFileFromAsset(relPath, info.path)
file := &File{ if err != nil {
sourcePath: relPath, return err
Meta: make(map[string]interface{}),
modTime: info.ModTime(),
size: info.Size(),
dataPath: info.path,
} }
context.DispatchFile(file) context.DispatchFile(file)

View File

@ -14,32 +14,32 @@ func (*saver) Name() string {
return "saver" return "saver"
} }
func (saver *saver) Initialize(context *Context) error { func (self *saver) Initialize(context *Context) error {
saver.tokens = make(map[string]bool) self.tokens = make(map[string]bool)
context.Threads(1) context.Threads(1)
return nil return nil
} }
func (saver *saver) Process(context *Context, file *File) error { func (self *saver) Process(context *Context, file *File) error {
for token := cleanPath(file.sourcePath); token != "."; token = filepath.Dir(token) { for token := cleanPath(file.relPath); token != "."; token = filepath.Dir(token) {
saver.tokens[token] = true self.tokens[token] = true
} }
return file.export(context.goldsmith.targetDir) return file.export(context.goldsmith.targetDir)
} }
func (saver *saver) Finalize(context *Context) error { func (self *saver) Finalize(context *Context) error {
if !saver.clean { if !self.clean {
return nil return nil
} }
infos := make(chan fileInfo) scannedInfo := make(chan fileInfo)
go scanDir(context.goldsmith.targetDir, infos) go scanDir(context.goldsmith.targetDir, scannedInfo)
for info := range infos { for info := range scannedInfo {
if info.path != context.goldsmith.targetDir { if info.path != context.goldsmith.targetDir {
relPath, _ := filepath.Rel(context.goldsmith.targetDir, info.path) relPath, _ := filepath.Rel(context.goldsmith.targetDir, info.path)
if contained, _ := saver.tokens[relPath]; !contained { if contained, _ := self.tokens[relPath]; !contained {
os.RemoveAll(info.path) os.RemoveAll(info.path)
} }
} }