Exemplo n.º 1
0
        public void Execute(BuildTask task)
        {
            var messagePane = Package.TundraOutputPane;
            var buildPane   = Package.BuildOutputPane;

            try
            {
                Project proj = null;
                if (task == BuildTask.Compile)
                {
                    proj = TundraPackage.GetProject(Package.DTE.ActiveDocument);
                }
                else
                {
                    proj = Package.GetBuildProject();
                }

                bool regularCommand = false;
                if (proj != null)
                {
                    Match m = TundraPackage.GetBuildCommandLine(proj, proj.ConfigurationManager.ActiveConfiguration);
                    if (m != null && m.Success)
                    {
                        Package.DTE.ExecuteCommand("File.SaveAll");

                        if (task == BuildTask.Compile && !Package.DTE.ActiveDocument.Saved)
                        {
                            Package.DTE.ActiveDocument.Save();
                        }

                        try
                        {
                            var tundraPath = m.Groups[1].ToString();
                            var dir        = m.Groups[2].ToString().Trim(new char[] { '"' });
                            var config     = m.Groups[3].ToString();

                            string projects     = string.Empty;
                            string arguments    = string.Empty;
                            int    projectCount = 0;
                            if (task == BuildTask.Compile)
                            {
                                projects     = proj.Name.ToString();
                                projectCount = 1;
                                arguments    = string.Format("\"{0}\" {1}", Package.DTE.ActiveDocument.FullName, config);
                            }
                            else
                            {
                                var projs = m.Groups[4].ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                projects     = string.Join(" ", projs);
                                projectCount = projs.Length;
                                string arg = string.Empty;
                                if (task == BuildTask.Clean)
                                {
                                    arg = "--clean";
                                }
                                else if (task == BuildTask.Rebuild)
                                {
                                    arg = "--rebuild";
                                }
                                arguments = string.Format("{0} {1} {2}", arg, config, m.Groups[4].ToString());
                            }

                            var p = Package.CreateTundraProcess(tundraPath, dir, arguments);
                            if (p == null)
                            {
                                throw new Exception("Couldn't create process");
                            }

                            p.EnableRaisingEvents = true;
                            p.OutputDataReceived += (s, e) => buildPane.OutputString(e.Data + "\n");
                            p.ErrorDataReceived  += (s, e) => buildPane.OutputString(e.Data + "\n");
                            p.Exited += (s, e) =>
                            {
                                if (!p.HasExited)
                                {
                                    throw new Exception("Tundra process should've exited already!");
                                }
                                if (p.ExitCode == 0)
                                {
                                    buildPane.OutputString(string.Format("========== {0}: {1} succeeded ==========\n", task, projectCount));
                                }
                                else
                                {
                                    buildPane.OutputString(string.Format("========== {0}: Failed: {1} ==========\n", task, p.ExitCode));
                                }
                                p.Dispose();
                            };

                            buildPane.Clear();
                            buildPane.OutputString(string.Format("------ {0} started: Project{1}: {2}, Configuration: {3} ------\n", task, projectCount != 1 ? "(s)" : "", projects, config));
                            buildPane.Activate();
                            Package.DTE.ToolWindows.OutputWindow.Parent.Activate();

                            p.Start();
                            p.BeginOutputReadLine();
                            p.BeginErrorReadLine();
                        }
                        catch (Exception ex)
                        {
                            messagePane.OutputString(string.Format("Failed to launch Tundra: {0}\n", ex.Message));
                        }
                    }
                    else
                    {
                        regularCommand = true;
                    }
                }
                else
                {
                    regularCommand = true;
                }

                if (regularCommand)
                {
                    string command = string.Empty;
                    switch (task)
                    {
                    case BuildTask.Compile:
                        command = "Build.Compile";
                        break;

                    case BuildTask.Build:
                        command = "Build.BuildSolution";
                        break;

                    case BuildTask.Rebuild:
                        command = "Build.RebuildSolution";
                        break;

                    case BuildTask.Clean:
                        command = "Build.CleanSolution";
                        break;
                    }
                    if (!string.IsNullOrEmpty(command))
                    {
                        Package.DTE.ExecuteCommand(command);
                    }
                }
            }
            catch (Exception ex)
            {
                messagePane.OutputString(string.Format("Failed with exception: {0}\n", ex.Message));
                messagePane.OutputString(string.Format("{0}\n", ex.StackTrace));
            }
        }