~foosoft/goldsmith

769b3073af9d2a340b630e2b6d4bbaf464374227 — Alex Yatskov 9 years ago 5bd898f
WIP
4 files changed, 47 insertions(+), 35 deletions(-)

M cmd/main.go
M file.go
M goldsmith.go
M types.go
M cmd/main.go => cmd/main.go +2 -3
@@ 24,11 24,10 @@ package main

import (
	"github.com/FooSoft/goldsmith"
	_ "github.com/FooSoft/goldsmith-plugins/markdown"
	"github.com/FooSoft/goldsmith-plugins/markdown"
)

func main() {
	gs := goldsmith.NewGoldsmith("/home/alex/projects/website/content/src")
	// gs.Apply(markdown.NewMarkdown()).Complete("/home/alex/projects/test")
	gs.Complete("/home/alex/projects/test")
	gs.Apply(markdown.NewMarkdown()).Complete("/home/alex/projects/test")
}

M file.go => file.go +21 -19
@@ 28,18 28,18 @@ import (
)

type file struct {
	path string
	meta map[string]interface{}
	buff *bytes.Buffer
	err  error
	relPath, srcPath string
	meta             map[string]interface{}
	buff             *bytes.Buffer
	err              error
}

func (f *file) Path() string {
	return f.path
	return f.relPath
}

func (f *file) SetPath(path string) {
	f.path = path
	f.relPath = path
}

func (f *file) Property(key, def string) interface{} {


@@ 63,24 63,26 @@ func (f *file) SetError(err error) {
	f.err = err
}

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

	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
	if len(f.srcPath) > 0 {
		file, err := os.Open(f.srcPath)
		if err != nil {
			f.SetError(err)
			return nil
		}
		defer file.Close()

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

	f.buff = &buff
	return f.buff, nil
	return f.buff
}

M goldsmith.go => goldsmith.go +23 -12
@@ 24,6 24,9 @@ package goldsmith

import (
	"io/ioutil"
	"log"
	"os"
	"path"
	"path/filepath"

	"github.com/bmatcuk/doublestar"


@@ 38,14 41,14 @@ type goldsmith struct {
	files  chan File
}

func NewGoldsmith(path string) Goldsmith {
func NewGoldsmith(src string) Goldsmith {
	gs := new(goldsmith)
	gs.scan(path)
	gs.scan(src)
	return gs
}

func (gs *goldsmith) scan(path string) error {
	matches, err := doublestar.Glob(filepath.Join(path, "**"))
func (gs *goldsmith) scan(srcDir string) error {
	matches, err := doublestar.Glob(filepath.Join(srcDir, "**"))
	if err != nil {
		return err
	}


@@ 56,14 59,15 @@ func (gs *goldsmith) scan(path string) error {
	}

	for _, match := range matches {
		path, err := filepath.Rel(path, match)
		relPath, err := filepath.Rel(srcDir, match)
		if err != nil {
			return err
		}

		s.output <- gs.NewFile(path)
		s.output <- &file{relPath: relPath, srcPath: match}
	}

	close(s.output)
	gs.stages = append(gs.stages, s)
	return nil
}


@@ 78,8 82,8 @@ func (gs *goldsmith) stage() stage {
	return s
}

func (gs *goldsmith) NewFile(path string) File {
	return &file{path: path}
func (gs *goldsmith) NewFile(relPath string) File {
	return &file{relPath: relPath}
}

func (gs *goldsmith) Apply(p Processor) Goldsmith {


@@ 88,18 92,25 @@ func (gs *goldsmith) Apply(p Processor) Goldsmith {
	return gs
}

func (gs *goldsmith) Complete(path string) []File {
func (gs *goldsmith) Complete(dstDir string) []File {
	s := gs.stages[len(gs.stages)-1]

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

		data := file.Data()
		if data == nil {
			continue
		}

		absPath := filepath.Join(dstDir, file.Path())

		if err := os.MkdirAll(path.Dir(absPath), 0755); 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

M types.go => types.go +1 -1
@@ 38,7 38,7 @@ type File interface {
	Error() error
	SetError(err error)

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

type Processor interface {

Do not follow this link