Renaming files
This commit is contained in:
parent
16d02310e6
commit
bbd3252541
150
core.go
Normal file
150
core.go
Normal file
@ -0,0 +1,150 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type goldsmith struct {
|
||||
srcDir, dstDir string
|
||||
contexts []*context
|
||||
|
||||
refs map[string]bool
|
||||
refMtx sync.Mutex
|
||||
|
||||
errors []error
|
||||
errorMtx sync.Mutex
|
||||
}
|
||||
|
||||
func (gs *goldsmith) pushContext(plug Plugin) *context {
|
||||
ctx := &context{gs: gs, plug: plug, output: make(chan *file)}
|
||||
if len(gs.contexts) > 0 {
|
||||
ctx.input = gs.contexts[len(gs.contexts)-1].output
|
||||
}
|
||||
|
||||
gs.contexts = append(gs.contexts, ctx)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (gs *goldsmith) cleanupFiles() {
|
||||
files := make(chan string)
|
||||
dirs := make(chan string)
|
||||
go scanDir(gs.dstDir, files, dirs)
|
||||
|
||||
for files != nil || dirs != nil {
|
||||
var (
|
||||
path string
|
||||
ok bool
|
||||
)
|
||||
|
||||
select {
|
||||
case path, ok = <-files:
|
||||
if !ok {
|
||||
files = nil
|
||||
continue
|
||||
}
|
||||
case path, ok = <-dirs:
|
||||
if !ok {
|
||||
dirs = nil
|
||||
continue
|
||||
}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
relPath, err := filepath.Rel(gs.dstDir, path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if contained, _ := gs.refs[relPath]; contained {
|
||||
continue
|
||||
}
|
||||
|
||||
os.RemoveAll(path)
|
||||
}
|
||||
}
|
||||
|
||||
func (gs *goldsmith) exportFile(f *file) error {
|
||||
absPath := filepath.Join(gs.dstDir, f.path)
|
||||
if err := f.export(absPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gs.referenceFile(f.path)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gs *goldsmith) referenceFile(path string) {
|
||||
gs.refMtx.Lock()
|
||||
defer gs.refMtx.Unlock()
|
||||
|
||||
path = cleanPath(path)
|
||||
|
||||
for {
|
||||
gs.refs[path] = true
|
||||
if path == "." {
|
||||
break
|
||||
}
|
||||
|
||||
path = filepath.Dir(path)
|
||||
}
|
||||
}
|
||||
|
||||
func (gs *goldsmith) fault(f *file, err error) {
|
||||
gs.errorMtx.Lock()
|
||||
ferr := &Error{Err: err}
|
||||
if f != nil {
|
||||
ferr.Path = f.path
|
||||
}
|
||||
gs.errors = append(gs.errors, ferr)
|
||||
gs.errorMtx.Unlock()
|
||||
}
|
||||
|
||||
//
|
||||
// Goldsmith Implementation
|
||||
//
|
||||
|
||||
func (gs *goldsmith) Chain(p Plugin) Goldsmith {
|
||||
gs.pushContext(p)
|
||||
return gs
|
||||
}
|
||||
|
||||
func (gs *goldsmith) End(dstDir string) []error {
|
||||
gs.dstDir = dstDir
|
||||
|
||||
for _, ctx := range gs.contexts {
|
||||
go ctx.chain()
|
||||
}
|
||||
|
||||
ctx := gs.contexts[len(gs.contexts)-1]
|
||||
for f := range ctx.output {
|
||||
gs.exportFile(f)
|
||||
}
|
||||
|
||||
gs.cleanupFiles()
|
||||
return gs.errors
|
||||
}
|
176
goldsmith.go
176
goldsmith.go
@ -23,128 +23,80 @@
|
||||
package goldsmith
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"bytes"
|
||||
"io"
|
||||
)
|
||||
|
||||
type goldsmith struct {
|
||||
srcDir, dstDir string
|
||||
contexts []*context
|
||||
|
||||
refs map[string]bool
|
||||
refMtx sync.Mutex
|
||||
|
||||
errors []error
|
||||
errorMtx sync.Mutex
|
||||
type Goldsmith interface {
|
||||
Chain(p Plugin) Goldsmith
|
||||
End(dstDir string) []error
|
||||
}
|
||||
|
||||
func (gs *goldsmith) pushContext(plug Plugin) *context {
|
||||
ctx := &context{gs: gs, plug: plug, output: make(chan *file)}
|
||||
if len(gs.contexts) > 0 {
|
||||
ctx.input = gs.contexts[len(gs.contexts)-1].output
|
||||
}
|
||||
|
||||
gs.contexts = append(gs.contexts, ctx)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (gs *goldsmith) cleanupFiles() {
|
||||
files := make(chan string)
|
||||
dirs := make(chan string)
|
||||
go scanDir(gs.dstDir, files, dirs)
|
||||
|
||||
for files != nil || dirs != nil {
|
||||
var (
|
||||
path string
|
||||
ok bool
|
||||
)
|
||||
|
||||
select {
|
||||
case path, ok = <-files:
|
||||
if !ok {
|
||||
files = nil
|
||||
continue
|
||||
}
|
||||
case path, ok = <-dirs:
|
||||
if !ok {
|
||||
dirs = nil
|
||||
continue
|
||||
}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
relPath, err := filepath.Rel(gs.dstDir, path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if contained, _ := gs.refs[relPath]; contained {
|
||||
continue
|
||||
}
|
||||
|
||||
os.RemoveAll(path)
|
||||
}
|
||||
}
|
||||
|
||||
func (gs *goldsmith) exportFile(f *file) error {
|
||||
absPath := filepath.Join(gs.dstDir, f.path)
|
||||
if err := f.export(absPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gs.referenceFile(f.path)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gs *goldsmith) referenceFile(path string) {
|
||||
gs.refMtx.Lock()
|
||||
defer gs.refMtx.Unlock()
|
||||
|
||||
path = cleanPath(path)
|
||||
|
||||
for {
|
||||
gs.refs[path] = true
|
||||
if path == "." {
|
||||
break
|
||||
}
|
||||
|
||||
path = filepath.Dir(path)
|
||||
}
|
||||
}
|
||||
|
||||
func (gs *goldsmith) fault(f *file, err error) {
|
||||
gs.errorMtx.Lock()
|
||||
ferr := &Error{Err: err}
|
||||
if f != nil {
|
||||
ferr.Path = f.path
|
||||
}
|
||||
gs.errors = append(gs.errors, ferr)
|
||||
gs.errorMtx.Unlock()
|
||||
}
|
||||
|
||||
//
|
||||
// Goldsmith Implementation
|
||||
//
|
||||
|
||||
func (gs *goldsmith) Chain(p Plugin) Goldsmith {
|
||||
gs.pushContext(p)
|
||||
func Begin(srcDir string) Goldsmith {
|
||||
gs := &goldsmith{srcDir: srcDir, refs: make(map[string]bool)}
|
||||
gs.Chain(new(loader))
|
||||
return gs
|
||||
}
|
||||
|
||||
func (gs *goldsmith) End(dstDir string) []error {
|
||||
gs.dstDir = dstDir
|
||||
type File interface {
|
||||
Path() string
|
||||
|
||||
for _, ctx := range gs.contexts {
|
||||
go ctx.chain()
|
||||
Value(key string) (interface{}, bool)
|
||||
SetValue(key string, value interface{})
|
||||
CopyValues(src File)
|
||||
|
||||
Read(p []byte) (int, error)
|
||||
WriteTo(w io.Writer) (int64, error)
|
||||
Seek(offset int64, whence int) (int64, error)
|
||||
}
|
||||
|
||||
ctx := gs.contexts[len(gs.contexts)-1]
|
||||
for f := range ctx.output {
|
||||
gs.exportFile(f)
|
||||
func NewFileFromData(path string, data []byte) File {
|
||||
return &file{
|
||||
path: path,
|
||||
Meta: make(map[string]interface{}),
|
||||
reader: bytes.NewReader(data),
|
||||
}
|
||||
}
|
||||
|
||||
gs.cleanupFiles()
|
||||
return gs.errors
|
||||
func NewFileFromAsset(path, asset string) File {
|
||||
return &file{
|
||||
path: path,
|
||||
Meta: make(map[string]interface{}),
|
||||
asset: asset,
|
||||
}
|
||||
}
|
||||
|
||||
type Context interface {
|
||||
DispatchFile(f File)
|
||||
ReferenceFile(path string)
|
||||
|
||||
SrcDir() string
|
||||
DstDir() string
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
Err error
|
||||
Path string
|
||||
}
|
||||
|
||||
func (e Error) Error() string {
|
||||
return e.Err.Error()
|
||||
}
|
||||
|
||||
type Initializer interface {
|
||||
Initialize(ctx Context) error
|
||||
}
|
||||
|
||||
type Accepter interface {
|
||||
Accept(ctx Context, f File) bool
|
||||
}
|
||||
|
||||
type Processor interface {
|
||||
Process(ctx Context, f File) error
|
||||
}
|
||||
|
||||
type Finalizer interface {
|
||||
Finalize(ctx Context) error
|
||||
}
|
||||
|
||||
type Plugin interface{}
|
||||
|
102
types.go
102
types.go
@ -1,102 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Goldsmith interface {
|
||||
Chain(p Plugin) Goldsmith
|
||||
End(dstDir string) []error
|
||||
}
|
||||
|
||||
func Begin(srcDir string) Goldsmith {
|
||||
gs := &goldsmith{srcDir: srcDir, refs: make(map[string]bool)}
|
||||
gs.Chain(new(loader))
|
||||
return gs
|
||||
}
|
||||
|
||||
type File interface {
|
||||
Path() string
|
||||
|
||||
Value(key string) (interface{}, bool)
|
||||
SetValue(key string, value interface{})
|
||||
CopyValues(src File)
|
||||
|
||||
Read(p []byte) (int, error)
|
||||
WriteTo(w io.Writer) (int64, error)
|
||||
Seek(offset int64, whence int) (int64, error)
|
||||
}
|
||||
|
||||
func NewFileFromData(path string, data []byte) File {
|
||||
return &file{
|
||||
path: path,
|
||||
Meta: make(map[string]interface{}),
|
||||
reader: bytes.NewReader(data),
|
||||
}
|
||||
}
|
||||
|
||||
func NewFileFromAsset(path, asset string) File {
|
||||
return &file{
|
||||
path: path,
|
||||
Meta: make(map[string]interface{}),
|
||||
asset: asset,
|
||||
}
|
||||
}
|
||||
|
||||
type Context interface {
|
||||
DispatchFile(f File)
|
||||
ReferenceFile(path string)
|
||||
|
||||
SrcDir() string
|
||||
DstDir() string
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
Err error
|
||||
Path string
|
||||
}
|
||||
|
||||
func (e Error) Error() string {
|
||||
return e.Err.Error()
|
||||
}
|
||||
|
||||
type Initializer interface {
|
||||
Initialize(ctx Context) error
|
||||
}
|
||||
|
||||
type Accepter interface {
|
||||
Accept(ctx Context, f File) bool
|
||||
}
|
||||
|
||||
type Processor interface {
|
||||
Process(ctx Context, f File) error
|
||||
}
|
||||
|
||||
type Finalizer interface {
|
||||
Finalize(ctx Context) error
|
||||
}
|
||||
|
||||
type Plugin interface{}
|
Loading…
Reference in New Issue
Block a user