This commit is contained in:
Alex Yatskov 2024-03-03 16:50:03 -08:00
parent ab0c1c53d2
commit 05f7ee5280
4 changed files with 40 additions and 33 deletions

View File

@ -117,8 +117,8 @@ func (self *contextImpl) DispatchFile(file File) {
// dependencies on any input files that are needed to generate it, and then
// passes it to the next link in the chain.
func (self *contextImpl) DispatchAndCacheFile(outputFile File, inputFiles ...File) {
if self.goldsmith.cache != nil {
self.goldsmith.cache.storeFile(self, outputFile, inputFiles)
if self.goldsmith.state.cache != nil {
self.goldsmith.state.cache.storeFile(self, outputFile, inputFiles)
}
self.DispatchFile(outputFile)
@ -129,8 +129,8 @@ func (self *contextImpl) DispatchAndCacheFile(outputFile File, inputFiles ...Fil
// will return nil if the desired file is not found in the cache.
func (self *contextImpl) RetrieveCachedFile(outputPath string, inputFiles ...File) File {
var outputFile File
if self.goldsmith.cache != nil {
outputFile, _ = self.goldsmith.cache.retrieveFile(self, outputPath, inputFiles)
if self.goldsmith.state.cache != nil {
outputFile, _ = self.goldsmith.state.cache.retrieveFile(self, outputPath, inputFiles)
}
return outputFile

View File

@ -5,7 +5,6 @@ package devserver
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
@ -98,7 +97,7 @@ func build(dirs []string, callback func()) {
func watch(dir string, watcher *fsnotify.Watcher) {
watcher.Add(dir)
items, err := ioutil.ReadDir(dir)
items, err := os.ReadDir(dir)
if err != nil {
log.Fatal(err)
}

View File

@ -6,8 +6,7 @@ import (
"sync"
)
// Goldsmith chainable context.
type Goldsmith struct {
type chainState struct {
contexts []*contextImpl
cache *cache
@ -19,22 +18,27 @@ type Goldsmith struct {
mutex sync.Mutex
}
// Goldsmith chainable context.
type Goldsmith struct {
state *chainState
}
// Begin starts a chain, reading the files located in the source directory as input.
func Begin(sourceDir string) *Goldsmith {
goldsmith := new(Goldsmith)
goldsmith.Chain(&fileImporter{sourceDir: sourceDir})
return goldsmith
func (self *Goldsmith) Begin(sourceDir string) *Goldsmith {
self.state = new(chainState)
self.Chain(&fileImporter{sourceDir: sourceDir})
return self
}
// Cache enables caching in cacheDir for the remainder of the chain.
func (self *Goldsmith) Cache(cacheDir string) *Goldsmith {
self.cache = &cache{cacheDir}
self.state.cache = &cache{cacheDir}
return self
}
// Clean enables or disables removal of leftover files in the target directory.
func (self *Goldsmith) Clean(clean bool) *Goldsmith {
self.clean = clean
self.state.clean = clean
return self
}
@ -43,53 +47,56 @@ func (self *Goldsmith) Chain(plugin Plugin) *Goldsmith {
context := &contextImpl{
goldsmith: self,
plugin: plugin,
filtersExt: append(filterStack(nil), self.filters...),
index: self.index,
filtersExt: append(filterStack(nil), self.state.filters...),
index: self.state.index,
filesOut: make(chan File),
}
if len(self.contexts) > 0 {
context.filesIn = self.contexts[len(self.contexts)-1].filesOut
if len(self.state.contexts) > 0 {
context.filesIn = self.state.contexts[len(self.state.contexts)-1].filesOut
}
self.contexts = append(self.contexts, context)
self.index++
self.state.contexts = append(self.state.contexts, context)
self.state.index++
return self
}
// FilterPush pushes a filter instance on the chain's filter stack.
func (self *Goldsmith) FilterPush(filter Filter) *Goldsmith {
self.filters.push(filter, self.index)
self.index++
self.state.filters.push(filter, self.state.index)
self.state.index++
return self
}
// FilterPop pops a filter instance from the chain's filter stack.
func (self *Goldsmith) FilterPop() *Goldsmith {
self.filters.pop()
self.index++
self.state.filters.pop()
self.state.index++
return self
}
// End stops a chain, writing all recieved files to targetDir as output.
func (self *Goldsmith) End(targetDir string) []error {
self.Chain(&fileExporter{targetDir: targetDir, clean: self.clean})
for _, context := range self.contexts {
self.Chain(&fileExporter{targetDir: targetDir, clean: self.state.clean})
for _, context := range self.state.contexts {
go context.step()
}
context := self.contexts[len(self.contexts)-1]
context := self.state.contexts[len(self.state.contexts)-1]
for range context.filesOut {
}
return self.errors
errors := self.state.errors
self.state = nil
return errors
}
func (self *Goldsmith) fault(name string, file File, err error) {
self.mutex.Lock()
defer self.mutex.Unlock()
self.state.mutex.Lock()
defer self.state.mutex.Unlock()
var faultError error
if file == nil {
@ -98,5 +105,5 @@ func (self *Goldsmith) fault(name string, file File, err error) {
faultError = fmt.Errorf("[%s@%v]: %w", name, file, err)
}
self.errors = append(self.errors, faultError)
self.state.errors = append(self.state.errors, faultError)
}

View File

@ -68,8 +68,9 @@ func validate(sourceDir, targetDir, cacheDir, referenceDir string, stager Stager
}
func execute(sourceDir, targetDir, cacheDir string, stager Stager) []error {
gs := goldsmith.Begin(sourceDir).Cache(cacheDir).Clean(true)
stager(gs)
var gs goldsmith.Goldsmith
gs.Begin(sourceDir).Cache(cacheDir).Clean(true)
stager(&gs)
return gs.End(targetDir)
}