Improved file handling

This commit is contained in:
Alex Yatskov 2015-11-09 13:35:24 +09:00
parent 449384a02d
commit c727a21239
2 changed files with 26 additions and 26 deletions

View File

@ -23,8 +23,6 @@
package goldsmith package goldsmith
import ( import (
"bytes"
"fmt"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -41,7 +39,6 @@ type stage struct {
type goldsmith struct { type goldsmith struct {
srcDir, dstDir string srcDir, dstDir string
stages []stage stages []stage
files chan *File
refs map[string]bool refs map[string]bool
err error err error
} }
@ -74,7 +71,7 @@ func (gs *goldsmith) scanFs() error {
panic(err) panic(err)
} }
file, _ := gs.NewFile(relPath) file := gs.NewFile(relPath)
var f *os.File var f *os.File
if f, file.Err = os.Open(match); file.Err == nil { if f, file.Err = os.Open(match); file.Err == nil {
@ -116,7 +113,7 @@ func (gs *goldsmith) cleanupFiles() error {
} }
func (gs *goldsmith) exportFile(file *File) { func (gs *goldsmith) exportFile(file *File) {
defer func() { file.Buff = nil }() defer file.Buff.Reset()
if file.Err != nil { if file.Err != nil {
return return
@ -163,41 +160,44 @@ func (gs *goldsmith) chain(s stage, c Chainer) {
} }
} }
func (gs *goldsmith) NewFile(path string) (*File, error) { func (gs *goldsmith) NewFile(path string) *File {
if filepath.IsAbs(path) { if filepath.IsAbs(path) {
return nil, fmt.Errorf("absolute paths are not supported: %s", path) var err error
path, err = filepath.Rel("/", path)
if err != nil {
panic(err)
}
} }
file := &File{ return &File{
Path: path, Path: filepath.Clean(path),
Meta: make(map[string]interface{}), Meta: make(map[string]interface{}),
Buff: new(bytes.Buffer),
} }
return file, nil
} }
func (gs *goldsmith) NewFileStatic(path string) (*File, error) { func (gs *goldsmith) NewFileStatic(path string) *File {
file, err := gs.NewFile(path) file := gs.NewFile(path)
if err != nil {
return nil, err
}
file.flags |= FileFlagStatic file.flags |= FileFlagStatic
return file, nil return file
} }
func (gs *goldsmith) RefFile(path string) error { func (gs *goldsmith) RefFile(path string) {
if filepath.IsAbs(path) { if filepath.IsAbs(path) {
return fmt.Errorf("absolute paths are not supported: %s", path) var err error
path, err = filepath.Rel("/", path)
if err != nil {
panic(err)
}
} }
path = filepath.Clean(path) path = filepath.Clean(path)
for { for {
gs.refs[path] = true gs.refs[path] = true
if path == "." { if path == "." {
return nil break
} }
path = filepath.Dir(path) path = filepath.Dir(path)
} }
} }

View File

@ -40,17 +40,17 @@ type Filterer interface {
type File struct { type File struct {
Path string Path string
Meta map[string]interface{} Meta map[string]interface{}
Buff *bytes.Buffer Buff bytes.Buffer
Err error Err error
flags uint32 flags uint32
} }
type Context interface { type Context interface {
NewFileStatic(path string) (*File, error) NewFileStatic(path string) *File
NewFile(path string) (*File, error) NewFile(path string) *File
RefFile(path string) error RefFile(path string)
SrcDir() string SrcDir() string
DstDir() string DstDir() string