goldsmith/core.go

133 lines
2.3 KiB
Go
Raw Normal View History

2016-01-13 03:21:30 +00:00
package goldsmith
import (
"os"
"path/filepath"
"sync"
)
type goldsmith struct {
srcDir, dstDir string
contexts []*context
2016-01-13 04:12:50 +00:00
refs map[string]bool
2016-09-04 18:43:04 +00:00
complete bool
2018-01-07 23:42:32 +00:00
filters []Filter
2016-01-13 03:21:30 +00:00
errors []error
errorMtx sync.Mutex
}
2018-01-07 23:42:32 +00:00
func (gs *goldsmith) pushContext(plug Plugin) *context {
ctx := &context{gs: gs, plug: plug, output: make(chan *file)}
ctx.filters = append(ctx.filters, gs.filters...)
2016-01-13 03:52:45 +00:00
2016-01-13 03:21:30 +00:00
if len(gs.contexts) > 0 {
ctx.input = gs.contexts[len(gs.contexts)-1].output
}
gs.contexts = append(gs.contexts, ctx)
return ctx
}
func (gs *goldsmith) cleanupFiles() {
2016-06-11 20:13:16 +00:00
infos := make(chan fileInfo)
go scanDir(gs.dstDir, infos)
2016-01-13 03:21:30 +00:00
2016-06-11 20:13:16 +00:00
for info := range infos {
relPath, _ := filepath.Rel(gs.dstDir, info.path)
2016-01-13 03:21:30 +00:00
if contained, _ := gs.refs[relPath]; contained {
continue
}
2016-06-11 20:13:16 +00:00
os.RemoveAll(info.path)
2016-01-13 03:21:30 +00:00
}
}
func (gs *goldsmith) exportFile(f *file) error {
2016-01-13 04:12:50 +00:00
if err := f.export(gs.dstDir); err != nil {
2016-01-13 03:21:30 +00:00
return err
}
2016-01-13 04:12:50 +00:00
pathSeg := cleanPath(f.path)
2016-01-13 03:21:30 +00:00
for {
2016-01-13 04:12:50 +00:00
gs.refs[pathSeg] = true
if pathSeg == "." {
2016-01-13 03:21:30 +00:00
break
}
2016-01-13 04:12:50 +00:00
pathSeg = filepath.Dir(pathSeg)
2016-01-13 03:21:30 +00:00
}
2016-01-13 04:12:50 +00:00
return nil
2016-01-13 03:21:30 +00:00
}
2016-08-21 19:54:44 +00:00
func (gs *goldsmith) fault(name string, f *file, err error) {
2016-01-13 03:21:30 +00:00
gs.errorMtx.Lock()
2016-01-13 03:52:45 +00:00
defer gs.errorMtx.Unlock()
2016-08-21 19:54:44 +00:00
ferr := &Error{Name: name, Err: err}
2016-01-13 03:21:30 +00:00
if f != nil {
ferr.Path = f.path
}
2016-01-13 03:52:45 +00:00
2016-01-13 03:21:30 +00:00
gs.errors = append(gs.errors, ferr)
}
//
// Goldsmith Implementation
//
2018-01-07 23:42:32 +00:00
func (gs *goldsmith) Chain(p Plugin) Goldsmith {
2016-09-04 18:43:04 +00:00
if gs.complete {
panic("attempted reuse of goldsmith instance")
}
2018-01-07 23:42:32 +00:00
gs.pushContext(p)
return gs
}
func (gs *goldsmith) FilterPush(f Filter) Goldsmith {
if gs.complete {
panic("attempted reuse of goldsmith instance")
}
gs.filters = append(gs.filters, f)
return gs
}
func (gs *goldsmith) FilterPop() Goldsmith {
if gs.complete {
panic("attempted reuse of goldsmith instance")
}
count := len(gs.filters)
if count == 0 {
panic("attempted to pop empty filter stack")
}
gs.filters = gs.filters[:count-1]
2016-01-13 03:21:30 +00:00
return gs
}
func (gs *goldsmith) End(dstDir string) []error {
2016-09-04 18:43:04 +00:00
if gs.complete {
panic("attempted reuse of goldsmith instance")
}
2016-01-13 03:21:30 +00:00
gs.dstDir = dstDir
for _, ctx := range gs.contexts {
go ctx.chain()
}
ctx := gs.contexts[len(gs.contexts)-1]
for f := range ctx.output {
gs.exportFile(f)
}
gs.cleanupFiles()
2016-09-04 18:43:04 +00:00
gs.complete = true
2016-01-13 03:21:30 +00:00
return gs.errors
}