goldsmith/goldsmith.go

152 lines
3.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-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-10-29 14:26:43 +00:00
)
type goldsmith struct {
2015-11-02 08:11:11 +00:00
srcDir, dstDir string
2015-12-20 08:58:50 +00:00
contexts []*context
2015-12-18 11:30:14 +00:00
refs map[string]bool
refMtx sync.Mutex
2015-12-20 08:38:53 +00:00
errors []error
errorMtx sync.Mutex
2015-12-18 04:14:39 +00:00
}
2015-12-20 08:38:53 +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-20 08:58:50 +00:00
ctx := newContext(gs)
2015-11-01 05:09:46 +00:00
2015-11-03 01:13:04 +00:00
go func() {
2015-12-20 08:58:50 +00:00
defer close(ctx.output)
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-18 10:19:43 +00:00
f := NewFileFromAsset(relPath, path)
2015-12-20 08:58:50 +00:00
ctx.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-19 12:01:54 +00:00
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 {
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 11:30:14 +00:00
gs.refMtx.Lock()
defer gs.refMtx.Unlock()
2015-12-17 07:32:21 +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-20 08:38:53 +00:00
func (gs *goldsmith) fault(f *file, err error) {
gs.errorMtx.Lock()
2015-12-21 11:18:09 +00:00
gs.errors = append(gs.errors, &Error{f, err})
2015-12-20 08:38:53 +00:00
gs.errorMtx.Unlock()
2015-12-18 04:37:32 +00:00
}
//
// Goldsmith Implementation
//
2015-12-17 03:37:11 +00:00
func (gs *goldsmith) Chain(p Plugin) Goldsmith {
2015-12-20 08:58:50 +00:00
ctx := newContext(gs)
go ctx.chain(p)
2015-10-29 14:26:43 +00:00
return gs
2015-10-29 09:24:47 +00:00
}
2015-12-20 08:38:53 +00:00
func (gs *goldsmith) Complete() []error {
2015-12-20 08:58:50 +00:00
ctx := gs.contexts[len(gs.contexts)-1]
for f := range ctx.output {
2015-12-18 04:14:39 +00:00
gs.exportFile(f)
2015-10-31 03:30:41 +00:00
}
2015-12-17 03:37:11 +00:00
gs.cleanupFiles()
2015-12-20 08:38:53 +00:00
return gs.errors
2015-10-29 09:24:47 +00:00
}