goldsmith/goldsmith.go

145 lines
2.9 KiB
Go
Raw Normal View History

2015-10-29 09:24:47 +00:00
package goldsmith
2015-10-29 14:26:43 +00:00
import (
2018-12-08 19:18:51 +00:00
"hash"
"hash/crc32"
2016-06-11 23:24:06 +00:00
"os"
2018-12-08 19:18:51 +00:00
"path/filepath"
"sync"
2015-10-29 14:26:43 +00:00
)
2018-12-08 19:18:51 +00:00
type Goldsmith struct {
sourceDir string
targetDir string
contexts []*Context
contextHasher hash.Hash32
fileRefs map[string]bool
fileFilters []Filter
fileCache *fileCache
errors []error
errorMtx sync.Mutex
2016-01-13 03:21:30 +00:00
}
2015-12-18 11:30:14 +00:00
2018-12-08 19:18:51 +00:00
func Begin(sourceDir string) *Goldsmith {
gs := &Goldsmith{
sourceDir: sourceDir,
contextHasher: crc32.NewIEEE(),
fileRefs: make(map[string]bool),
}
2018-01-07 23:42:32 +00:00
gs.Chain(new(loader))
2016-01-13 03:21:30 +00:00
return gs
2015-12-18 04:14:39 +00:00
}
2018-12-08 19:18:51 +00:00
func (gs *Goldsmith) Cache(cacheDir string) *Goldsmith {
gs.fileCache = &fileCache{cacheDir}
return gs
2015-10-29 14:26:43 +00:00
}
2018-12-08 19:18:51 +00:00
func (gs *Goldsmith) Chain(plugin Plugin) *Goldsmith {
gs.contextHasher.Write([]byte(plugin.Name()))
2015-11-02 09:07:34 +00:00
2018-12-08 19:18:51 +00:00
context := &Context{
goldsmith: gs,
plugin: plugin,
hash: gs.contextHasher.Sum32(),
outputFiles: make(chan *File),
2016-06-11 23:24:06 +00:00
}
2018-12-08 19:18:51 +00:00
context.fileFilters = append(context.fileFilters, gs.fileFilters...)
2016-06-12 02:38:17 +00:00
2018-12-08 19:18:51 +00:00
if len(gs.contexts) > 0 {
context.inputFiles = gs.contexts[len(gs.contexts)-1].outputFiles
2015-11-02 09:23:13 +00:00
}
2016-06-11 23:24:06 +00:00
2018-12-08 19:18:51 +00:00
gs.contexts = append(gs.contexts, context)
return gs
2015-11-02 09:23:13 +00:00
}
2018-12-08 19:18:51 +00:00
func (gs *Goldsmith) FilterPush(filter Filter) *Goldsmith {
gs.fileFilters = append(gs.fileFilters, filter)
return gs
2015-10-31 05:12:03 +00:00
}
2018-12-08 19:18:51 +00:00
func (gs *Goldsmith) FilterPop() *Goldsmith {
count := len(gs.fileFilters)
if count == 0 {
panic("attempted to pop empty filter stack")
}
gs.fileFilters = gs.fileFilters[:count-1]
return gs
2015-12-18 04:37:32 +00:00
}
2018-12-08 19:18:51 +00:00
func (gs *Goldsmith) End(targetDir string) []error {
gs.targetDir = targetDir
for _, context := range gs.contexts {
go context.step()
2016-08-21 19:54:44 +00:00
}
2018-12-08 19:18:51 +00:00
context := gs.contexts[len(gs.contexts)-1]
for file := range context.outputFiles {
gs.exportFile(file)
}
2015-10-29 09:24:47 +00:00
2018-12-08 19:18:51 +00:00
gs.removeUnreferencedFiles()
return gs.errors
2016-01-13 03:21:30 +00:00
}
2016-01-10 11:17:35 +00:00
2018-12-08 19:18:51 +00:00
func (gs *Goldsmith) retrieveFile(context *Context, outputPath string, inputFiles []*File) *File {
if gs.fileCache != nil {
outputFile, _ := gs.fileCache.retrieveFile(context, outputPath, inputFiles)
return outputFile
}
return nil
2016-01-13 03:21:30 +00:00
}
2015-10-31 03:30:41 +00:00
2018-12-08 19:18:51 +00:00
func (gs *Goldsmith) storeFile(context *Context, outputFile *File, inputFiles []*File) {
if gs.fileCache != nil {
gs.fileCache.storeFile(context, outputFile, inputFiles)
}
2015-10-29 09:24:47 +00:00
}
2016-01-13 03:21:30 +00:00
2018-12-08 19:18:51 +00:00
func (gs *Goldsmith) removeUnreferencedFiles() {
infos := make(chan fileInfo)
go scanDir(gs.targetDir, infos)
for info := range infos {
if info.path != gs.targetDir {
relPath, _ := filepath.Rel(gs.targetDir, info.path)
if contained, _ := gs.fileRefs[relPath]; !contained {
os.RemoveAll(info.path)
}
}
}
2016-08-21 19:54:44 +00:00
}
2018-01-07 23:42:32 +00:00
2018-12-08 19:18:51 +00:00
func (gs *Goldsmith) exportFile(file *File) error {
if err := file.export(gs.targetDir); err != nil {
return err
}
for pathSeg := cleanPath(file.sourcePath); pathSeg != "."; pathSeg = filepath.Dir(pathSeg) {
gs.fileRefs[pathSeg] = true
}
return nil
2018-01-07 23:42:32 +00:00
}
2018-12-08 19:18:51 +00:00
func (gs *Goldsmith) fault(pluginName string, file *File, err error) {
gs.errorMtx.Lock()
defer gs.errorMtx.Unlock()
faultError := &Error{Name: pluginName, Err: err}
if file != nil {
faultError.Path = file.sourcePath
}
gs.errors = append(gs.errors, faultError)
2018-01-07 23:42:32 +00:00
}