Meta.MetaPackage.Build C# (CSharp) Method

Build() public method

public Build ( IVsHierarchy hierarchy, string targets, ICollection loggers, bool onlyProject = true, bool isDesignTimeBuild = true, bool NeedsUIThread = true, bool async = true ) : bool
hierarchy IVsHierarchy
targets string
loggers ICollection
onlyProject bool
isDesignTimeBuild bool
NeedsUIThread bool
async bool
return bool
        public bool Build(IVsHierarchy hierarchy, string [] targets, ICollection<ILogger> loggers, bool onlyProject = true, bool isDesignTimeBuild = true, bool NeedsUIThread = true, bool async = true )
        {
            // Get the accessor from the IServiceProvider interface for the
            // project system
            IVsBuildManagerAccessor accessor = GetService(typeof(SVsBuildManagerAccessor)) as IVsBuildManagerAccessor;
            bool releaseUIThread = false;

            // Claim the UI thread under the following conditions:
            // 1. The build must use a resource that uses the UI thread
            // or,
            // 2. The build requires the in-proc node AND waits on the
            // UI thread for the build to complete
            if(accessor != null && NeedsUIThread)
            {
                int result = accessor.ClaimUIThreadForBuild();
                if(result != VSConstants.S_OK)
                {
                    // Not allowed to claim the UI thread right now
                    return false;
                }
                releaseUIThread = true;
            }

            if(accessor != null && isDesignTimeBuild)
            {
                // Start the design time build
                int result = accessor.BeginDesignTimeBuild();
                if(result != VSConstants.S_OK)
                {
                    // Not allowed to begin a design-time build at
                    // this time. Try again later.
                    return false;
                }
            }
            else
            {
                BuildParameters buildParameters = new BuildParameters(ProjectCollection.GlobalProjectCollection);
                BuildManager.DefaultBuildManager.BeginBuild(buildParameters);
                ProjectCollection.GlobalProjectCollection.RegisterLoggers(loggers);
            }

            var proj = ProjectHelper.GetProject(hierarchy);

            System.Collections.Generic.ICollection<MSBuild.Project> loadedProjects = MSBuild.ProjectCollection.GlobalProjectCollection.GetLoadedProjects(proj.FullName);
            var iter = loadedProjects.GetEnumerator();
            bool b = iter.MoveNext();

            MSBuild.Project buildProject = null;
            if (!b)
            {
                buildProject = new MSBuild.Project(proj.FullName);
            }
            else
                buildProject = iter.Current;

            if( buildProject == null )
                throw new InvalidOperationException();
            //MSBuild.Project buildProject = iter.Current;
            MSBuildExec.ProjectInstance pInst = buildProject.CreateProjectInstance();
            if( onlyProject )
                pInst.SetProperty("BuildProjectReferences", "false");
            pInst.DefaultTargets.Clear();
            pInst.DefaultTargets.AddRange(targets);

            MSBuild.ProjectCollection.GlobalProjectCollection.HostServices.SetNodeAffinity(pInst.FullPath, NodeAffinity.InProc);
            BuildRequestData requestData = new BuildRequestData( pInst
                                                               , pInst.DefaultTargets.ToArray()
                                                               , MSBuild.ProjectCollection.GlobalProjectCollection.HostServices);

            ClaimBuildState();
            BuildSubmission submission = BuildManager.DefaultBuildManager.PendBuildRequest(requestData);

            // Register the loggers in BuildLoggers
            if (accessor != null && isDesignTimeBuild)
            {
                foreach (ILogger l in loggers)
                {
                    accessor.RegisterLogger(submission.SubmissionId, l);
                }
            }

            if (async)
            {
                submission.ExecuteAsync(sub =>
                {
                    try
                    {
                        if (accessor != null)
                        {
                            // Unregister the loggers, if necessary.
                            accessor.UnregisterLoggers(sub.SubmissionId);

                            // Release the UI thread, if used
                            if (releaseUIThread)
                                accessor.ReleaseUIThreadForBuild();

                            // End the design time build, if used
                            if (isDesignTimeBuild)
                                accessor.EndDesignTimeBuild();
                        }
                        else
                        {
                            BuildManager.DefaultBuildManager.EndBuild();
                            ProjectCollection.GlobalProjectCollection.UnregisterAllLoggers();
                        }
                    }
                    finally
                    {
                        FreeBuildState();
                    }
                }, null);

                return true;
            }
            else
            {
                try
                {
                    submission.Execute();
                    return true;
                }
                // Clean up resources
                finally
                {
                    if (accessor != null)
                    {
                        // Unregister the loggers, if necessary.
                        accessor.UnregisterLoggers(submission.SubmissionId);
                        // Release the UI thread, if used
                        if (releaseUIThread)
                        {
                            accessor.ReleaseUIThreadForBuild();
                        }
                        // End the design time build, if used
                        if (isDesignTimeBuild)
                        {
                            accessor.EndDesignTimeBuild();
                        }
                    }
                    else
                    {
                        BuildManager.DefaultBuildManager.EndBuild();
                        ProjectCollection.GlobalProjectCollection.UnregisterAllLoggers();
                    }

                    FreeBuildState();
                }
            }
        }