Compare commits
5 Commits
f5cee22324
...
42c98df5f7
Author | SHA1 | Date | |
---|---|---|---|
42c98df5f7 | |||
ab0a930eaa | |||
|
0e6946b010 | ||
818855e875 | |||
90326ba081 |
@ -10,7 +10,6 @@ type chainState struct {
|
||||
|
||||
cache *cache
|
||||
filters filterStack
|
||||
clean bool
|
||||
index int
|
||||
|
||||
errors []error
|
||||
|
@ -15,6 +15,10 @@ func (*fileImporter) Name() string {
|
||||
|
||||
func (self *fileImporter) Initialize(context *Context) error {
|
||||
return filepath.Walk(self.sourceDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
32
filters/depth/depth.go
Normal file
32
filters/depth/depth.go
Normal file
@ -0,0 +1,32 @@
|
||||
package depth
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.foosoft.net/alex/goldsmith"
|
||||
)
|
||||
|
||||
type Depth struct {
|
||||
depth int
|
||||
}
|
||||
|
||||
func New(depth int) *Depth {
|
||||
return &Depth{depth: depth}
|
||||
}
|
||||
|
||||
func (*Depth) Name() string {
|
||||
return "depth"
|
||||
}
|
||||
|
||||
func (self *Depth) Accept(file *goldsmith.File) bool {
|
||||
if self.depth == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
if parts := strings.Split(file.Path(), string(filepath.Separator)); len(parts) <= self.depth {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
48
filters/depth/depth_test.go
Normal file
48
filters/depth/depth_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
package depth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.foosoft.net/alex/goldsmith"
|
||||
"git.foosoft.net/alex/goldsmith/harness"
|
||||
)
|
||||
|
||||
func Test0(t *testing.T) {
|
||||
harness.ValidateCase(
|
||||
t,
|
||||
"test_0",
|
||||
func(gs *goldsmith.Goldsmith) {
|
||||
gs.FilterPush(New(0))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func Test1(t *testing.T) {
|
||||
harness.ValidateCase(
|
||||
t,
|
||||
"test_1",
|
||||
func(gs *goldsmith.Goldsmith) {
|
||||
gs.FilterPush(New(1))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func Test2(t *testing.T) {
|
||||
harness.ValidateCase(
|
||||
t,
|
||||
"test_2",
|
||||
func(gs *goldsmith.Goldsmith) {
|
||||
gs.FilterPush(New(2))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func Test10(t *testing.T) {
|
||||
harness.ValidateCase(
|
||||
t,
|
||||
"test_10",
|
||||
func(gs *goldsmith.Goldsmith) {
|
||||
gs.FilterPush(New(10))
|
||||
},
|
||||
)
|
||||
}
|
1
filters/depth/testdata/test_0/reference/1.txt
vendored
Normal file
1
filters/depth/testdata/test_0/reference/1.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_0/reference/child/2.txt
vendored
Normal file
1
filters/depth/testdata/test_0/reference/child/2.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_0/reference/child/child/3.txt
vendored
Normal file
1
filters/depth/testdata/test_0/reference/child/child/3.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_0/source/1.txt
vendored
Normal file
1
filters/depth/testdata/test_0/source/1.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_0/source/child/2.txt
vendored
Normal file
1
filters/depth/testdata/test_0/source/child/2.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_0/source/child/child/3.txt
vendored
Normal file
1
filters/depth/testdata/test_0/source/child/child/3.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_1/reference/1.txt
vendored
Normal file
1
filters/depth/testdata/test_1/reference/1.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_1/source/1.txt
vendored
Normal file
1
filters/depth/testdata/test_1/source/1.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_1/source/child/2.txt
vendored
Normal file
1
filters/depth/testdata/test_1/source/child/2.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_1/source/child/child/3.txt
vendored
Normal file
1
filters/depth/testdata/test_1/source/child/child/3.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_10/reference/1.txt
vendored
Normal file
1
filters/depth/testdata/test_10/reference/1.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_10/reference/child/2.txt
vendored
Normal file
1
filters/depth/testdata/test_10/reference/child/2.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_10/reference/child/child/3.txt
vendored
Normal file
1
filters/depth/testdata/test_10/reference/child/child/3.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_10/source/1.txt
vendored
Normal file
1
filters/depth/testdata/test_10/source/1.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_10/source/child/2.txt
vendored
Normal file
1
filters/depth/testdata/test_10/source/child/2.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_10/source/child/child/3.txt
vendored
Normal file
1
filters/depth/testdata/test_10/source/child/child/3.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_2/reference/1.txt
vendored
Normal file
1
filters/depth/testdata/test_2/reference/1.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_2/reference/child/2.txt
vendored
Normal file
1
filters/depth/testdata/test_2/reference/child/2.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_2/source/1.txt
vendored
Normal file
1
filters/depth/testdata/test_2/source/1.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_2/source/child/2.txt
vendored
Normal file
1
filters/depth/testdata/test_2/source/child/2.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
1
filters/depth/testdata/test_2/source/child/child/3.txt
vendored
Normal file
1
filters/depth/testdata/test_2/source/child/child/3.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
|
@ -1,6 +1,7 @@
|
||||
package wildcard
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.foosoft.net/alex/goldsmith"
|
||||
@ -26,10 +27,10 @@ func (*Wildcard) Name() string {
|
||||
}
|
||||
|
||||
func (self *Wildcard) Accept(file *goldsmith.File) bool {
|
||||
filePath := self.adjustCase(file.Path())
|
||||
filePath := self.adjustPath(file.Path())
|
||||
|
||||
for _, wildcard := range self.wildcards {
|
||||
wildcard = self.adjustCase(wildcard)
|
||||
wildcard = self.adjustPath(wildcard)
|
||||
if matched, _ := doublestar.PathMatch(wildcard, filePath); matched {
|
||||
return true
|
||||
}
|
||||
@ -38,7 +39,9 @@ func (self *Wildcard) Accept(file *goldsmith.File) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *Wildcard) adjustCase(str string) string {
|
||||
func (self *Wildcard) adjustPath(str string) string {
|
||||
str = filepath.FromSlash(str)
|
||||
|
||||
if self.caseSensitive {
|
||||
return str
|
||||
}
|
||||
|
22
goldsmith.go
22
goldsmith.go
@ -3,28 +3,23 @@ package goldsmith
|
||||
|
||||
// Goldsmith chainable context.
|
||||
type Goldsmith struct {
|
||||
CacheDir string
|
||||
Clean bool
|
||||
|
||||
chain *chainState
|
||||
}
|
||||
|
||||
// Begin starts a chain, reading the files located in the source directory as input.
|
||||
func (self *Goldsmith) Begin(sourceDir string) *Goldsmith {
|
||||
self.chain = &chainState{}
|
||||
if len(self.CacheDir) > 0 {
|
||||
self.chain.cache = &cache{self.CacheDir}
|
||||
}
|
||||
|
||||
self.Chain(&fileImporter{sourceDir: sourceDir})
|
||||
return self
|
||||
}
|
||||
|
||||
// Cache enables caching in cacheDir for the remainder of the chain.
|
||||
func (self *Goldsmith) Cache(cacheDir string) *Goldsmith {
|
||||
self.chain.cache = &cache{cacheDir}
|
||||
return self
|
||||
}
|
||||
|
||||
// Clean enables or disables removal of leftover files in the target directory.
|
||||
func (self *Goldsmith) Clean(clean bool) *Goldsmith {
|
||||
self.chain.clean = clean
|
||||
return self
|
||||
}
|
||||
|
||||
// Chain links a plugin instance into the chain.
|
||||
func (self *Goldsmith) Chain(plugin Plugin) *Goldsmith {
|
||||
context := &Context{
|
||||
@ -61,7 +56,8 @@ func (self *Goldsmith) FilterPop() *Goldsmith {
|
||||
|
||||
// End stops a chain, writing all recieved files to targetDir as output.
|
||||
func (self *Goldsmith) End(targetDir string) []error {
|
||||
self.Chain(&fileExporter{targetDir: targetDir, clean: self.chain.clean})
|
||||
self.Chain(&fileExporter{targetDir: targetDir, clean: self.Clean})
|
||||
|
||||
for _, context := range self.chain.contexts {
|
||||
go context.step()
|
||||
}
|
||||
|
@ -68,8 +68,8 @@ func validate(sourceDir, targetDir, cacheDir, referenceDir string, stager Stagin
|
||||
}
|
||||
|
||||
func execute(sourceDir, targetDir, cacheDir string, stager StagingCallback) []error {
|
||||
var gs goldsmith.Goldsmith
|
||||
gs.Begin(sourceDir).Cache(cacheDir).Clean(true)
|
||||
gs := goldsmith.Goldsmith{CacheDir: cacheDir, Clean: true}
|
||||
gs.Begin(sourceDir)
|
||||
stager(&gs)
|
||||
return gs.End(targetDir)
|
||||
}
|
||||
|
85
plugins/sideload/sideload.go
Normal file
85
plugins/sideload/sideload.go
Normal file
@ -0,0 +1,85 @@
|
||||
package sideload
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"path/filepath"
|
||||
|
||||
"git.foosoft.net/alex/goldsmith"
|
||||
)
|
||||
|
||||
type Sideload struct {
|
||||
files []*goldsmith.File
|
||||
fileSystems []embed.FS
|
||||
}
|
||||
|
||||
func New() *Sideload {
|
||||
return &Sideload{}
|
||||
}
|
||||
|
||||
func (*Sideload) Name() string {
|
||||
return "sideload"
|
||||
}
|
||||
|
||||
func (self *Sideload) Finalize(context *goldsmith.Context) error {
|
||||
files := self.files
|
||||
|
||||
for _, fileSystem := range self.fileSystems {
|
||||
currFiles, err := self.gatherFsFiles(context, fileSystem, ".")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
files = append(files, currFiles...)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
context.DispatchFile(file)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Sideload) Files(files ...*goldsmith.File) *Sideload {
|
||||
self.files = append(self.files, files...)
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *Sideload) FileSystems(fileSystems ...embed.FS) *Sideload {
|
||||
self.fileSystems = append(self.fileSystems, fileSystems...)
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *Sideload) gatherFsFiles(context *goldsmith.Context, fileSystem embed.FS, path string) ([]*goldsmith.File, error) {
|
||||
entries, err := fileSystem.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var files []*goldsmith.File
|
||||
for _, entry := range entries {
|
||||
currPath := filepath.Join(path, entry.Name())
|
||||
if entry.IsDir() {
|
||||
currFiles, err := self.gatherFsFiles(context, fileSystem, currPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
files = append(files, currFiles...)
|
||||
} else {
|
||||
data, err := fileSystem.ReadFile(currPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
file, err := context.CreateFileFromReader(currPath, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
files = append(files, file)
|
||||
}
|
||||
}
|
||||
|
||||
return files, nil
|
||||
}
|
22
plugins/sideload/sideload_test.go
Normal file
22
plugins/sideload/sideload_test.go
Normal file
@ -0,0 +1,22 @@
|
||||
package sideload
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"embed"
|
||||
|
||||
"git.foosoft.net/alex/goldsmith"
|
||||
"git.foosoft.net/alex/goldsmith/harness"
|
||||
)
|
||||
|
||||
//go:embed testdata/source
|
||||
var embedFs embed.FS
|
||||
|
||||
func Test(self *testing.T) {
|
||||
harness.Validate(
|
||||
self,
|
||||
func(gs *goldsmith.Goldsmith) {
|
||||
gs.Chain(New().FileSystems(embedFs))
|
||||
},
|
||||
)
|
||||
}
|
0
plugins/sideload/testdata/source/1.txt
vendored
Normal file
0
plugins/sideload/testdata/source/1.txt
vendored
Normal file
0
plugins/sideload/testdata/source/child/2.txt
vendored
Normal file
0
plugins/sideload/testdata/source/child/2.txt
vendored
Normal file
Loading…
Reference in New Issue
Block a user