Beispiel #1
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void Execute(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            var selectedItemPaths = _serviceInfo.SolutionSelectionService.GetSelectedItemsPaths();

            var allPaths = _serviceInfo.InnerPathFinder.GetAllInnerPaths(selectedItemPaths);

            if (!allPaths.Any())
            {
                return;
            }

            var solutionFile = new FileInfo(package.GetSolution().FullName);
            var projectFile  = ProjectHelper.GetProjectFilePath(allPaths[0]);
            var tempScript   = CreateTempBatchScript(Path.GetFullPath(allPaths[0]), solutionFile, projectFile);


            ThreadHelper.JoinableTaskFactory.RunAsync(async() =>
            {
                await Task.Run(() =>
                {
                    try
                    {
                        var p = new Process();

                        p.StartInfo.FileName               = tempScript;
                        p.StartInfo.WorkingDirectory       = Path.GetDirectoryName(Path.GetFullPath(allPaths[0]));
                        p.StartInfo.UseShellExecute        = false;
                        p.StartInfo.CreateNoWindow         = true;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.StartInfo.RedirectStandardError  = true;

                        p.OutputDataReceived += async(s, args) => await Output(args.Data);
                        p.ErrorDataReceived  += async(s, args) => await Output(args.Data);

                        p.Start();
                        p.BeginOutputReadLine();
                        p.BeginErrorReadLine();

                        p.WaitForExit();
                        Output($"Script exited with exit code: {p.ExitCode}.");
                    }
                    finally
                    {
                        File.Delete(tempScript);
                    }
                });
            });
        }