Skip to content

Commit

Permalink
feat(proxy): add initial sequencing functionality (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Dec 12, 2023
1 parent db2ec13 commit 11fbe2f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 24 deletions.
51 changes: 51 additions & 0 deletions src/app/proxy/execution-sequence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package proxy

import (
"github.com/snivilised/cobrass/src/clif"
"github.com/snivilised/extendio/collections"
)

type Step interface {
Run() error
}

type Sequence interface {
RunAll() error
}

// magickStep knows how to combine parameters together so that the program
// can be invoked correctly; but it does not know how to compose the input
// and output file names; this is the responsibility of the runner, which uses
// the path-finder to accomplish that task.
type magickStep struct { // Step
fileManager *FileManager
program Executor
thirdPartyCL clif.ThirdPartyCommandLine
sourcePath string
outputPath string
journalPath string
}

func (s *magickStep) Run() error {
positional := []string{s.sourcePath}

return s.program.Execute(clif.Expand(positional, s.thirdPartyCL)...)
}

// ExecutionSequence will batch together a list of steps. They are executed within
// the same go routine. The sequence will consist of 1 or more external blocking
// executions. By default, the execution sequence will stop running if the third
// party program returns a non zero result.
type ExecutionSequence struct { // Sequence
iterator collections.Iterator[Step]
}

func (s *ExecutionSequence) RunAll() error {
// this will continue in the presence of errors .... (TODO change the while condition)
var err error
for entry := s.iterator.Start(); s.iterator.Valid(); entry = s.iterator.Next() {
err = entry.Run()
}

return err
}
92 changes: 68 additions & 24 deletions src/app/proxy/item-runners.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package proxy

import (
"github.com/samber/lo"
"github.com/snivilised/cobrass"
"github.com/snivilised/cobrass/src/clif"
"github.com/snivilised/extendio/collections"
"github.com/snivilised/extendio/xfs/nav"
)

Expand All @@ -14,7 +14,8 @@ import (
//

type itemRunner struct {
shared *SharedRunnerInfo
shared *SharedRunnerInfo
exSequence Sequence
}

func (r *itemRunner) Reset() {
Expand All @@ -27,43 +28,86 @@ type SamplerRunner struct {
func (r *SamplerRunner) OnNewShrinkItem(item *nav.TraverseItem,
positional []string,
) error {
_ = item
_ = positional

// foreach profile inside the scheme:
// foreach profileName inside the scheme:
// start off with (*r.shared.profilesCFG)[blurProfile] => copy into another map: current
// foreach args in secondary
// if secondary arg not present in current, add to current
// if secondary arg not present in current, if not already in current
// ---
// - at end, we end up with a map(current) that contains profile args combined with present
// - at end, we end up with a map(current) that contains profileName args combined with present
// - add the current to end of positional to create a clif.ThirdPartyCommandLine: useCL
// to achieve this Overlay needs the KnownBy collection the same way Evaluate does;
// the reason is although we can limit the flags specified inside the config to be
// of their long forms, the arguments present on the command line must be free to be
// either long or short forms, therefore we, need to be able to recognise if a short form
// flag in present is actually in present in the config as its long form.
//
profile := r.shared.Inputs.RootInputs.ProfileFam.Native.Profile
explicit := r.shared.Inputs.ParamSet.Native.ThirdPartySet.LongChangedCL
withArgs := lo.TernaryF(profile != "",
func() clif.ThirdPartyCommandLine {
primary := r.shared.profiles[profile]

return cobrass.Evaluate(
primary,
r.shared.Inputs.ParamSet.Native.ThirdPartySet.KnownBy,
explicit,
)
},
func() clif.ThirdPartyCommandLine {
return explicit
},
)

// get scheme and run for every profile it contains...
profileName := r.shared.Inputs.RootInputs.ProfileFam.Native.Profile
schemeName := r.shared.Inputs.ParamSet.Native.Scheme
//
changed := r.shared.Inputs.ParamSet.Native.ThirdPartySet.LongChangedCL

var steps []Step

switch {
case profileName != "":
cl := r.composeProfileCL(profileName, changed)
step := &magickStep{
program: r.shared.program,
thirdPartyCL: cl,
sourcePath: item.Path,
}
steps = []Step{step}

case schemeName != "":
scheme := r.shared.sampler.Schemes[schemeName]
steps = make([]Step, 0, len(scheme.Profiles))

for _, currentProfileName := range scheme.Profiles {
cl := r.composeProfileCL(currentProfileName, changed)
step := &magickStep{
program: r.shared.program,
thirdPartyCL: cl,
sourcePath: item.Path,
}

return r.shared.program.Execute(clif.Expand(positional, withArgs)...)
steps = append(steps, step)
}

default:
step := &magickStep{
// fileManager
program: r.shared.program,
thirdPartyCL: changed,
sourcePath: item.Path,
// outputPath: ,
// journalPath: ,
}
steps = []Step{step}
}

var zero Step

sequence := ExecutionSequence{
iterator: collections.BeginIt[Step](steps, zero),
}

return sequence.RunAll()
}

func (r *SamplerRunner) composeProfileCL(
profileName string,
secondary clif.ThirdPartyCommandLine,
) clif.ThirdPartyCommandLine {
primary := r.shared.profiles[profileName]

return cobrass.Evaluate(
primary,
r.shared.Inputs.ParamSet.Native.ThirdPartySet.KnownBy,
secondary,
)
}

type FullRunner struct {
Expand Down

0 comments on commit 11fbe2f

Please sign in to comment.