goldsmith/goldsmith.go

274 lines
5.1 KiB
Go
Raw Normal View History

2015-10-29 09:24:47 +00:00
/*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package goldsmith
2015-10-29 14:26:43 +00:00
import (
2015-12-18 04:14:39 +00:00
"log"
2015-10-31 04:08:10 +00:00
"os"
2015-10-29 14:26:43 +00:00
"path/filepath"
2015-11-10 04:00:24 +00:00
"sync"
2015-12-17 04:20:41 +00:00
"sync/atomic"
2015-12-17 08:55:59 +00:00
"time"
2015-10-29 14:26:43 +00:00
)
2015-12-18 04:21:49 +00:00
type stage struct {
2015-12-18 04:37:32 +00:00
name string
2015-12-18 04:21:49 +00:00
input, output chan *file
}
2015-10-29 14:26:43 +00:00
type goldsmith struct {
2015-11-02 08:11:11 +00:00
srcDir, dstDir string
2015-11-02 09:07:34 +00:00
refs map[string]bool
2015-12-17 04:06:44 +00:00
mtx sync.Mutex
2015-12-18 04:37:32 +00:00
stages []*stage
2015-12-17 14:17:55 +00:00
active int64
2015-12-17 08:55:59 +00:00
stalled int64
2015-12-18 04:14:39 +00:00
tainted bool
}
2015-12-17 08:55:59 +00:00
func (gs *goldsmith) queueFiles(target uint) {
2015-11-11 08:32:09 +00:00
files := make(chan string)
go scanDir(gs.srcDir, files, nil)
2015-10-29 14:26:43 +00:00
2015-12-18 04:37:32 +00:00
s := gs.newStage("Goldsmith")
2015-11-01 05:09:46 +00:00
2015-11-03 01:13:04 +00:00
go func() {
defer close(s.output)
2015-11-01 05:09:46 +00:00
2015-11-11 08:32:09 +00:00
for path := range files {
2015-12-17 08:55:59 +00:00
for {
2015-12-17 14:17:55 +00:00
if gs.active-gs.stalled >= int64(target) {
2015-12-17 08:55:59 +00:00
time.Sleep(time.Millisecond)
} else {
break
}
}
2015-11-11 08:32:09 +00:00
relPath, err := filepath.Rel(gs.srcDir, path)
2015-11-03 01:13:04 +00:00
if err != nil {
panic(err)
}
2015-11-01 05:09:46 +00:00
2015-12-18 04:21:49 +00:00
gs.CopyFile(relPath, path)
2015-11-03 01:13:04 +00:00
}
}()
2015-10-29 14:26:43 +00:00
}
2015-11-11 08:32:09 +00:00
func (gs *goldsmith) cleanupFiles() {
2015-12-17 04:06:44 +00:00
var (
files = make(chan string)
dirs = make(chan string)
)
2015-11-11 08:32:09 +00:00
go scanDir(gs.dstDir, files, dirs)
for files != nil || dirs != nil {
var (
path string
ok bool
)
select {
case path, ok = <-files:
if !ok {
files = nil
continue
}
case path, ok = <-dirs:
if !ok {
dirs = nil
continue
}
default:
continue
}
2015-11-07 07:40:13 +00:00
2015-11-11 08:32:09 +00:00
relPath, err := filepath.Rel(gs.dstDir, path)
2015-11-02 09:07:34 +00:00
if err != nil {
2015-11-07 07:40:13 +00:00
panic(err)
2015-11-02 09:07:34 +00:00
}
2015-11-07 07:40:13 +00:00
if contained, _ := gs.refs[relPath]; contained {
continue
}
2015-11-11 08:32:09 +00:00
os.RemoveAll(path)
2015-11-02 09:07:34 +00:00
}
}
2015-12-18 04:14:39 +00:00
func (gs *goldsmith) exportFile(f *file) error {
defer atomic.AddInt64(&gs.active, -1)
2015-12-17 04:20:41 +00:00
2015-12-18 04:14:39 +00:00
absPath := filepath.Join(gs.dstDir, f.path)
if err := f.export(absPath); err != nil {
return err
2015-11-02 09:23:13 +00:00
}
2015-12-18 04:21:49 +00:00
gs.RefFile(f.path)
2015-12-18 04:14:39 +00:00
return nil
2015-11-02 09:23:13 +00:00
}
2015-12-18 04:37:32 +00:00
func (gs *goldsmith) newStage(name string) *stage {
s := &stage{
name: name,
output: make(chan *file),
}
2015-11-03 01:13:04 +00:00
if len(gs.stages) > 0 {
s.input = gs.stages[len(gs.stages)-1].output
2015-11-02 08:11:11 +00:00
}
2015-10-29 14:26:43 +00:00
gs.stages = append(gs.stages, s)
return s
}
2015-12-17 03:37:11 +00:00
func (gs *goldsmith) chain(s *stage, p Plugin) {
defer close(s.output)
2015-11-10 04:00:24 +00:00
2015-12-17 07:32:21 +00:00
init, _ := p.(Initializer)
2015-12-17 05:12:01 +00:00
accept, _ := p.(Accepter)
proc, _ := p.(Processor)
2015-12-17 07:16:18 +00:00
fin, _ := p.(Finalizer)
var (
wg sync.WaitGroup
mtx sync.Mutex
2015-12-18 04:14:39 +00:00
batch []File
2015-12-17 07:16:18 +00:00
)
2015-12-18 04:14:39 +00:00
dispatch := func(f *file) {
2015-12-17 07:16:18 +00:00
if fin == nil {
s.output <- f
} else {
mtx.Lock()
2015-12-17 07:32:21 +00:00
batch = append(batch, f)
2015-12-17 07:16:18 +00:00
mtx.Unlock()
2015-12-18 04:14:39 +00:00
2015-12-18 04:21:49 +00:00
atomic.AddInt64(&gs.stalled, 1)
2015-12-17 07:16:18 +00:00
}
}
2015-12-17 05:12:01 +00:00
2015-12-17 07:32:21 +00:00
if init != nil {
2015-12-18 04:21:49 +00:00
if err := init.Initialize(gs); err != nil {
2015-12-18 04:14:39 +00:00
gs.fault(s, nil, err)
2015-12-17 07:32:21 +00:00
return
}
}
2015-12-18 04:14:39 +00:00
for f := range s.input {
if accept != nil && !accept.Accept(f) {
s.output <- f
2015-12-17 08:55:59 +00:00
} else if proc == nil {
2015-12-18 04:14:39 +00:00
dispatch(f)
2015-12-17 05:12:01 +00:00
} else {
wg.Add(1)
2015-12-18 04:14:39 +00:00
go func(f *file) {
2015-12-17 03:37:11 +00:00
defer wg.Done()
2015-12-18 04:21:49 +00:00
if err := proc.Process(gs, f); err != nil {
2015-12-18 04:14:39 +00:00
gs.fault(s, f, err)
2015-12-17 03:37:11 +00:00
}
2015-12-18 04:14:39 +00:00
dispatch(f)
}(f)
2015-11-10 04:00:24 +00:00
}
2015-12-17 03:37:11 +00:00
}
2015-12-17 07:16:18 +00:00
2015-12-17 05:12:01 +00:00
wg.Wait()
2015-12-17 03:37:11 +00:00
2015-12-17 07:16:18 +00:00
if fin != nil {
2015-12-18 04:21:49 +00:00
if err := fin.Finalize(gs, batch); err != nil {
2015-12-18 04:14:39 +00:00
gs.fault(s, nil, err)
}
for _, f := range batch {
2015-12-18 04:21:49 +00:00
atomic.AddInt64(&gs.stalled, -1)
2015-12-18 04:14:39 +00:00
s.output <- f.(*file)
2015-12-17 07:16:18 +00:00
}
2015-12-17 03:37:11 +00:00
}
2015-10-31 05:12:03 +00:00
}
2015-12-18 04:37:32 +00:00
func (gs *goldsmith) fault(s *stage, f *file, err error) {
log.Printf("%s\t%s\t%s", s.name, f.path, err)
gs.tainted = true
}
//
// Goldsmith Implementation
//
2015-12-17 03:37:11 +00:00
func (gs *goldsmith) Chain(p Plugin) Goldsmith {
2015-12-18 04:37:32 +00:00
go gs.chain(gs.newStage(p.Name()), p)
2015-10-29 14:26:43 +00:00
return gs
2015-10-29 09:24:47 +00:00
}
2015-12-18 04:14:39 +00:00
func (gs *goldsmith) Complete() bool {
2015-10-31 03:30:41 +00:00
s := gs.stages[len(gs.stages)-1]
2015-12-18 04:14:39 +00:00
for f := range s.output {
gs.exportFile(f)
2015-10-31 03:30:41 +00:00
}
2015-12-17 03:37:11 +00:00
gs.cleanupFiles()
2015-12-18 04:37:32 +00:00
return gs.tainted
2015-10-29 09:24:47 +00:00
}
2015-12-18 04:21:49 +00:00
2015-12-18 04:37:32 +00:00
//
// Context Implementation
//
2015-12-18 04:21:49 +00:00
func (gs *goldsmith) NewFile(path string, data []byte) File {
atomic.AddInt64(&gs.active, 1)
return newFileFromData(path, data)
}
func (gs *goldsmith) CopyFile(dst, src string) File {
atomic.AddInt64(&gs.active, 1)
return newFileFromPath(dst, src)
}
func (gs *goldsmith) RefFile(path string) {
gs.mtx.Lock()
defer gs.mtx.Unlock()
if gs.refs == nil {
gs.refs = make(map[string]bool)
}
path = cleanPath(path)
for {
gs.refs[path] = true
if path == "." {
break
}
path = filepath.Dir(path)
}
}
func (gs *goldsmith) SrcDir() string {
return gs.srcDir
}
func (gs *goldsmith) DstDir() string {
return gs.dstDir
}