goldsmith/goldsmith.go

130 lines
2.9 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-08-21 19:54:44 +00:00
"fmt"
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, filters ...string) Goldsmith
2016-01-13 03:21:30 +00:00
End(dstDir string) []error
}
2015-12-18 11:30:14 +00:00
func Begin(srcDir string, filters ...string) Goldsmith {
2016-01-13 03:21:30 +00:00
gs := &goldsmith{srcDir: srcDir, refs: make(map[string]bool)}
gs.Chain(new(loader), filters...)
2016-01-13 03:21:30 +00:00
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 {
2016-08-21 19:54:44 +00:00
Name string
2016-01-13 03:21:30 +00:00
Path string
2016-08-21 19:54:44 +00:00
Err error
2015-12-18 04:37:32 +00:00
}
2016-01-13 03:21:30 +00:00
func (e Error) Error() string {
2016-08-21 19:54:44 +00:00
var path string
if len(e.Path) > 0 {
path = "@" + e.Path
}
return fmt.Sprintf("[%s%s]: %s", e.Name, path, e.Err.Error())
2015-10-29 09:24:47 +00:00
}
2016-01-13 03:21:30 +00:00
type Initializer interface {
2016-07-10 20:05:23 +00:00
Initialize(ctx Context) ([]string, error)
2016-01-13 03:21:30 +00:00
}
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
2016-08-21 19:54:44 +00:00
type Plugin interface {
Name() string
}