private void GulpCallback(object sender, EventArgs e) { var cmd = (OleMenuCommand)sender; var text = cmd.Text; var task = text.Substring(text.IndexOf(':') + 1).Trim(); if (task == "Gulp") { task = ""; } // if the command is checked it means that there is a running grunt task associated // so we kill it if (cmd.Checked) { System.Diagnostics.Process pro; processes.TryGetValue(cmd, out pro); if (pro != null) { OutputHelpers.Output("Stopping process " + cmd.Text); ProcessHelpers.KillProcessAndChildren(pro.Id); processes.Remove(cmd); } } if (!cmd.Checked) { // launches the grunt process and redirects the output to the output window RunProcess(cmd, " /c \"gulp --no-color " + task + " 2>&1 \" ", false); } else { cmd.Checked = false; } }
private static void RunProcess(OleMenuCommand cmd, string argument, bool fromRoot) { dte.StatusBar.Animate(true, vsStatusAnimation.vsStatusAnimationBuild); try { System.Diagnostics.ProcessStartInfo procStartInfo = new ProcessStartInfo() { RedirectStandardOutput = true, RedirectStandardError = true, StandardOutputEncoding = Encoding.UTF8, StandardErrorEncoding = Encoding.UTF8, UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = fromRoot ? SolutionHelpers.GetRootFolder(dte) : Path.GetDirectoryName(SolutionHelpers.GetSourceFilePath()), FileName = "cmd", Arguments = argument, }; System.Diagnostics.Process proc = new System.Diagnostics.Process() { StartInfo = procStartInfo, EnableRaisingEvents = true }; OutputHelpers.Output("Executing " + cmd.Text + " \r\n\r\n", true); proc.OutputDataReceived += (object sendingProcess, DataReceivedEventArgs outLine) => OutputHelpers.Output(outLine.Data + "\r\n"); proc.ErrorDataReceived += (object sendingProcess, DataReceivedEventArgs outLine) => OutputHelpers.Output(outLine.Data + "\r\n"); proc.Exited += (x, y) => { processes.Remove(cmd); cmd.Checked = false; dte.StatusBar.Animate(false, vsStatusAnimation.vsStatusAnimationBuild); }; proc.Start(); proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); cmd.Checked = true; processes.Add(cmd, proc); } catch (Exception ex) { OutputHelpers.Output(ex.Message); } }