protected override void execute() { var scriptRunner = Context.Get<IScriptRunner>(); if(string.IsNullOrWhiteSpace(Token.Value)) return; var commands = Token.Value.Split(new string[]{ " " }, StringSplitOptions.RemoveEmptyEntries); if(commands != null && commands.Length > 1) { var scriptToken = new ScriptToken { Name = commands[1] }; var command = commands[0]; if(command == "abort") scriptRunner.Stop(scriptToken); if(command == "pause") scriptRunner.Pause(scriptToken); if(command == "resume") scriptRunner.Resume(scriptToken); if(command == "vars") scriptRunner.Vars(scriptToken); } TaskSource.TrySetResult(new CompletionEventArgs()); }
public void does_not_try_to_run_script_if_cannot_load() { var token = new ScriptToken(); token.Name = "myscript"; token.Args = new string[]{ "one", "two", "three four" }; token.Id = "myId"; var task = theRunner.Run(token); Assert.IsNull(task); Assert.IsNull(theScript.Script); Assert.IsNull(theScript.Args); }
public void runs_the_script() { const string scriptData = "start:\nput %0\npause 1\ngoto start"; theLoader.AddData("myscript", scriptData); var token = new ScriptToken(); token.Name = "myscript"; token.Args = new string[]{ "one", "two", "three four" }; token.Id = "myId"; var task = theRunner.Run(token); task.Wait(); Assert.AreEqual(token.Name, theScript.Name); Assert.True(token.Args.SequenceEqual(theScript.Args)); Assert.AreEqual(scriptData, theScript.Script); }
public void run_integrated_if_script() { theRunner.Create = theRunner.DefaultCreate; const string scriptData = "\nstart:\n\tif (\"%1\" == \"one\") then\n\t\techo one\n\telse if (\"%2\" == \"two\") then\n\t\techo two\n\telse\n\t\techo three\n\n\tgoto end\n\nend:"; const string expected = "myscript started\npassing label: start\nif (\"one\" == \"one\")\nif result true\necho one\ngoto end\npassing label: end\n"; theLoader.AddData("myscript", scriptData); var token = new ScriptToken(); token.Name = "myscript"; token.Args = new string[]{ "one", "two", "three four" }; token.Id = "myId"; var task = theRunner.Run(token); task.Wait(); task.ContinueWith(t => { Assert.AreEqual(expected, theLogger.Builder.ToString()); Assert.AreEqual(0, theRunner.Scripts().Count()); }); }
public static TokenDefinitionRegistry ClientCommands() { var registry = new TokenDefinitionRegistry(); registry.New(d => { d.Type = "script"; d.Pattern = "^\\.(\\w+)"; d.Ignore = false; d.BuildToken = (source, match, def)=> { var value = source.Substring(match.Groups[1].Index, source.Length - match.Groups[1].Index); var args = source.Substring(match.Groups[1].Index + match.Groups[1].Length, source.Length - (match.Groups[1].Index + match.Groups[1].Length)); var splitArgs = Regex .Matches(args, RegexPatterns.Arguments) .Cast<Match>() .Select(m => m.Groups["match"].Value.Trim('"')) .ToArray(); var token = new ScriptToken { Id = Guid.NewGuid().ToString(), Name = match.Groups[1].Value, Text = source, Type = def.Type, Value = value, Args = splitArgs }; return token; }; }); registry.New(d => { d.Type = "scriptcommand"; d.Pattern = "^#script"; d.Ignore = false; d.BuildToken = (source, match, def)=> { var token = new Token { Text = source, Type = def.Type, Value = source.Substring(match.Index + match.Length, source.Length - (match.Index + match.Length)).Trim() }; return token; }; }); registry.New(d => { d.Type = "send"; d.Pattern = "^#send"; d.Ignore = false; d.BuildToken = (source, match, def)=> { var value = source.Substring(match.Index + match.Length, source.Length - (match.Index + match.Length)).Trim(); var token = new Token { Text = source, Type = def.Type, Value = value }; return token; }; }); registry.New(d => { d.Type = "globalvar"; d.Pattern = "^#var"; d.Ignore = false; d.BuildToken = (source, match, def)=> { var value = source.Substring(match.Index + match.Length, source.Length - (match.Index + match.Length)).Trim(); var token = new Token { Text = source, Type = def.Type, Value = value }; return token; }; }); registry.New(d => { d.Type = "parse"; d.Pattern = "^#parse"; d.Ignore = false; d.BuildToken = (source, match, def)=> { var value = source.Substring(match.Index + match.Length, source.Length - (match.Index + match.Length)).Trim(); var token = new Token { Text = source, Type = def.Type, Value = value }; return token; }; }); return registry; }
public void run_integrated_script() { theRunner.Create = theRunner.DefaultCreate; const string scriptData = "start:\ngoto end\nend:"; const string expected = "myscript started\npassing label: start\ngoto end\npassing label: end\nmyscript finished\n"; theLoader.AddData("myscript", scriptData); var token = new ScriptToken(); token.Name = "myscript"; token.Args = new string[]{ "one", "two", "three four" }; token.Id = "myId"; var task = theRunner.Run(token); task.Wait(); task.ContinueWith(t => { Assert.AreEqual(expected, theLogger.Builder.ToString()); Assert.AreEqual(0, theRunner.Scripts().Count()); }); }
public void Vars(ScriptToken token) { VarsToken = token; }
public void Stop(ScriptToken token) { StopToken = token; }
public Task Run(ScriptToken token) { RunToken = token; return null; }
public void Resume(ScriptToken token) { ResumeToken = token; }
public void Pause(ScriptToken token) { PauseToken = token; }