private string captureOutput(string code) { var capture = new FlexibleCaptureStream(); UnixShell shell = new UnixShell(); shell.Environment.Output.PipeTo(capture); shell.Environment.Error.PipeTo(capture); try { Task.Run(async() => await shell.RunScriptAsync(code: code)).Wait(); } catch (Exception ex) { Log.Error(ex); } var result = capture.Result; result = result.Replace(Environment.NewLine, "\n"); return(result); }
public async Task Run(string[] args) { fixFileAssociations(); DesktopPlatform.LogTargets.StandardOutput = false; // option values bool help = false; Mode mode = Mode.Interactive; string commandString = null; string scriptFile = null; List <string> parameters = new List <string> (); // option parser OptionSet optionSet = new OptionSet(); optionSet.Add("h|help|?", "Prints out the options.", option => help = (option != null)); optionSet.Add("log|debug", "Show log messages.", option => DesktopPlatform.LogTargets.StandardOutput = option != null); optionSet.Add("c=", "Execute a command string.", option => { mode = Mode.CommandString; commandString = option; }); optionSet.Add("<>", option => { if (mode != Mode.CommandString && scriptFile == null) { mode = Mode.ScriptFile; scriptFile = option; Log.Warning("Script file: ", option); } else { parameters.Add(option); Log.Warning("Parameter: ", option); } }); try { optionSet.Parse(args); } catch (OptionException) { help = true; } // need help ? if (help) { printOptions(optionSet); return; } RegularFileSystems.Register(); RegularExecutables.Register(); UnixShell shell = new UnixShell(); shell.Environment.Output.PipeTo(NonBlockingConsole.Instance); shell.Environment.Error.PipeTo(NonBlockingConsole.Instance); // run code line if (mode == Mode.CommandString) { try { await shell.RunScriptAsync(code : commandString); } catch (Exception ex) { Log.Error(ex); } } // run script if (mode == Mode.ScriptFile) { try { await shell.RunScriptAsync(code : File.ReadAllText(scriptFile)); } catch (Exception ex) { Log.Error(ex); } } // run interactively if (mode == Mode.Interactive) { // run test code if there is no interactive console if (!PlatformInfo.System.IsInteractive) { DesktopPlatform.LogTargets.StandardOutput = true; test(shell); } // run interactively else { await shell.PrintWelcomeAsync(); await Interactive(shell : shell, console : NonBlockingConsole.Instance); } } }