Cleanup
This commit is contained in:
parent
ab0c1c53d2
commit
05f7ee5280
@ -117,8 +117,8 @@ func (self *contextImpl) DispatchFile(file File) {
|
|||||||
// 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 (self *contextImpl) DispatchAndCacheFile(outputFile File, inputFiles ...File) {
|
func (self *contextImpl) DispatchAndCacheFile(outputFile File, inputFiles ...File) {
|
||||||
if self.goldsmith.cache != nil {
|
if self.goldsmith.state.cache != nil {
|
||||||
self.goldsmith.cache.storeFile(self, outputFile, inputFiles)
|
self.goldsmith.state.cache.storeFile(self, outputFile, inputFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.DispatchFile(outputFile)
|
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.
|
// will return nil if the desired file is not found in the cache.
|
||||||
func (self *contextImpl) RetrieveCachedFile(outputPath string, inputFiles ...File) File {
|
func (self *contextImpl) RetrieveCachedFile(outputPath string, inputFiles ...File) File {
|
||||||
var outputFile File
|
var outputFile File
|
||||||
if self.goldsmith.cache != nil {
|
if self.goldsmith.state.cache != nil {
|
||||||
outputFile, _ = self.goldsmith.cache.retrieveFile(self, outputPath, inputFiles)
|
outputFile, _ = self.goldsmith.state.cache.retrieveFile(self, outputPath, inputFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
return outputFile
|
return outputFile
|
||||||
|
@ -5,7 +5,6 @@ package devserver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -98,7 +97,7 @@ func build(dirs []string, callback func()) {
|
|||||||
func watch(dir string, watcher *fsnotify.Watcher) {
|
func watch(dir string, watcher *fsnotify.Watcher) {
|
||||||
watcher.Add(dir)
|
watcher.Add(dir)
|
||||||
|
|
||||||
items, err := ioutil.ReadDir(dir)
|
items, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
57
goldsmith.go
57
goldsmith.go
@ -6,8 +6,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Goldsmith chainable context.
|
type chainState struct {
|
||||||
type Goldsmith struct {
|
|
||||||
contexts []*contextImpl
|
contexts []*contextImpl
|
||||||
|
|
||||||
cache *cache
|
cache *cache
|
||||||
@ -19,22 +18,27 @@ type Goldsmith struct {
|
|||||||
mutex sync.Mutex
|
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.
|
// Begin starts a chain, reading the files located in the source directory as input.
|
||||||
func Begin(sourceDir string) *Goldsmith {
|
func (self *Goldsmith) Begin(sourceDir string) *Goldsmith {
|
||||||
goldsmith := new(Goldsmith)
|
self.state = new(chainState)
|
||||||
goldsmith.Chain(&fileImporter{sourceDir: sourceDir})
|
self.Chain(&fileImporter{sourceDir: sourceDir})
|
||||||
return goldsmith
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache enables caching in cacheDir for the remainder of the chain.
|
// Cache enables caching in cacheDir for the remainder of the chain.
|
||||||
func (self *Goldsmith) Cache(cacheDir string) *Goldsmith {
|
func (self *Goldsmith) Cache(cacheDir string) *Goldsmith {
|
||||||
self.cache = &cache{cacheDir}
|
self.state.cache = &cache{cacheDir}
|
||||||
return self
|
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 (self *Goldsmith) Clean(clean bool) *Goldsmith {
|
func (self *Goldsmith) Clean(clean bool) *Goldsmith {
|
||||||
self.clean = clean
|
self.state.clean = clean
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,53 +47,56 @@ func (self *Goldsmith) Chain(plugin Plugin) *Goldsmith {
|
|||||||
context := &contextImpl{
|
context := &contextImpl{
|
||||||
goldsmith: self,
|
goldsmith: self,
|
||||||
plugin: plugin,
|
plugin: plugin,
|
||||||
filtersExt: append(filterStack(nil), self.filters...),
|
filtersExt: append(filterStack(nil), self.state.filters...),
|
||||||
index: self.index,
|
index: self.state.index,
|
||||||
filesOut: make(chan File),
|
filesOut: make(chan File),
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(self.contexts) > 0 {
|
if len(self.state.contexts) > 0 {
|
||||||
context.filesIn = self.contexts[len(self.contexts)-1].filesOut
|
context.filesIn = self.state.contexts[len(self.state.contexts)-1].filesOut
|
||||||
}
|
}
|
||||||
|
|
||||||
self.contexts = append(self.contexts, context)
|
self.state.contexts = append(self.state.contexts, context)
|
||||||
self.index++
|
self.state.index++
|
||||||
|
|
||||||
return self
|
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 (self *Goldsmith) FilterPush(filter Filter) *Goldsmith {
|
func (self *Goldsmith) FilterPush(filter Filter) *Goldsmith {
|
||||||
self.filters.push(filter, self.index)
|
self.state.filters.push(filter, self.state.index)
|
||||||
self.index++
|
self.state.index++
|
||||||
return self
|
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 (self *Goldsmith) FilterPop() *Goldsmith {
|
func (self *Goldsmith) FilterPop() *Goldsmith {
|
||||||
self.filters.pop()
|
self.state.filters.pop()
|
||||||
self.index++
|
self.state.index++
|
||||||
return self
|
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 (self *Goldsmith) End(targetDir string) []error {
|
func (self *Goldsmith) End(targetDir string) []error {
|
||||||
self.Chain(&fileExporter{targetDir: targetDir, clean: self.clean})
|
self.Chain(&fileExporter{targetDir: targetDir, clean: self.state.clean})
|
||||||
for _, context := range self.contexts {
|
for _, context := range self.state.contexts {
|
||||||
go context.step()
|
go context.step()
|
||||||
}
|
}
|
||||||
|
|
||||||
context := self.contexts[len(self.contexts)-1]
|
context := self.state.contexts[len(self.state.contexts)-1]
|
||||||
for range context.filesOut {
|
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) {
|
func (self *Goldsmith) fault(name string, file File, err error) {
|
||||||
self.mutex.Lock()
|
self.state.mutex.Lock()
|
||||||
defer self.mutex.Unlock()
|
defer self.state.mutex.Unlock()
|
||||||
|
|
||||||
var faultError error
|
var faultError error
|
||||||
if file == nil {
|
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)
|
faultError = fmt.Errorf("[%s@%v]: %w", name, file, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.errors = append(self.errors, faultError)
|
self.state.errors = append(self.state.errors, faultError)
|
||||||
}
|
}
|
||||||
|
@ -68,8 +68,9 @@ func validate(sourceDir, targetDir, cacheDir, referenceDir string, stager Stager
|
|||||||
}
|
}
|
||||||
|
|
||||||
func execute(sourceDir, targetDir, cacheDir string, stager Stager) []error {
|
func execute(sourceDir, targetDir, cacheDir string, stager Stager) []error {
|
||||||
gs := goldsmith.Begin(sourceDir).Cache(cacheDir).Clean(true)
|
var gs goldsmith.Goldsmith
|
||||||
stager(gs)
|
gs.Begin(sourceDir).Cache(cacheDir).Clean(true)
|
||||||
|
stager(&gs)
|
||||||
return gs.End(targetDir)
|
return gs.End(targetDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user