public SyrupExecuteResult RunAfterMakeCurrent(LocalReleaseInfo releaseInfo) { _log.Debug($"RunAfterMakeCurrent; File: {releaseInfo.File}"); var rootPath = new DirectoryInfo(_pathToApps).Parent?.FullName; var currentAppPath = Path.Combine(_pathToApps, releaseInfo.App); var scriptsPath = Path.Combine(currentAppPath, "_syrup\\scripts"); var scriptPath = Path.Combine(scriptsPath, "after-make-current.csx"); var globals = new ExecutorRequest(scriptPath, rootPath, currentAppPath, releaseInfo.Name); var res = Run(globals); return(res); }
private SyrupExecuteResult Run(ExecutorRequest syrupContainer) { var executeResult = new SyrupExecuteResult(); string fileString; var text = JsonConvert.SerializeObject(syrupContainer); var path = _registry.SyrupExecutorFilePath; var fileName = $"{Consts.MEM_FILE_PREFIX}-{Guid.NewGuid()}"; var size = Consts.MEM_FILE_SIZE; _log.Debug($"Create mem file; Name: {fileName}; Size: {size}b "); _log.Debug($"Script runner: {path}"); using (var mmf = MemoryMappedFile.CreateNew(fileName, size)) { using (var stream = mmf.CreateViewStream()) { var writer = new BinaryWriter(stream); writer.Write(text); } _log.Debug($"Starting script runner: {path}"); // Command line args are separated by a space var p = Process.Start(path, fileName); _log.Debug("Waiting child to die"); p.WaitForExit(); _log.Debug("Child died"); using (var stream = mmf.CreateViewStream()) { var reader = new BinaryReader(stream); fileString = reader.ReadString(); } } _log.Debug($"Execute result: {fileString}"); return(executeResult); }
public SyrupContainer(ExecutorRequest globals) { Syrup = globals; }
public ExecutorResponse Run(ExecutorRequest executorRequest) { var ret = new ExecutorResponse(); var sw = new Stopwatch(); sw.Start(); _log.Debug($"Run script: {executorRequest.ScriptPath}"); var sg = new SyrupGlobals(executorRequest.RootPath, executorRequest.AppPath, executorRequest.AppName); _log.Debug($"AppName: {sg.CurrentAppName}"); _log.Debug($"RootPath: {sg.CurrentRootPath}"); _log.Debug($"AppPath: {sg.CurrentAppPath}"); var syrupContainer = new SyrupContainer(sg); var executeResult = syrupContainer.Syrup.ExecuteResult; var code = File.ReadAllText(executorRequest.ScriptPath); //_log.Debug($"Code: {code}"); try { var opts = ScriptOptions.Default .AddReferences(Assembly.GetEntryAssembly()) .AddImports("System") .AddImports("System.Diagnostics") .AddImports("System.Linq") .AddImports("Syrup.ScriptExecutor.Models") ; var script = CSharpScript.Create(code, opts, typeof(SyrupContainer)); var compilation = script.GetCompilation(); var diagnostics = compilation.GetDiagnostics(); var r1 = compilation.ReferencedAssemblyNames .Where(x => x.Name.IndexOf("syrup", StringComparison.OrdinalIgnoreCase) > -1) .ToArray(); foreach (var assemblyIdentity in r1) { _log.Debug($"assemblyIdentity: {assemblyIdentity}"); } if (diagnostics.Any()) { foreach (var diagnostic in diagnostics) { var msg = diagnostic.GetMessage(); executeResult.AddMessage(msg); Console.WriteLine(diagnostic.GetMessage()); } executeResult.AbortProcess("Script error"); } else { var result = script.RunAsync(syrupContainer).Result; ret.ReturnValue = result.ReturnValue?.ToString() ?? string.Empty; ret.Success = true; } } catch (Exception e) { _log.Error(e); } ret.Messages = executeResult.Messages; ret.Continue = executeResult.Continue; sw.Stop(); _log.Debug($"Run script took: {sw.ElapsedMilliseconds}ms"); return(ret); }