Skip to content

Commit

Permalink
ref(tv,kernel): setup navigator (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Jun 26, 2024
1 parent 9df5319 commit 2601e25
Show file tree
Hide file tree
Showing 21 changed files with 314 additions and 69 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"Errorf",
"exportloopref",
"extendio",
"extention",
"Fastward",
"fieldalignment",
"fortytw",
Expand Down
15 changes: 12 additions & 3 deletions builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@ type buildArtefacts struct {
o *pref.Options
nav core.Navigator
plugins []types.Plugin
ext extent
}

type Builders struct {
fs pref.FsBuilder
options optionsBuilder
navigator kernel.NavigatorBuilder
plugins pluginsBuilder
ext extent
extent extentBuilder
}

func (bs *Builders) buildAll() (*buildArtefacts, error) {
o, optionsErr := bs.options.build(bs.ext)
ext := bs.extent.build(bs.fs.Build())

o, optionsErr := bs.options.build(ext)
if optionsErr != nil {
had := kernel.HadesNav(optionsErr)

return &buildArtefacts{
o: o,
nav: had,
ext: ext,
}, optionsErr
}

Expand All @@ -38,12 +43,13 @@ func (bs *Builders) buildAll() (*buildArtefacts, error) {
return &buildArtefacts{
o: o,
nav: had,
ext: ext,
}, navErr
}

plugins, pluginsErr := bs.plugins.build(o,
artefacts.Mediator,
bs.ext.plugin(artefacts.Mediator),
ext.plugin(artefacts.Mediator),
)

if pluginsErr != nil {
Expand All @@ -52,6 +58,7 @@ func (bs *Builders) buildAll() (*buildArtefacts, error) {
return &buildArtefacts{
o: o,
nav: had,
ext: ext,
}, pluginsErr
}

Expand All @@ -61,6 +68,7 @@ func (bs *Builders) buildAll() (*buildArtefacts, error) {
o: o,
nav: artefacts.Navigator,
plugins: plugins,
ext: ext,
}, bindErr
}
}
Expand All @@ -69,5 +77,6 @@ func (bs *Builders) buildAll() (*buildArtefacts, error) {
o: o,
nav: artefacts.Navigator,
plugins: plugins,
ext: ext,
}, nil
}
11 changes: 7 additions & 4 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ func New(

// Root creates a new node Event which represents the root of directory
// tree to traverse.
func Root() *Node {
event := &Node{
// TODO: complete
func Root(root string, info fs.FileInfo) *Node {
node := &Node{
Path: root,
Info: info,
Children: []fs.DirEntry{},
}
node.dir = isDir(node)

return event
return node
}

// Clone makes shallow copy of Event (excluding the error).
Expand Down
55 changes: 34 additions & 21 deletions director.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tv

import (
"io/fs"
"os"

"github.com/snivilised/traverse/internal/hiber"
Expand Down Expand Up @@ -54,22 +55,28 @@ func activated(o *pref.Options, mediator types.Mediator,
// Prime extent requests that the navigator performs a full
// traversal from the root path specified.
func Prime(using *pref.Using, settings ...pref.Option) *Builders {
navFS := os.DirFS(using.Root)

// TODO: we need to create an aux file system, which is bound
// to a pre-defined location, that will be called upon if
// the navigation session is terminated either by a ctrl-c or
// by a panic.

//
return &Builders{
ext: &primeExtent{
baseExtent: baseExtent{
fsys: fileSystems{
nas: navFS,
fs: pref.FileSystem(func() fs.FS {
if using.GetFS != nil {
return using.GetFS()
}
return os.DirFS(using.Root)
}),
extent: extension(func(fsys fs.FS) extent {
return &primeExtent{
baseExtent: baseExtent{
fsys: fileSystems{
nas: fsys,
},
},
},
u: using,
},
u: using,
}
}),
options: optionals(func(ext extent) (*pref.Options, error) {
if err := using.Validate(); err != nil {
return nil, err
Expand All @@ -84,7 +91,7 @@ func Prime(using *pref.Using, settings ...pref.Option) *Builders {
navigator: kernel.Builder(func(o *pref.Options) (*kernel.Artefacts, error) {
return kernel.New(using, o, &kernel.Benign{}), nil
}),
plugins: features(activated),
plugins: features(activated), // swap over features & activated
}
}

Expand All @@ -93,22 +100,28 @@ func Prime(using *pref.Using, settings ...pref.Option) *Builders {
// as a result of it being terminated prematurely via a ctrl-c
// interrupt.
func Resume(was *Was, settings ...pref.Option) *Builders {
res := os.DirFS(was.From)

// TODO: the navigation file system, baseExtent.sys, will be set for
// resume, only once the resume file has been loaded, as
// its only at this point, we know where the original root
// path was.

//
return &Builders{
ext: &resumeExtent{
baseExtent: baseExtent{
fsys: fileSystems{
res: res,
fs: pref.FileSystem(func() fs.FS {
if was.Using.GetFS != nil {
return was.Using.GetFS()
}
return os.DirFS(was.Using.Root)
}),
extent: extension(func(fsys fs.FS) extent {
return &resumeExtent{
baseExtent: baseExtent{
fsys: fileSystems{
nas: fsys,
},
},
},
w: was,
},
w: was,
}
}),
// we need state; record the hibernation wake point, so
// using a func here is probably not optimal.
//
Expand Down
4 changes: 2 additions & 2 deletions factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (f *walkerFac) Configure() Director {
trunk: trunk{
nav: artefacts.nav,
o: artefacts.o,
extent: bs.ext,
extent: artefacts.ext,
err: err,
},
},
Expand Down Expand Up @@ -72,7 +72,7 @@ func (f *runnerFac) Configure() Director {
trunk: trunk{
nav: artefacts.nav,
o: artefacts.o,
extent: bs.ext,
extent: artefacts.ext,
err: err,
},
wg: f.wg,
Expand Down
26 changes: 21 additions & 5 deletions internal-traverse-defs.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package tv

import (
"github.com/snivilised/traverse/core"
"io/fs"

"github.com/snivilised/traverse/internal/types"
"github.com/snivilised/traverse/pref"
)

// optionsBuilder
type optionsBuilder interface {
build(ext extent) (*pref.Options, error)
}
Expand All @@ -16,6 +18,7 @@ func (fn optionals) build(ext extent) (*pref.Options, error) {
return fn(ext)
}

// pluginsBuilder
type pluginsBuilder interface {
build(*pref.Options, types.Mediator, ...types.Plugin) ([]types.Plugin, error)
}
Expand All @@ -26,11 +29,24 @@ func (fn features) build(o *pref.Options, mediator types.Mediator, others ...typ
return fn(o, mediator, others...)
}

// TODO: do we pass in another func to the directory that represents the sync?
type director func(bs *Builders) core.Navigator
type fsBuilder interface {
build(path string) fs.FS
}

type filesystem func(path string) fs.FS

func (fn filesystem) build(path string) fs.FS {
return fn(path)
}

type extentBuilder interface {
build(fsys fs.FS) extent
}

type extension func(fs.FS) extent

func (fn director) Extent(bs *Builders) core.Navigator {
return fn(bs)
func (fn extension) build(fsys fs.FS) extent {
return fn(fsys)
}

// We need an entity that manages the decoration of the client handler. The
Expand Down
64 changes: 64 additions & 0 deletions internal/kernel/directory-entries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package kernel

import (
"io/fs"

"github.com/snivilised/traverse/enums"
"github.com/snivilised/traverse/internal/lo"
"github.com/snivilised/traverse/pref"
)

func newDirectoryContents(o *pref.Options, entries []fs.DirEntry) *DirectoryContents {
contents := DirectoryContents{
o: o,
}

contents.arrange(entries)

return &contents
}

// DirectoryContents represents the contents of a directory's contents and
// handles sorting order which by default is different between various
// operating systems. This abstraction removes the differences in sorting
// behaviour on different platforms.
type DirectoryContents struct {
Folders []fs.DirEntry
Files []fs.DirEntry
o *pref.Options
}

// All returns the contents of a directory respecting the directory sorting
// order defined in the traversal options.
func (e *DirectoryContents) All() []fs.DirEntry {
result := make([]fs.DirEntry, 0, len(e.Files)+len(e.Folders))

switch e.o.Core.Behaviours.Sort.DirectoryEntryOrder {
case enums.DirectoryContentsOrderFoldersFirst:
result = e.Folders
result = append(result, e.Files...)

case enums.DirectoryContentsOrderFilesFirst:
result = e.Files
result = append(result, e.Folders...)
}

return result
}

func (e *DirectoryContents) arrange(entries []fs.DirEntry) {
grouped := lo.GroupBy(entries, func(entry fs.DirEntry) bool {
return entry.IsDir()
})

e.Folders = grouped[true]
e.Files = grouped[false]

if e.Folders == nil {
e.Folders = []fs.DirEntry{}
}

if e.Files == nil {
e.Files = []fs.DirEntry{}
}
}
Loading

0 comments on commit 2601e25

Please sign in to comment.