goldsmith/goldsmith.go

125 lines
2.8 KiB
Go
Raw Normal View History

2015-10-29 09:24:47 +00:00
/*
* 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
2015-10-29 14:26:43 +00:00
import (
2016-01-13 03:21:30 +00:00
"bytes"
2016-06-12 02:38:17 +00:00
"errors"
2016-01-13 03:21:30 +00:00
"io"
2016-06-11 23:24:06 +00:00
"os"
"time"
2015-10-29 14:26:43 +00:00
)
2016-01-13 03:21:30 +00:00
type Goldsmith interface {
Chain(p Plugin) Goldsmith
End(dstDir string) []error
}
2015-12-18 11:30:14 +00:00
2016-01-13 03:21:30 +00:00
func Begin(srcDir string) Goldsmith {
gs := &goldsmith{srcDir: srcDir, refs: make(map[string]bool)}
gs.Chain(new(loader))
return gs
2015-12-18 04:14:39 +00:00
}
2016-01-13 03:21:30 +00:00
type File interface {
Path() string
2016-06-11 23:24:06 +00:00
Name() string
2016-06-06 05:02:09 +00:00
Dir() string
2016-06-11 23:24:06 +00:00
Ext() string
Size() int64
ModTime() time.Time
2016-01-13 03:21:30 +00:00
Value(key string) (interface{}, bool)
SetValue(key string, value interface{})
CopyValues(src File)
2015-11-01 05:09:46 +00:00
2016-01-13 03:21:30 +00:00
Read(p []byte) (int, error)
WriteTo(w io.Writer) (int64, error)
Seek(offset int64, whence int) (int64, error)
2015-10-29 14:26:43 +00:00
}
2016-01-13 03:21:30 +00:00
func NewFileFromData(path string, data []byte) File {
return &file{
2016-06-11 23:24:06 +00:00
path: path,
Meta: make(map[string]interface{}),
reader: bytes.NewReader(data),
size: int64(len(data)),
modTime: time.Now(),
2015-11-02 09:07:34 +00:00
}
}
2016-06-11 23:24:06 +00:00
func NewFileFromAsset(path, asset string) (File, error) {
info, err := os.Stat(asset)
if err != nil {
return nil, err
}
2016-06-12 02:38:17 +00:00
if info.IsDir() {
return nil, errors.New("assets must be files")
}
2016-06-11 23:24:06 +00:00
f := &file{
path: path,
Meta: make(map[string]interface{}),
size: info.Size(),
modTime: info.ModTime(),
asset: asset,
2015-11-02 09:23:13 +00:00
}
2016-06-11 23:24:06 +00:00
return f, nil
2015-11-02 09:23:13 +00:00
}
2016-01-13 03:21:30 +00:00
type Context interface {
DispatchFile(f File)
2015-12-17 03:37:11 +00:00
2016-01-13 03:21:30 +00:00
SrcDir() string
DstDir() string
2015-10-31 05:12:03 +00:00
}
2016-01-13 03:21:30 +00:00
type Error struct {
Err error
Path string
2015-12-18 04:37:32 +00:00
}
2016-01-13 03:21:30 +00:00
func (e Error) Error() string {
return e.Err.Error()
2015-10-29 09:24:47 +00:00
}
2016-01-13 03:21:30 +00:00
type Initializer interface {
Initialize(ctx Context) error
}
2016-01-10 11:17:35 +00:00
2016-01-13 03:21:30 +00:00
type Accepter interface {
Accept(ctx Context, f File) bool
}
2016-01-10 11:17:35 +00:00
2016-01-13 03:21:30 +00:00
type Processor interface {
Process(ctx Context, f File) error
}
2015-10-31 03:30:41 +00:00
2016-01-13 03:21:30 +00:00
type Finalizer interface {
Finalize(ctx Context) error
2015-10-29 09:24:47 +00:00
}
2016-01-13 03:21:30 +00:00
type Plugin interface{}