This commit is contained in:
Alex Yatskov 2015-12-17 12:52:14 +09:00
parent 37539daaf6
commit fdc5126a1e
4 changed files with 128 additions and 70 deletions

View File

@ -30,6 +30,7 @@ import (
)
type stage struct {
gs *goldsmith
input, output chan *File
err error
}
@ -40,29 +41,17 @@ type goldsmith struct {
refs map[string]bool
}
func New(srcDir, dstDir string) Goldsmith {
gs := &goldsmith{srcDir: srcDir, dstDir: dstDir}
gs.queueFiles()
return gs
}
func (gs *goldsmith) NewFile(path string) *File {
func newFile(path string) *File {
return &File{
Path: cleanPath(path),
Meta: make(map[string]interface{}),
}
}
func (gs *goldsmith) NewFileStatic(path string) *File {
file := gs.NewFile(path)
file.Type = FileStatic
return file
}
func (gs *goldsmith) NewFileRef(path string) *File {
file := gs.NewFile(path)
file.Type = FileReference
return file
func New(srcDir, dstDir string) Goldsmith {
gs := &goldsmith{srcDir: srcDir, dstDir: dstDir}
gs.queueFiles()
return gs
}
func (gs *goldsmith) queueFiles() {
@ -80,7 +69,7 @@ func (gs *goldsmith) queueFiles() {
panic(err)
}
file := gs.NewFile(relPath)
file := s.NewFile(relPath)
var f *os.File
if f, file.Err = os.Open(path); file.Err == nil {
@ -170,7 +159,7 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
defer close(s.output)
if init, ok := p.(Initializer); ok {
if s.err = init.Initialize(gs); s.err != nil {
if s.err = init.Initialize(s); s.err != nil {
return
}
}
@ -180,7 +169,7 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
for file := range s.input {
go func(f *File) {
defer wg.Done()
if proc.Process(gs, f) {
if proc.Process(s, f) {
s.output <- f
}
}(file)
@ -194,7 +183,7 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
}
if fin, ok := p.(Finalizer); ok {
s.err = fin.Finalize(gs)
s.err = fin.Finalize(s)
}
}
@ -215,14 +204,6 @@ func (gs *goldsmith) refFile(path string) {
}
}
func (gs *goldsmith) SrcDir() string {
return gs.srcDir
}
func (gs *goldsmith) DstDir() string {
return gs.dstDir
}
func (gs *goldsmith) Chain(p Plugin) Goldsmith {
go gs.chain(gs.makeStage(), p)
return gs
@ -252,43 +233,3 @@ func (gs *goldsmith) Complete() ([]*File, []error) {
return files, errs
}
func cleanPath(path string) string {
if filepath.IsAbs(path) {
var err error
if path, err = filepath.Rel("/", path); err != nil {
panic(err)
}
}
return filepath.Clean(path)
}
func scanDir(root string, files, dirs chan string) {
defer func() {
if files != nil {
close(files)
}
if dirs != nil {
close(dirs)
}
}()
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if dirs != nil {
dirs <- path
}
} else {
if files != nil {
files <- path
}
}
return nil
})
}

49
stage.go Normal file
View File

@ -0,0 +1,49 @@
/*
* 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
func (s *stage) NewFile(path string) *File {
file := newFile(path)
s.output <- file
return file
}
func (s *stage) NewFileStatic(path string) *File {
file := s.NewFile(path)
file.Type = FileStatic
return file
}
func (s *stage) NewFileRef(path string) *File {
file := s.NewFile(path)
file.Type = FileReference
return file
}
func (s *stage) SrcDir() string {
return s.gs.srcDir
}
func (s *stage) DstDir() string {
return s.gs.dstDir
}

View File

@ -25,7 +25,7 @@ package goldsmith
import "bytes"
type Goldsmith interface {
Chain(p Plugin, err error) Goldsmith
Chain(p Plugin) Goldsmith
Complete() ([]*File, []error)
}

68
util.go Normal file
View File

@ -0,0 +1,68 @@
/*
* 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
import (
"os"
"path/filepath"
)
func cleanPath(path string) string {
if filepath.IsAbs(path) {
var err error
if path, err = filepath.Rel("/", path); err != nil {
panic(err)
}
}
return filepath.Clean(path)
}
func scanDir(root string, files, dirs chan string) {
defer func() {
if files != nil {
close(files)
}
if dirs != nil {
close(dirs)
}
}()
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if dirs != nil {
dirs <- path
}
} else {
if files != nil {
files <- path
}
}
return nil
})
}