protected CommandResult Inspect(ScriptResult result) { if (result == null) { return(CommandResult.Error); } if (result.CompileExceptionInfo != null) { var ex = result.CompileExceptionInfo.SourceException; Logger.ErrorException("Script compilation failed.", ex); return(CommandResult.Error); } if (result.ExecuteExceptionInfo != null) { var ex = result.ExecuteExceptionInfo.SourceException; Logger.ErrorException("Script execution failed.", ex); return(CommandResult.Error); } if (!result.IsCompleteSubmission) { Logger.Error("The script is incomplete."); return(CommandResult.Error); } return(CommandResult.Success); }
public virtual ScriptResult ProcessCommand(string command, ScriptPackSession scriptPackSession) { _logger.Debug("Processing REPL Command [" + command + "] >>"); var scriptResult = new ScriptResult(); if (_replCommands.Count.Equals(0)) { if (scriptPackSession.ReplCommands != null && scriptPackSession.ReplCommands.Count > 0) { foreach (var scriptPackReplCommand in scriptPackSession.ReplCommands) { _replCommands.Add(scriptPackReplCommand.Key, scriptPackReplCommand.Value); } } } var script = ParseArguments(command); if (!string.IsNullOrWhiteSpace(script)) { script = NormaliseScript(script); if (_scriptEngine != null) { scriptResult = _scriptEngine.Execute(script, new string[0], scriptPackSession.References, scriptPackSession.Namespaces, scriptPackSession); } _logger.Debug("<< REPL Command executed."); } else { scriptResult.ReturnValue = "REPL Command not found"; _logger.Debug("<< REPL Command not defined."); } return(scriptResult); }
private bool ProcessCoreCommand(string script, out ScriptResult scriptResult) { scriptResult = new ScriptResult(); var executedCoreCommand = false; try { if (script.StartsWith("#reset") || script.StartsWith(":reset")) { Reset(); executedCoreCommand = true; } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("\r\n" + ex + "\r\n"); scriptResult = new ScriptResult { ExecuteExceptionInfo = ExceptionDispatchInfo.Capture(ex) }; } return(executedCoreCommand); }
public virtual ScriptResult Execute(string code, string[] scriptArgs, IEnumerable<string> references, IEnumerable<string> namespaces, ScriptPackSession scriptPackSession) { Guard.AgainstNullArgument("scriptPackSession", scriptPackSession); _logger.Info("Starting to create execution components"); _logger.Debug("Creating script host"); var session = GetSession(references, namespaces, scriptPackSession); _logger.Info("Starting execution"); var fileName = new FileInfo(FileName).Name + ".dll"; var codeDir = Path.Combine(_fileSystem.CurrentDirectory, "code"); var dllPath = Path.Combine(codeDir, fileName); var dllExists = _fileSystem.FileExists(dllPath); if (!_fileSystem.DirectoryExists(codeDir)) _fileSystem.CreateDirectory(codeDir); var scriptResult = new ScriptResult(); if (!dllExists) { _logger.Debug("Compiling submission"); var referencesForCompilation = session.References.Union(new[] { "mscorlib", "System", "System.Core", "Microsoft.CSharp" }).Distinct().Select(MetadataReference.CreateAssemblyReference).ToList(); var namespacesForCompilation = ReadOnlyArray<string>.CreateFrom(session.Namespaces); var parseOptions = new ParseOptions(CompatibilityMode.None, LanguageVersion.CSharp6, true, SourceCodeKind.Script); var syntaxTree = SyntaxTree.ParseText(code, options: parseOptions); var options = new CompilationOptions(OutputKind.DynamicallyLinkedLibrary) .WithUsings(namespacesForCompilation.ToList()); var compilation = Compilation.Create(AssemblyNamePrefix, options, new[] { syntaxTree }, referencesForCompilation); using (var exeStream = new MemoryStream()) { var result = compilation.Emit(exeStream); if (result.Success) { _logger.Debug("Compilation was successful."); var exeBytes = exeStream.ToArray(); _fileSystem.WriteAllBytes(dllPath, exeBytes); dllExists = true; } else { var errors = String.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString())); _logger.ErrorFormat("Error occurred when compiling: {0})", errors); scriptResult.CompileException = new CompilationException(result.Diagnostics); } } } if (dllExists) { _logger.Debug("Creating Executify Sandbox AppDomain"); var evidence = new Evidence(); evidence.AddHostEvidence(new Zone(SecurityZone.Untrusted)); var permissions = SecurityManager.GetStandardSandbox(evidence); permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); permissions.AddPermission(new WebPermission(PermissionState.Unrestricted)); permissions.AddPermission(new DnsPermission(PermissionState.Unrestricted)); permissions.AddPermission(new NetworkInformationPermission(NetworkInformationAccess.Ping)); var setup = AppDomain.CurrentDomain.SetupInformation; var domain = AppDomain.CreateDomain("ExecutifySandbox", evidence, setup, permissions, null); _logger.Debug("Retrieving compiled script method (reflection)."); try { _logger.Debug("Invoking method."); Activator.CreateInstanceFrom(domain, dllPath, "Script"); scriptResult.ReturnValue = null; } catch (Exception exc) { _logger.Error("An error occurred when executing the scripts."); _logger.ErrorFormat("Exception Message: {0} {1}Stack Trace:{2}", exc.InnerException.Message, Environment.NewLine, exc.InnerException.StackTrace); scriptResult.ExecuteException = exc; } } return scriptResult; }