/// <summary> /// 运行bash命令 /// </summary> public static bool RunCommand(string commandToRun, NLog.Logger logger, Func <bool> checkCancel = null, string workingDirectory = null) { Process process = null; IlogProgress pr = null; try { if (string.IsNullOrEmpty(workingDirectory)) { workingDirectory = Directory.GetDirectoryRoot(Directory.GetCurrentDirectory()); } var processStartInfo = new ProcessStartInfo() { FileName = "cmd", RedirectStandardOutput = true, RedirectStandardInput = true, RedirectStandardError = true, CreateNoWindow = true, UseShellExecute = false, Verb = "runas", WorkingDirectory = workingDirectory }; pr = new BuildProgress(logger); process = Process.Start(processStartInfo); process.OutputDataReceived += (sender, args) => { if (checkCancel != null) { var r = checkCancel(); if (r) { try { process?.Dispose(); } catch (Exception) { //ignore } try { process?.Kill(); } catch (Exception) { //ignore } } } if (!string.IsNullOrWhiteSpace(args.Data)) { if (args.Data.StartsWith(" "))//有这个代表肯定build有出问题 { logger.Info(args.Data); } if (args.Data.Contains(": warning")) { pr.Log(new BuildEventArgs { level = LogLevel.Warn, message = args.Data }); } else if (args.Data.Contains(": error")) { if (pr is BuildProgress) { pr.Dispose(); pr = new FullLogProgress(logger);//一旦有出错 后面就走全部日志 } logger.Error(args.Data); } else { pr.Log(new BuildEventArgs { level = LogLevel.Info, message = args.Data }); } } }; process.BeginOutputReadLine(); process.ErrorDataReceived += (sender, data) => { if (pr is BuildProgress) { pr.Dispose(); pr = new FullLogProgress(logger);//一旦有出错 后面就走全部日志 } if (checkCancel != null) { var r = checkCancel(); if (r) { try { process?.Dispose(); } catch (Exception) { //ignore } try { process?.Kill(); } catch (Exception) { //ignore } } } if (!string.IsNullOrWhiteSpace(data.Data)) { pr.Log(new BuildEventArgs { level = LogLevel.Error, message = data.Data }); } }; process.BeginErrorReadLine(); //录入命令 process.StandardInput.WriteLine($"{commandToRun} & exit"); process.WaitForExit(); try { process.Kill(); } catch (Exception) { //ignore } return(process.ExitCode == 0); } catch (Exception ex) { if (checkCancel != null) { var r = checkCancel(); if (r) { logger?.Error("deploy task was canceled!"); return(false); } } logger?.Error(ex.Message); return(false); } finally { try { process?.Kill(); } catch (Exception) { //ignore } try { pr?.Dispose(); } catch (Exception) { //ignore } try { process?.Dispose(); } catch (Exception) { //ignore } } }
/// <summary> /// 执行dotnet Command命令 /// </summary> /// <param name="projectPath"></param> /// <param name="fileName"></param> /// <param name="arguments"></param> /// <param name="logger"></param> /// <returns></returns> private static bool RunDotnetExternalExe(string projectPath, string fileName, string arguments, NLog.Logger logger, Func <bool> checkCancel = null) { Process process = null; BuildProgress pr = null; try { try { logger.Info(fileName + " " + arguments); } catch (Exception) { } if (string.IsNullOrEmpty(arguments)) { throw new ArgumentException(nameof(arguments)); } //执行dotnet命令如果 projectdir路径含有空格 或者 outDir 路径含有空格 都是没有问题的 pr = new BuildProgress(logger); process = new Process(); process.StartInfo.WorkingDirectory = projectPath; process.StartInfo.FileName = fileName; process.StartInfo.Arguments = arguments; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.UseShellExecute = false; process.StartInfo.Verb = "runas"; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardOutput = true; process.Start(); process.OutputDataReceived += (sender, args) => { if (checkCancel != null) { var r = checkCancel(); if (r) { try { process?.Dispose(); } catch (Exception) { //ignore } try { process?.Kill(); } catch (Exception) { //ignore } } } if (!string.IsNullOrWhiteSpace(args.Data)) { if (args.Data.StartsWith(" "))//有这个代表肯定build有出问题 { logger.Info(args.Data); } if (args.Data.Contains(": warning")) { pr.Log(new BuildEventArgs { level = LogLevel.Warn, message = args.Data }); } else if (args.Data.Contains(": error")) { logger.Error(args.Data); } else { pr.Log(new BuildEventArgs { level = LogLevel.Info, message = args.Data }); } } }; process.BeginOutputReadLine(); process.ErrorDataReceived += (sender, data) => { if (checkCancel != null) { var r = checkCancel(); if (r) { try { process?.Dispose(); } catch (Exception) { //ignore } try { process?.Kill(); } catch (Exception) { //ignore } } } if (!string.IsNullOrWhiteSpace(data.Data)) { pr.Log(new BuildEventArgs { level = LogLevel.Error, message = data.Data }); } }; process.BeginErrorReadLine(); process.WaitForExit(); try { process.Kill(); } catch (Exception) { //ignore } return(process.ExitCode == 0); } catch (Exception ex) { if (checkCancel != null) { var r = checkCancel(); if (r) { logger?.Error("deploy task was canceled!"); return(false); } } logger?.Error(ex.Message); return(false); } finally { try { process?.Kill(); } catch (Exception) { //ignore } try { pr?.Dispose(); } catch (Exception) { //ignore } try { process?.Dispose(); } catch (Exception) { //ignore } } }