~foosoft/goldsmith

619e3dfd410269a8e4ca4988b5afcba09e569241 — Alex Yatskov 9 years ago 5327d67
WIP
3 files changed, 40 insertions(+), 16 deletions(-)

M file.go
M goldsmith.go
M types.go
M file.go => file.go +11 -0
@@ 31,6 31,7 @@ type file struct {
	path string
	meta map[string]interface{}
	buff *bytes.Buffer
	err  error
}

func (f *file) Path() string {


@@ 50,6 51,14 @@ func (f *file) SetProperty(key string, value interface{}) {
	f.meta[key] = value
}

func (f *file) Error() error {
	return f.err
}

func (f *file) SetError(err error) {
	f.err = err
}

func (f *file) Data() (*bytes.Buffer, error) {
	if f.buff != nil {
		return f.buff, nil


@@ 57,12 66,14 @@ func (f *file) Data() (*bytes.Buffer, error) {

	file, err := os.Open(f.path)
	if err != nil {
		f.SetError(err)
		return nil, err
	}
	defer file.Close()

	var buff bytes.Buffer
	if _, err := buff.ReadFrom(file); err != nil {
		f.SetError(err)
		return nil, err
	}


M goldsmith.go => goldsmith.go +25 -15
@@ 23,21 23,19 @@
package goldsmith

import (
	"io/ioutil"
	"path/filepath"
	"sync"

	"github.com/bmatcuk/doublestar"
)

type stage struct {
	input  chan File
	output chan File
	input, output chan File
}

type goldsmith struct {
	stages []stage
	files  chan File
	wg     sync.WaitGroup
}

func NewGoldsmith(path string) (Goldsmith, error) {


@@ 84,22 82,34 @@ func (gs *goldsmith) stage() stage {
}

func (gs *goldsmith) NewFile(path string) File {
	return &file{path, make(map[string]interface{}), nil}
	return &file{path: path}
}

func (gs *goldsmith) Apply(p Processor) Goldsmith {
	s := gs.stage()

	gs.wg.Add(1)
	go func() {
		p.Process(gs, s.input, s.output)
		gs.wg.Done()
	}()

	go p.Process(gs, s.input, s.output)
	return gs
}

func (gs *goldsmith) Complete(path string) error {
	gs.wg.Wait()
	return nil
func (gs *goldsmith) Complete(path string) []File {
	s := gs.stages[len(gs.stages)-1]

	var files []File
	for file := range s.output {
		data, err := file.Data()
		if err != nil {
			file.SetError(err)
			continue
		}

		absPath := filepath.Join(path, file.Path())
		if err := ioutil.WriteFile(absPath, data.Bytes(), 0644); err != nil {
			file.SetError(err)
			continue
		}

		files = append(files, file)
	}

	return files
}

M types.go => types.go +4 -1
@@ 35,6 35,9 @@ type File interface {
	Property(key string) (interface{}, bool)
	SetProperty(key string, value interface{})

	Error() error
	SetError(err error)

	Data() (*bytes.Buffer, error)
}



@@ 44,5 47,5 @@ type Processor interface {

type Goldsmith interface {
	Apply(p Processor) Goldsmith
	Complete(path string) error
	Complete(path string) []File
}

Do not follow this link