Simplification
This commit is contained in:
parent
570c65db08
commit
12e62912c8
35
goldsmith.go
35
goldsmith.go
@ -23,22 +23,27 @@
|
|||||||
package goldsmith
|
package goldsmith
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
type goldsmith struct {
|
type goldsmith struct {
|
||||||
srcDir, dstDir string
|
srcDir, dstDir string
|
||||||
refs map[string]bool
|
|
||||||
mtx sync.Mutex
|
stages []*stage
|
||||||
stages []*stage
|
active int64
|
||||||
active int64
|
stalled int64
|
||||||
stalled int64
|
|
||||||
tainted bool
|
refs map[string]bool
|
||||||
|
refMtx sync.Mutex
|
||||||
|
|
||||||
|
tainted bool
|
||||||
|
faultMtx sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gs *goldsmith) queueFiles(target uint) {
|
func (gs *goldsmith) queueFiles(target uint) {
|
||||||
@ -125,8 +130,8 @@ func (gs *goldsmith) exportFile(f *file) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gs *goldsmith) referenceFile(path string) {
|
func (gs *goldsmith) referenceFile(path string) {
|
||||||
gs.mtx.Lock()
|
gs.refMtx.Lock()
|
||||||
defer gs.mtx.Unlock()
|
defer gs.refMtx.Unlock()
|
||||||
|
|
||||||
if gs.refs == nil {
|
if gs.refs == nil {
|
||||||
gs.refs = make(map[string]bool)
|
gs.refs = make(map[string]bool)
|
||||||
@ -144,8 +149,16 @@ func (gs *goldsmith) referenceFile(path string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gs *goldsmith) fault(s *stage, f *file, err error) {
|
func (gs *goldsmith) fault(s *stage, step string, f *file, err error) {
|
||||||
log.Printf("%s\t%s\t%s", s.name, f.path, err)
|
gs.faultMtx.Lock()
|
||||||
|
defer gs.faultMtx.Unlock()
|
||||||
|
|
||||||
|
color.Red("Fault Detected\n")
|
||||||
|
color.Yellow("\tPlugin:\t%s\n", color.WhiteString(s.name))
|
||||||
|
color.Yellow("\tStep:\t%s\n", color.WhiteString(step))
|
||||||
|
color.Yellow("\tFile:\t%s\n", color.WhiteString(f.path))
|
||||||
|
color.Yellow("\tError:\t%s\n\n", color.WhiteString(err.Error()))
|
||||||
|
|
||||||
gs.tainted = true
|
gs.tainted = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
7
stage.go
7
stage.go
@ -45,6 +45,7 @@ func newStage(gs *goldsmith) *stage {
|
|||||||
|
|
||||||
func (s *stage) chain(p Plugin) {
|
func (s *stage) chain(p Plugin) {
|
||||||
defer close(s.output)
|
defer close(s.output)
|
||||||
|
s.name = p.Name()
|
||||||
|
|
||||||
init, _ := p.(Initializer)
|
init, _ := p.(Initializer)
|
||||||
accept, _ := p.(Accepter)
|
accept, _ := p.(Accepter)
|
||||||
@ -71,7 +72,7 @@ func (s *stage) chain(p Plugin) {
|
|||||||
|
|
||||||
if init != nil {
|
if init != nil {
|
||||||
if err := init.Initialize(s); err != nil {
|
if err := init.Initialize(s); err != nil {
|
||||||
s.gs.fault(s, nil, err)
|
s.gs.fault(s, "Initialization", nil, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,7 +89,7 @@ func (s *stage) chain(p Plugin) {
|
|||||||
|
|
||||||
f.rewind()
|
f.rewind()
|
||||||
if err := proc.Process(s, f); err != nil {
|
if err := proc.Process(s, f); err != nil {
|
||||||
s.gs.fault(s, f, err)
|
s.gs.fault(s, "Processing", f, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(f)
|
dispatch(f)
|
||||||
@ -100,7 +101,7 @@ func (s *stage) chain(p Plugin) {
|
|||||||
|
|
||||||
if fin != nil {
|
if fin != nil {
|
||||||
if err := fin.Finalize(s, batch); err != nil {
|
if err := fin.Finalize(s, batch); err != nil {
|
||||||
s.gs.fault(s, nil, err)
|
s.gs.fault(s, "Finalization", nil, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range batch {
|
for _, f := range batch {
|
||||||
|
6
types.go
6
types.go
@ -34,17 +34,13 @@ type Goldsmith interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New(srcDir, dstDir string) Goldsmith {
|
func New(srcDir, dstDir string) Goldsmith {
|
||||||
return NewThrottled(srcDir, dstDir, uint(runtime.NumCPU()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewThrottled(srcDir, dstDir string, targetFileCount uint) Goldsmith {
|
|
||||||
gs := &goldsmith{
|
gs := &goldsmith{
|
||||||
srcDir: srcDir,
|
srcDir: srcDir,
|
||||||
dstDir: dstDir,
|
dstDir: dstDir,
|
||||||
refs: make(map[string]bool),
|
refs: make(map[string]bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
gs.queueFiles(targetFileCount)
|
gs.queueFiles(uint(runtime.NumCPU()))
|
||||||
return gs
|
return gs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user