goldsmith/goldsmith.go

267 lines
4.9 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-10-31 04:08:10 +00:00
"os"
"path"
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-10-29 14:26:43 +00:00
)
type stage struct {
2015-12-17 03:52:14 +00:00
gs *goldsmith
2015-11-02 08:11:11 +00:00
input, output chan *File
2015-12-17 03:37:11 +00:00
err error
2015-10-29 09:24:47 +00:00
}
2015-10-29 14:26:43 +00:00
type goldsmith struct {
2015-11-02 08:11:11 +00:00
srcDir, dstDir string
2015-12-17 03:37:11 +00:00
stages []*stage
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-17 04:20:41 +00:00
count int32
2015-11-11 05:21:47 +00:00
}
2015-11-11 13:57:49 +00:00
func (gs *goldsmith) queueFiles() {
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-17 03:58:28 +00:00
s := gs.newStage()
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 {
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-17 04:20:41 +00:00
file := NewFile(relPath)
2015-10-29 14:26:43 +00:00
2015-11-03 01:13:04 +00:00
var f *os.File
2015-11-11 08:32:09 +00:00
if f, file.Err = os.Open(path); file.Err == nil {
2015-11-03 01:13:04 +00:00
_, file.Err = file.Buff.ReadFrom(f)
f.Close()
}
2015-10-29 14:26:43 +00:00
2015-11-03 01:13:04 +00:00
s.output <- file
}
}()
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-11-03 01:13:04 +00:00
func (gs *goldsmith) exportFile(file *File) {
2015-12-17 04:20:41 +00:00
defer func() {
file.Buff.Reset()
gs.decFiles()
}()
2015-11-02 09:23:13 +00:00
if file.Err != nil {
return
}
absPath := filepath.Join(gs.dstDir, file.Path)
if file.Err = os.MkdirAll(path.Dir(absPath), 0755); file.Err != nil {
return
}
var f *os.File
if f, file.Err = os.Create(absPath); file.Err == nil {
2015-11-07 04:36:20 +00:00
defer f.Close()
2015-11-02 09:23:13 +00:00
if _, file.Err = f.Write(file.Buff.Bytes()); file.Err == nil {
2015-11-11 05:21:47 +00:00
gs.refFile(file.Path)
2015-11-02 09:23:13 +00:00
}
}
}
2015-12-17 04:20:41 +00:00
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) incFiles() {
atomic.AddInt32(&gs.count, 1)
}
func (gs *goldsmith) decFiles() {
atomic.AddInt32(&gs.count, -1)
}
2015-12-17 03:58:28 +00:00
func (gs *goldsmith) newStage() *stage {
s := &stage{
gs: gs,
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 03:37:11 +00:00
if init, ok := p.(Initializer); ok {
2015-12-17 03:52:14 +00:00
if s.err = init.Initialize(s); s.err != nil {
2015-12-17 03:37:11 +00:00
return
2015-11-01 07:41:56 +00:00
}
2015-12-17 03:37:11 +00:00
}
2015-11-10 14:05:07 +00:00
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
files []*File
)
dispatch := func(f *File) {
if fin == nil {
s.output <- f
} else {
mtx.Lock()
files = append(files, f)
mtx.Unlock()
}
}
2015-12-17 05:12:01 +00:00
for file := range s.input {
if file.Err != nil || proc == nil || (accept != nil && !accept.Accept(file)) {
2015-12-17 07:16:18 +00:00
dispatch(file)
2015-12-17 05:12:01 +00:00
} else {
wg.Add(1)
2015-12-17 03:37:11 +00:00
go func(f *File) {
defer wg.Done()
2015-12-17 03:52:14 +00:00
if proc.Process(s, f) {
2015-12-17 07:16:18 +00:00
dispatch(f)
2015-12-17 05:12:01 +00:00
} else {
gs.decFiles()
2015-12-17 03:37:11 +00:00
}
}(file)
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 {
if s.err = fin.Finalize(s, files); s.err == nil {
for _, file := range files {
s.output <- file
}
}
2015-12-17 03:37:11 +00:00
}
2015-10-31 05:12:03 +00:00
}
2015-12-17 03:37:11 +00:00
func (gs *goldsmith) Chain(p Plugin) Goldsmith {
2015-12-17 03:58:28 +00:00
go gs.chain(gs.newStage(), p)
2015-10-29 14:26:43 +00:00
return gs
2015-10-29 09:24:47 +00:00
}
2015-12-17 03:37:11 +00:00
func (gs *goldsmith) Complete() ([]*File, []error) {
2015-10-31 03:30:41 +00:00
s := gs.stages[len(gs.stages)-1]
2015-11-02 08:11:11 +00:00
var files []*File
2015-10-31 03:30:41 +00:00
for file := range s.output {
2015-12-17 03:37:11 +00:00
gs.exportFile(file)
2015-10-31 03:30:41 +00:00
files = append(files, file)
}
2015-12-17 03:37:11 +00:00
gs.cleanupFiles()
2015-11-11 09:13:57 +00:00
2015-12-17 03:37:11 +00:00
var errs []error
for _, s := range gs.stages {
if s.err != nil {
errs = append(errs, s.err)
}
}
2015-11-11 13:57:49 +00:00
gs.stages = nil
gs.refs = nil
2015-12-17 03:37:11 +00:00
return files, errs
2015-10-29 09:24:47 +00:00
}