public CmResult RunCSharp(string path) { try { object result = CSharpScriptEngine.Execute(path + "return Test.Main();"); if (result == null) { return(CmResult.BuildSuccess("执行成功:没有返回值")); } return(CmResult.BuildSuccess(string.IsNullOrEmpty(result.ToString()) ? "执行成功" : result.ToString())); } catch (Exception ex) { return(CmResult.BuildFail("执行失败", ex.StackTrace)); } }
public CmResult C(string path) { if (!FileHelper.IsExists(path)) { return(CmResult.BuildFail("", "文件不存在")); } string dir = FileHelper.GetDir(path); string guid = Guid.NewGuid().ToString("N"); string runsh = Path.Combine(dir, guid); CmResult cr = BuildHelper.BuildCmm($"gcc {path} -o {runsh}"); if (cr.Success) { return(CmResult.BuildSuccess(Path.Combine(dir, $"./{guid}"))); } return(cr); }
private CmResult BuildJava(string path) { if (!FileHelper.IsExists(path)) { return(CmResult.BuildFail("", "文件不存在")); } //string dir = FileHelper.GetDir(path); CmResult cr = BuildCmm($"javac -encoding utf8 {path}"); if (!cr.Success) { return(cr); } string runName = path.Substring(0, path.LastIndexOf(".java")); return(CmResult.BuildSuccess(runName)); }
public CmResult CSharp(string path) { try { object result = BuildHelper.Execute(path + "return Test.Main();"); if (result == null) { return(CmResult.BuildSuccess("执行成功:没有返回值")); } if ("exec long time".Equals(result.ToString())) { return(CmResult.BuildFail("执行失败", $"执行时间超过{StaticVariable.RUN_MILL}ms,代码中可能包含死循环")); } return(CmResult.BuildSuccess(string.IsNullOrEmpty(result.ToString()) ? "执行成功" : result.ToString())); } catch (Exception ex) { return(CmResult.BuildFail("执行失败", ex.StackTrace)); } }
public CmResult Java(string path) { if (!FileHelper.IsExists(path)) { return(CmResult.BuildFail("", "文件不存在")); } if (!path.EndsWith(".java")) { return(CmResult.BuildFail("", "文件类型不正确")); } CmResult cr = BuildHelper.BuildCmm($"javac -encoding utf8 {path}"); if (!cr.Success) { return(cr); } string runName = path.Substring(0, path.LastIndexOf(".java")); return(CmResult.BuildSuccess(runName)); }
private CmResult BuildCmm(string cmd, int waitMils = 3000) { string strOutput = null; bool success = false; try { ProcessStartInfo psi = new ProcessStartInfo("sh"); psi.UseShellExecute = false; psi.RedirectStandardError = true; psi.RedirectStandardInput = true; psi.RedirectStandardOutput = true; psi.CreateNoWindow = true; using (Process proCompiler = new Process()) { proCompiler.StartInfo = psi; proCompiler.Start(); proCompiler.StandardInput.WriteLine(cmd); proCompiler.StandardInput.Flush(); proCompiler.StandardInput.Dispose(); // 是否有错误信息 strOutput = proCompiler.StandardError.ReadToEnd(); if (string.IsNullOrEmpty(strOutput)) { strOutput = proCompiler.StandardOutput.ReadToEnd(); success = true; } proCompiler.WaitForExit(waitMils); } return(success ? CmResult.BuildSuccess(string.IsNullOrEmpty(strOutput) ? "执行成功" : strOutput) : CmResult.BuildFail("执行失败", strOutput)); } catch (Exception ex) { return(CmResult.BuildFail("执行失败", ex.Message)); } }