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-11-01 08:06:15 +00:00
|
|
|
"bytes"
|
2015-10-31 04:08:10 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2015-10-29 14:26:43 +00:00
|
|
|
"path/filepath"
|
2015-10-31 05:12:03 +00:00
|
|
|
"sync"
|
2015-10-29 14:26:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type stage struct {
|
2015-11-02 08:11:11 +00:00
|
|
|
input, output chan *File
|
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
|
|
|
|
stages []stage
|
|
|
|
files chan *File
|
2015-11-02 09:07:34 +00:00
|
|
|
refs map[string]bool
|
2015-11-02 08:11:11 +00:00
|
|
|
err error
|
2015-10-29 14:26:43 +00:00
|
|
|
}
|
2015-10-29 13:07:27 +00:00
|
|
|
|
2015-11-02 08:11:11 +00:00
|
|
|
func New(srcDir, dstDir string) Goldsmith {
|
2015-11-02 09:07:34 +00:00
|
|
|
gs := &goldsmith{
|
|
|
|
srcDir: srcDir,
|
|
|
|
dstDir: dstDir,
|
|
|
|
refs: make(map[string]bool),
|
|
|
|
}
|
|
|
|
|
2015-11-02 08:11:11 +00:00
|
|
|
gs.scan()
|
2015-10-31 03:47:06 +00:00
|
|
|
return gs
|
2015-10-29 09:24:47 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 08:11:11 +00:00
|
|
|
func (gs *goldsmith) scan() {
|
2015-11-02 09:07:34 +00:00
|
|
|
fileMatches, _, err := scanDir(gs.srcDir)
|
2015-10-29 14:26:43 +00:00
|
|
|
if err != nil {
|
2015-11-01 03:30:26 +00:00
|
|
|
gs.err = err
|
|
|
|
return
|
2015-10-30 13:05:45 +00:00
|
|
|
}
|
2015-10-29 14:26:43 +00:00
|
|
|
|
2015-11-02 09:07:34 +00:00
|
|
|
s := stage{nil, make(chan *File, len(fileMatches))}
|
2015-11-01 05:09:46 +00:00
|
|
|
defer close(s.output)
|
|
|
|
|
2015-11-02 09:07:34 +00:00
|
|
|
for _, match := range fileMatches {
|
2015-11-02 08:11:11 +00:00
|
|
|
relPath, err := filepath.Rel(gs.srcDir, match)
|
2015-10-29 14:26:43 +00:00
|
|
|
if err != nil {
|
2015-11-01 05:09:46 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-11-02 08:11:11 +00:00
|
|
|
file := &File{
|
2015-11-01 05:09:46 +00:00
|
|
|
Path: relPath,
|
|
|
|
Meta: make(map[string]interface{}),
|
2015-11-01 08:06:15 +00:00
|
|
|
Buff: new(bytes.Buffer),
|
2015-11-01 05:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var f *os.File
|
|
|
|
if f, file.Err = os.Open(match); file.Err == nil {
|
|
|
|
_, file.Err = file.Buff.ReadFrom(f)
|
2015-11-01 07:41:56 +00:00
|
|
|
f.Close()
|
2015-10-29 14:26:43 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 05:09:46 +00:00
|
|
|
s.output <- file
|
2015-10-29 14:26:43 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 13:05:45 +00:00
|
|
|
gs.stages = append(gs.stages, s)
|
2015-10-29 14:26:43 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 09:07:34 +00:00
|
|
|
func (gs *goldsmith) clean() {
|
|
|
|
fileMatches, _, err := scanDir(gs.dstDir)
|
|
|
|
if err != nil {
|
|
|
|
gs.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range fileMatches {
|
|
|
|
relPath, err := filepath.Rel(gs.dstDir, path)
|
|
|
|
if err != nil {
|
|
|
|
gs.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if contained, _ := gs.refs[relPath]; !contained {
|
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
gs.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 04:40:16 +00:00
|
|
|
func (gs *goldsmith) makeStage() stage {
|
2015-11-02 08:11:11 +00:00
|
|
|
s := stage{
|
|
|
|
gs.stages[len(gs.stages)-1].output,
|
|
|
|
make(chan *File),
|
|
|
|
}
|
|
|
|
|
2015-10-29 14:26:43 +00:00
|
|
|
gs.stages = append(gs.stages, s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-11-01 09:27:02 +00:00
|
|
|
func (gs *goldsmith) chainSingle(s stage, cs ChainerSingle, globs []string) {
|
2015-11-01 07:41:56 +00:00
|
|
|
defer close(s.output)
|
2015-10-31 05:12:03 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for file := range s.input {
|
|
|
|
wg.Add(1)
|
2015-11-02 08:11:11 +00:00
|
|
|
go func(f *File) {
|
2015-11-01 07:41:56 +00:00
|
|
|
defer wg.Done()
|
2015-11-02 08:11:11 +00:00
|
|
|
if skipFile(f, globs) {
|
2015-11-01 07:41:56 +00:00
|
|
|
s.output <- f
|
2015-11-01 13:25:26 +00:00
|
|
|
} else {
|
2015-11-02 08:11:11 +00:00
|
|
|
s.output <- cs.ChainSingle(gs, f)
|
2015-11-01 07:41:56 +00:00
|
|
|
}
|
2015-10-31 05:12:03 +00:00
|
|
|
}(file)
|
|
|
|
}
|
|
|
|
|
2015-11-01 07:41:56 +00:00
|
|
|
wg.Wait()
|
2015-10-31 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 09:27:02 +00:00
|
|
|
func (gs *goldsmith) chainMultiple(s stage, cm ChainerMultiple, globs []string) {
|
2015-11-02 08:11:11 +00:00
|
|
|
filtered := make(chan *File)
|
2015-11-01 07:41:56 +00:00
|
|
|
defer close(filtered)
|
|
|
|
|
2015-11-02 08:11:11 +00:00
|
|
|
go cm.ChainMultiple(gs, filtered, s.output)
|
2015-11-01 07:41:56 +00:00
|
|
|
|
|
|
|
for file := range s.input {
|
2015-11-02 08:11:11 +00:00
|
|
|
if skipFile(file, globs) {
|
2015-11-01 07:41:56 +00:00
|
|
|
s.output <- file
|
2015-11-01 13:25:26 +00:00
|
|
|
} else {
|
|
|
|
filtered <- file
|
2015-11-01 07:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-31 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 08:11:11 +00:00
|
|
|
func (gs *goldsmith) SrcDir() string {
|
|
|
|
return gs.srcDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gs *goldsmith) DstDir() string {
|
|
|
|
return gs.dstDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gs *goldsmith) Chain(ctx Config) Goldsmith {
|
2015-11-01 04:11:45 +00:00
|
|
|
if gs.err != nil {
|
|
|
|
return gs
|
|
|
|
}
|
|
|
|
|
2015-11-01 08:06:15 +00:00
|
|
|
if gs.err = ctx.Err; gs.err == nil {
|
2015-11-01 07:41:56 +00:00
|
|
|
switch c := ctx.Chainer.(type) {
|
2015-11-01 04:11:45 +00:00
|
|
|
case ChainerSingle:
|
2015-11-01 09:27:02 +00:00
|
|
|
go gs.chainSingle(gs.makeStage(), c, ctx.Globs)
|
2015-11-01 04:11:45 +00:00
|
|
|
case ChainerMultiple:
|
2015-11-01 09:27:02 +00:00
|
|
|
go gs.chainMultiple(gs.makeStage(), c, ctx.Globs)
|
2015-11-01 03:30:26 +00:00
|
|
|
}
|
2015-10-31 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 14:26:43 +00:00
|
|
|
return gs
|
2015-10-29 09:24:47 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 08:11: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-11-01 05:09:46 +00:00
|
|
|
if file.Err == nil {
|
2015-11-02 08:11:11 +00:00
|
|
|
absPath := filepath.Join(gs.dstDir, file.Path)
|
2015-11-01 05:09:46 +00:00
|
|
|
if file.Err = os.MkdirAll(path.Dir(absPath), 0755); file.Err != nil {
|
2015-11-01 04:11:45 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-11-01 05:09:46 +00:00
|
|
|
var f *os.File
|
2015-11-01 08:06:15 +00:00
|
|
|
if f, file.Err = os.Create(absPath); file.Err == nil {
|
2015-11-02 09:07:34 +00:00
|
|
|
if _, file.Err = f.Write(file.Buff.Bytes()); file.Err == nil {
|
|
|
|
gs.refs[file.Path] = true
|
|
|
|
}
|
|
|
|
|
2015-11-01 05:09:46 +00:00
|
|
|
f.Close()
|
2015-11-01 04:11:45 +00:00
|
|
|
}
|
2015-10-31 03:30:41 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 08:06:15 +00:00
|
|
|
file.Buff = nil
|
2015-10-31 03:30:41 +00:00
|
|
|
files = append(files, file)
|
|
|
|
}
|
|
|
|
|
2015-11-02 09:07:34 +00:00
|
|
|
gs.clean()
|
|
|
|
|
2015-11-01 03:30:26 +00:00
|
|
|
return files, gs.err
|
2015-10-29 09:24:47 +00:00
|
|
|
}
|