Compare commits

..

5 Commits

Author SHA1 Message Date
42c98df5f7 Initial work on sideload plugin 2024-04-18 22:03:15 -07:00
ab0a930eaa Add depth filter 2024-04-15 20:52:36 -07:00
Alex Yatskov
0e6946b010 Fix paths on windows 2024-04-09 10:34:51 -07:00
818855e875 Fix walk 2024-04-07 13:56:38 -07:00
90326ba081 Move configuration to Goldsmith structure 2024-04-06 22:30:25 -07:00
32 changed files with 229 additions and 19 deletions

View File

@ -10,7 +10,6 @@ type chainState struct {
cache *cache cache *cache
filters filterStack filters filterStack
clean bool
index int index int
errors []error errors []error

View File

@ -15,6 +15,10 @@ func (*fileImporter) Name() string {
func (self *fileImporter) Initialize(context *Context) error { func (self *fileImporter) Initialize(context *Context) error {
return filepath.Walk(self.sourceDir, func(path string, info os.FileInfo, err error) error { return filepath.Walk(self.sourceDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() { if info.IsDir() {
return nil return nil
} }

32
filters/depth/depth.go Normal file
View 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
}

View 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))
},
)
}

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -1,6 +1,7 @@
package wildcard package wildcard
import ( import (
"path/filepath"
"strings" "strings"
"git.foosoft.net/alex/goldsmith" "git.foosoft.net/alex/goldsmith"
@ -26,10 +27,10 @@ func (*Wildcard) Name() string {
} }
func (self *Wildcard) Accept(file *goldsmith.File) bool { func (self *Wildcard) Accept(file *goldsmith.File) bool {
filePath := self.adjustCase(file.Path()) filePath := self.adjustPath(file.Path())
for _, wildcard := range self.wildcards { for _, wildcard := range self.wildcards {
wildcard = self.adjustCase(wildcard) wildcard = self.adjustPath(wildcard)
if matched, _ := doublestar.PathMatch(wildcard, filePath); matched { if matched, _ := doublestar.PathMatch(wildcard, filePath); matched {
return true return true
} }
@ -38,7 +39,9 @@ func (self *Wildcard) Accept(file *goldsmith.File) bool {
return false return false
} }
func (self *Wildcard) adjustCase(str string) string { func (self *Wildcard) adjustPath(str string) string {
str = filepath.FromSlash(str)
if self.caseSensitive { if self.caseSensitive {
return str return str
} }

View File

@ -3,28 +3,23 @@ package goldsmith
// Goldsmith chainable context. // Goldsmith chainable context.
type Goldsmith struct { type Goldsmith struct {
CacheDir string
Clean bool
chain *chainState chain *chainState
} }
// Begin starts a chain, reading the files located in the source directory as input. // Begin starts a chain, reading the files located in the source directory as input.
func (self *Goldsmith) Begin(sourceDir string) *Goldsmith { func (self *Goldsmith) Begin(sourceDir string) *Goldsmith {
self.chain = &chainState{} self.chain = &chainState{}
if len(self.CacheDir) > 0 {
self.chain.cache = &cache{self.CacheDir}
}
self.Chain(&fileImporter{sourceDir: sourceDir}) self.Chain(&fileImporter{sourceDir: sourceDir})
return self 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. // Chain links a plugin instance into the chain.
func (self *Goldsmith) Chain(plugin Plugin) *Goldsmith { func (self *Goldsmith) Chain(plugin Plugin) *Goldsmith {
context := &Context{ context := &Context{
@ -61,7 +56,8 @@ func (self *Goldsmith) FilterPop() *Goldsmith {
// End stops a chain, writing all recieved files to targetDir as output. // End stops a chain, writing all recieved files to targetDir as output.
func (self *Goldsmith) End(targetDir string) []error { 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 { for _, context := range self.chain.contexts {
go context.step() go context.step()
} }

View File

@ -68,8 +68,8 @@ func validate(sourceDir, targetDir, cacheDir, referenceDir string, stager Stagin
} }
func execute(sourceDir, targetDir, cacheDir string, stager StagingCallback) []error { func execute(sourceDir, targetDir, cacheDir string, stager StagingCallback) []error {
var gs goldsmith.Goldsmith gs := goldsmith.Goldsmith{CacheDir: cacheDir, Clean: true}
gs.Begin(sourceDir).Cache(cacheDir).Clean(true) gs.Begin(sourceDir)
stager(&gs) stager(&gs)
return gs.End(targetDir) return gs.End(targetDir)
} }

View 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
}

View 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))
},
)
}

View File

View File