public virtual ScriptResult Execute(string script, params string[] scriptArgs) { var path = Path.IsPathRooted(script) ? script : Path.Combine(FileSystem.CurrentDirectory, script); var result = FilePreProcessor.ProcessFile(path); ScriptEngine.FileName = Path.GetFileName(path); return(EngineExecute(Path.GetDirectoryName(path), scriptArgs, result)); }
protected internal virtual FilePreProcessorResult LoadScriptLibraries(string workingDirectory) { if (string.IsNullOrWhiteSpace(ScriptLibraryComposer.ScriptLibrariesFile)) { return(null); } var scriptLibrariesPath = Path.Combine(workingDirectory, FileSystem.PackagesFolder, ScriptLibraryComposer.ScriptLibrariesFile); if (FileSystem.FileExists(scriptLibrariesPath)) { _log.DebugFormat("Found Script Library at {0}", scriptLibrariesPath); return(FilePreProcessor.ProcessFile(scriptLibrariesPath)); } return(null); }
public override ScriptResult Execute(string script, params string[] scriptArgs) { if (script == null) { throw new ArgumentNullException(nameof(script)); } try { if (script.StartsWith(":")) { var tokens = script.Split(' '); if (tokens[0].Length > 1) { var command = Commands.FirstOrDefault(x => x.Key == tokens[0].Substring(1)); if (command.Value != null) { var argsToPass = new List <object>(); foreach (var argument in tokens.Skip(1)) { var argumentResult = ScriptEngine.Execute( argument, _scriptArgs, References, Namespaces, ScriptPackSession); if (argumentResult.CompileExceptionInfo != null) { throw new Exception( GetInvalidCommandArgumentMessage(argument), argumentResult.CompileExceptionInfo.SourceException); } if (argumentResult.ExecuteExceptionInfo != null) { throw new Exception( GetInvalidCommandArgumentMessage(argument), argumentResult.ExecuteExceptionInfo.SourceException); } if (!argumentResult.IsCompleteSubmission) { throw new Exception(GetInvalidCommandArgumentMessage(argument)); } argsToPass.Add(argumentResult.ReturnValue); } var commandResult = command.Value.Execute(this, argsToPass.ToArray()); return(ProcessCommandResult(commandResult)); } } } var preProcessResult = FilePreProcessor.ProcessScript(script); ImportNamespaces(preProcessResult.Namespaces.ToArray()); foreach (var reference in preProcessResult.References) { var referencePath = FileSystem.GetFullPath(Path.Combine(FileSystem.BinFolder, reference)); AddReferences(FileSystem.FileExists(referencePath) ? referencePath : reference); } Console.ForegroundColor = ConsoleColor.Cyan; InjectScriptLibraries(FileSystem.CurrentDirectory, preProcessResult, ScriptPackSession.State); Buffer = (Buffer == null) ? preProcessResult.Code : Buffer + Environment.NewLine + preProcessResult.Code; var namespaces = Namespaces.Union(preProcessResult.Namespaces); var references = References.Union(preProcessResult.References); var result = ScriptEngine.Execute(Buffer, _scriptArgs, references, namespaces, ScriptPackSession); if (result == null) { return(ScriptResult.Empty); } if (result.CompileExceptionInfo != null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(result.CompileExceptionInfo.SourceException.Message); } if (result.ExecuteExceptionInfo != null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(result.ExecuteExceptionInfo.SourceException.Message); } if (result.InvalidNamespaces.Any()) { RemoveNamespaces(result.InvalidNamespaces.ToArray()); } if (!result.IsCompleteSubmission) { return(result); } if (result.ReturnValue != null) { Console.ForegroundColor = ConsoleColor.Yellow; var serializedResult = _serializer.Serialize(result.ReturnValue); Console.WriteLine(serializedResult); } Buffer = null; return(result); } catch (FileNotFoundException fileEx) { RemoveReferences(fileEx.FileName); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(Environment.NewLine + fileEx + Environment.NewLine); return(new ScriptResult(compilationException: fileEx)); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(Environment.NewLine + ex + Environment.NewLine); return(new ScriptResult(executionException: ex)); } finally { Console.ResetColor(); } }
public virtual ScriptResult ExecuteScript(string script, params string[] scriptArgs) { var result = FilePreProcessor.ProcessScript(script); return(EngineExecute(FileSystem.CurrentDirectory, scriptArgs, result)); }