Skip to content

Commit

Permalink
Variables can be used in args
Browse files Browse the repository at this point in the history
  • Loading branch information
teplofizik committed Nov 10, 2022
1 parent bdfcbd7 commit 06de5a1
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 3 deletions.
37 changes: 37 additions & 0 deletions NyaFs/Processor/Scripting/Commands/Echo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NyaFs.Processor.Scripting.Commands
{
public class Echo : ScriptStepGenerator
{
public Echo() : base("echo")
{
AddConfig(new ScriptArgsConfig(0, null));
}

public override ScriptStep Get(ScriptArgs Args)
{
return new EchoScriptStep(Args.RawArgs);
}

public class EchoScriptStep : ScriptStep
{
private string[] Args;

public EchoScriptStep(string[] Args) : base("echo")
{
this.Args = Args;
}

public override ScriptStepResult Exec(ImageProcessor Processor)
{
Log.Write(0, String.Join(" ", Args));

return new ScriptStepResult(ScriptStepStatus.Ok, null);
}
}
}
}
2 changes: 2 additions & 0 deletions NyaFs/Processor/Scripting/Commands/Var.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public Var() : base("var")
new Params.StringScriptArgsParam("name") }));
}

public override int[] MaskedArgs => new int[] { 0 };

public override ScriptStep Get(ScriptArgs Args)
{
if(Args.ArgConfig == 0)
Expand Down
1 change: 1 addition & 0 deletions NyaFs/Processor/Scripting/ScriptBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ScriptBaseAll : ScriptBase
public ScriptBaseAll()
{
Add(new Commands.Include());
Add(new Commands.Echo());

Add(new Commands.Load());
Add(new Commands.Store());
Expand Down
24 changes: 21 additions & 3 deletions NyaFs/Processor/Scripting/ScriptStep.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NyaFs.Processor.Scripting
Expand All @@ -22,22 +23,39 @@ public ScriptStep(string Name)
this.Name = Name;
}


private string PreprocessArg(Variables.VariableScope Scope, string Arg)
{
if (Variables.VariableChecker.IsCorrectName(Arg) && Scope.IsDefined(Arg))
return Scope.GetValue(Arg);
else
{
var Vars = Variables.VariableChecker.ExtractVariables(Arg);
foreach(var V in Vars)
{
if (Scope.IsDefined(V))
Arg = Arg.Replace(V, Scope.GetValue(V));
}
return Arg;
}
}

private string[] PreprocessArgs(Variables.VariableScope Scope, string[] Args)
private string[] PreprocessArgs(Variables.VariableScope Scope, string[] Args, int[] Masked)
{
return Array.ConvertAll(Args, A => PreprocessArg(Scope, A));
var Res = new string[Args.Length];
for(int i = 0; i < Args.Length; i++)
{
if (Masked.Contains(i))
Res[i] = Args[i];
else
Res[i] = PreprocessArg(Scope, Args[i]);
}
return Res;
}

public ScriptStep GetPreprocessed(Variables.VariableScope Scope)
{
var SArgs = Generator.GetArgs(PreprocessArgs(Scope, GeneratorArgs));
var SArgs = Generator.GetArgs(PreprocessArgs(Scope, GeneratorArgs, Generator.MaskedArgs));
if (SArgs != null)
{
var Step = Generator.Get(SArgs);
Expand Down
5 changes: 5 additions & 0 deletions NyaFs/Processor/Scripting/ScriptStepGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ public virtual ScriptStep Get(ScriptArgs Args)
throw new NotImplementedException("There is need to implement script step getter");
}

public virtual int[] MaskedArgs => new int[] { };

public ScriptArgs GetArgs(string[] Args)
{
foreach(var C in Configs)
{
if(C.Params == null)
return new ScriptArgs(C.Id, Args);

if (CheckConfig(C, Args))
{
if (!C.CheckArgs(Args))
Expand Down
16 changes: 16 additions & 0 deletions NyaFs/Processor/Scripting/Variables/VariableChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,21 @@ public static bool IsCorrectName(string Name)
{
return (Name != null) && (Name.Length > 2) && (Name.Count(C => (C == '$')) == 1) && (Name[0] == '$');
}

public static string[] ExtractVariables(string Text)
{
var Res = new List<string>();
string Temp = null;

var Parts = Text.Split(new char[] { ' ', '\t', ',', ':', '\\', '/', '>', '<', '?', '!', '*', '*', '%', '#', '@', '~' });

foreach(var P in Parts)
{
if (IsCorrectName(P))
Res.Add(P);
}

return Res.ToArray();
}
}
}
43 changes: 43 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ Reset program state, drop all images:
reset
```

Echo any text:
```
echo <text>
```

## Commands for image loading
Default load command (type and image format autodetect):
```
Expand Down Expand Up @@ -367,6 +372,44 @@ List items in other dir:
ls <path>
```

## Variables
Variables can be defined with command:
```
var <name> <value>
```
or
```
var <name>
```
Variable names starts with $: $image, $path etc.

Variables can be used instead parameters:
```
var $image zynq.fit
load $image
```

Variables can be used in conditions. If variable defined, command line will be executed.
```
<var>? <command> <args...>
```
Example:
```
# Define image1 variable
var $image1
# Update some config based on condition:
$image1? file etc/config.cfg files/image1/config.cfg
$image2? file etc/config.cfg files/image2/config.cfg
```

As variant, variables can contain part of path:
```
var $image image1
echo Image is $image
file etc/config.cfg files/$image/config.cfg
```

## Plugins
Plugin is a dll that contain adiitional functionality.
Available plugins:
Expand Down

0 comments on commit 06de5a1

Please sign in to comment.