goldsmith/goldsmith.go

171 lines
3.4 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
)
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 08:03:30 +00:00
s := newStage(gs)
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 10:19:43 +00:00
f := NewFileFromAsset(relPath, path)
s.DispatchFile(f)
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 10:19:43 +00:00
gs.referenceFile(f.path)
2015-12-18 04:14:39 +00:00
return nil
2015-11-02 09:23:13 +00:00
}
2015-12-18 10:19:43 +00:00
func (gs *goldsmith) referenceFile(path string) {
2015-12-18 08:03:30 +00:00
gs.mtx.Lock()
defer gs.mtx.Unlock()
2015-12-17 07:32:21 +00:00
2015-12-18 08:03:30 +00:00
if gs.refs == nil {
gs.refs = make(map[string]bool)
2015-12-17 03:37:11 +00:00
}
2015-12-17 07:16:18 +00:00
2015-12-18 08:03:30 +00:00
path = cleanPath(path)
2015-12-17 03:37:11 +00:00
2015-12-18 08:03:30 +00:00
for {
gs.refs[path] = true
if path == "." {
break
2015-12-18 04:14:39 +00:00
}
2015-12-18 08:03:30 +00:00
path = filepath.Dir(path)
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 08:03:30 +00:00
s := newStage(gs)
go s.chain(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
}