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 { type stage struct {
gs *goldsmith
input, output chan *File input, output chan *File
err error err error
} }
@ -40,29 +41,17 @@ type goldsmith struct {
refs map[string]bool refs map[string]bool
} }
func New(srcDir, dstDir string) Goldsmith { func newFile(path string) *File {
gs := &goldsmith{srcDir: srcDir, dstDir: dstDir}
gs.queueFiles()
return gs
}
func (gs *goldsmith) NewFile(path string) *File {
return &File{ return &File{
Path: cleanPath(path), Path: cleanPath(path),
Meta: make(map[string]interface{}), Meta: make(map[string]interface{}),
} }
} }
func (gs *goldsmith) NewFileStatic(path string) *File { func New(srcDir, dstDir string) Goldsmith {
file := gs.NewFile(path) gs := &goldsmith{srcDir: srcDir, dstDir: dstDir}
file.Type = FileStatic gs.queueFiles()
return file return gs
}
func (gs *goldsmith) NewFileRef(path string) *File {
file := gs.NewFile(path)
file.Type = FileReference
return file
} }
func (gs *goldsmith) queueFiles() { func (gs *goldsmith) queueFiles() {
@ -80,7 +69,7 @@ func (gs *goldsmith) queueFiles() {
panic(err) panic(err)
} }
file := gs.NewFile(relPath) file := s.NewFile(relPath)
var f *os.File var f *os.File
if f, file.Err = os.Open(path); file.Err == nil { 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) defer close(s.output)
if init, ok := p.(Initializer); ok { 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 return
} }
} }
@ -180,7 +169,7 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
for file := range s.input { for file := range s.input {
go func(f *File) { go func(f *File) {
defer wg.Done() defer wg.Done()
if proc.Process(gs, f) { if proc.Process(s, f) {
s.output <- f s.output <- f
} }
}(file) }(file)
@ -194,7 +183,7 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
} }
if fin, ok := p.(Finalizer); ok { 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 { func (gs *goldsmith) Chain(p Plugin) Goldsmith {
go gs.chain(gs.makeStage(), p) go gs.chain(gs.makeStage(), p)
return gs return gs
@ -252,43 +233,3 @@ func (gs *goldsmith) Complete() ([]*File, []error) {
return files, errs 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" import "bytes"
type Goldsmith interface { type Goldsmith interface {
Chain(p Plugin, err error) Goldsmith Chain(p Plugin) Goldsmith
Complete() ([]*File, []error) 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
})
}