Exemplo n.º 1
0
        internal MSBuildProjectInstanceInfo LoadNativeInstance()
        {
            lock (readLock) {
                var supportsMSBuild = UseMSBuildEngine && GetGlobalPropertyGroup().GetValue("UseMSBuildEngine", true);

                if (engineManager == null)
                {
                    engineManager        = new MSBuildEngineManager();
                    engineManagerIsLocal = true;
                }

                MSBuildEngine e = engineManager.GetEngine(supportsMSBuild);

                if (nativeProjectInfo != null && nativeProjectInfo.Engine != null && (nativeProjectInfo.Engine != e || nativeProjectInfo.ProjectStamp != ChangeStamp))
                {
                    nativeProjectInfo.Engine.UnloadProject(nativeProjectInfo.Project);
                    nativeProjectInfo = null;
                }

                if (nativeProjectInfo == null)
                {
                    nativeProjectInfo = new MSBuildProjectInstanceInfo {
                        Engine       = e,
                        ProjectStamp = ChangeStamp
                    };
                }

                if (nativeProjectInfo.Project == null)
                {
                    // Use a private metadata property to assign an id to each item. This id is used to match
                    // evaluated items with the items that generated them.

                    try {
                        DisableChangeTracking();

                        var ctx = new WriteContext {
                            Evaluating = true,
                            ItemMap    = new Dictionary <string, MSBuildItem> ()
                        };
                        var xml = SaveToString(ctx);

                        foreach (var it in GetAllItems())
                        {
                            it.EvaluatedItemCount = 0;
                        }

                        nativeProjectInfo.Project = e.LoadProject(this, xml, FileName);
                    } catch (Exception ex) {
                        // If the project can't be evaluated don't crash
                        LoggingService.LogError("MSBuild project could not be evaluated", ex);
                        throw new ProjectEvaluationException(this, ex.Message);
                    } finally {
                        EnableChangeTracking();
                    }
                }
                return(nativeProjectInfo);
            }
        }