public static async Task RunTortoiseGitCommand(string command, string args = "") { var solutionDir = await FileHelper.GetSolutionDir(); if (string.IsNullOrEmpty(solutionDir)) { return; } var options = await General.GetLiveInstanceAsync(); await StartTortoiseGitProc($"/command:{command} /path:\"{solutionDir}\" {args} /closeonend:{options.CloseOnEnd}"); }
/// <summary> /// Execute a Git command and return true if output is non-empty /// </summary> /// <param name="commands">Git command to be executed</param> /// <param name="showAlert">Show an alert dialog when error output is non-empty</param> /// <returns>True if output is non-empty</returns> public static async Task<bool> StartProcessGit(string commands, bool showAlert = true) { var solutionDir = await FileHelper.GetSolutionDir(); if (string.IsNullOrEmpty(solutionDir)) { return false; } var output = string.Empty; var error = string.Empty; var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = $"/c cd /D \"{solutionDir}\" && \"{FileHelper.GetMSysGit()}\" {commands}", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; proc.Start(); while (!proc.StandardOutput.EndOfStream) { output = await proc.StandardOutput.ReadLineAsync(); } while (!proc.StandardError.EndOfStream) { error += await proc.StandardError.ReadLineAsync(); } if (!string.IsNullOrEmpty(output)) { return true; } if (!string.IsNullOrEmpty(error) && showAlert) { await VS.MessageBox.ShowErrorAsync("TGit error", error); } return false; }
/// <summary> /// Get the root of the solution path (where the .git folder resides) /// </summary> /// <remarks>Cached for 30s</remarks> /// <returns></returns> public string GetSolutionDir() { if (_cache.Contains(CacheKeyEnum.SolutionDir.ToString())) { return(_cache.Get(CacheKeyEnum.SolutionDir.ToString()).ToString()); } var solutionDir = FileHelper.GetSolutionDir(_dte); if (!string.IsNullOrEmpty(solutionDir)) { _cache.Set(CacheKeyEnum.SolutionDir.ToString(), solutionDir, DateTimeOffset.Now.AddSeconds(30)); } return(solutionDir); }
/// <summary> /// Execute a Git command and return the output /// </summary> /// <param name="commands">Git command to be executed</param> /// <returns>Git output</returns> public static async Task<string> StartProcessGitResult(string commands) { var solutionDir = await FileHelper.GetSolutionDir(); if (string.IsNullOrEmpty(solutionDir)) { return string.Empty; } var output = string.Empty; var error = string.Empty; var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = $"/c cd /D \"{solutionDir}\" && \"{FileHelper.GetMSysGit()}\" {commands}", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; proc.Start(); while (!proc.StandardOutput.EndOfStream) { output += await proc.StandardOutput.ReadLineAsync() + ";"; } while (!proc.StandardError.EndOfStream) { error += await proc.StandardError.ReadLineAsync(); } return string.IsNullOrEmpty(output) ? error : output.TrimEnd(';'); }
public static void GetSolutionDir(DTE dte) => SolutionDir = FileHelper.GetSolutionDir(dte);
public static async Task<Process> StartProcessGui(string application, string args, string title, string branchName = "", OutputBox outputBox = null, string pushCommand = "") { var dialogResult = DialogResult.OK; if (!await StartProcessGit("config user.name") || !await StartProcessGit("config user.email")) { dialogResult = new Credentials().ShowDialog(); } if (dialogResult != DialogResult.OK) { return null; } try { var solutionDir = await FileHelper.GetSolutionDir(); var process = new Process { StartInfo = { WindowStyle = ProcessWindowStyle.Hidden, RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, FileName = application, Arguments = args, WorkingDirectory = solutionDir }, EnableRaisingEvents = true }; process.Exited += process_Exited; process.OutputDataReceived += OutputDataHandler; process.ErrorDataReceived += OutputDataHandler; if (outputBox == null) { _outputBox = new OutputBox(branchName, pushCommand); _outputBox.Show(); } else { _outputBox = outputBox; } process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); if (!string.IsNullOrEmpty(title)) { _outputBox.Text = title; } _outputBox.Show(); return process; } catch (Exception e) { await VS.MessageBox.ShowErrorAsync($"'{application}' not found", e.Message); } return null; }