public void Dispose ()
		{
			if (projectInstance != null) {
				engine.DisposeProjectInstance (projectInstance);
				projectInstance = null;
				engine = null;
			}
		}
		public void Dispose ()
		{
			lock (localLock) {
				if (internalEngine != null)
					internalEngine.Dispose ();
				if (msbuildEngine != null)
					msbuildEngine.Dispose ();
				internalEngine = null;
				msbuildEngine = null;
				disposed = true;
			}
		}
		public MSBuildEngine GetEngine (bool supportsMSBuild)
		{
/*			if (supportsMSBuild) {
				lock (localLock) {
					if (disposed)
						throw new ObjectDisposedException ("MSBuildEngineManager");
					if (msbuildEngine == null) {
						#if !WINDOWS
						msbuildEngine = new MSBuildEngineV4 (this);
						#else
						msbuildEngine = new MSBuildEngineV12 (this);
						#endif
					}
					return msbuildEngine;
				}
			}*/
			lock (localLock) {
				if (disposed)
					throw new ObjectDisposedException ("MSBuildEngineManager");
				if (internalEngine == null)
					internalEngine = new DefaultMSBuildEngine (this);
				return internalEngine;
			}
		}
Exemple #4
0
        void SyncBuildProject(Dictionary <string, MSBuildItem> currentItems, MSBuildEngine e, object project)
        {
            evaluatedItemsIgnoringCondition.Clear();
            evaluatedItems.Clear();

            if (!OnlyEvaluateProperties)
            {
                var evalItems = new Dictionary <string, MSBuildItemEvaluated> ();
                foreach (var it in e.GetEvaluatedItems(project))
                {
                    var xit = it as MSBuildItemEvaluated;
                    if (xit == null)
                    {
                        xit = CreateEvaluatedItem(e, it);
                        var itemId = e.GetItemMetadata(it, NodeIdPropertyName);
                        var key    = itemId + " " + xit.Include;
                        if (evalItems.ContainsKey(key))
                        {
                            continue;                             // xbuild seems to return duplicate items when using wildcards. This is a workaround to avoid the duplicates.
                        }
                        MSBuildItem pit;
                        if (!string.IsNullOrEmpty(itemId) && currentItems.TryGetValue(itemId, out pit))
                        {
                            xit.SourceItem  = pit;
                            xit.Condition   = pit.Condition;
                            evalItems [key] = xit;
                        }
                    }
                    evaluatedItems.Add(xit);
                }

                var evalItemsNoCond = new Dictionary <string, MSBuildItemEvaluated> ();
                foreach (var it in e.GetEvaluatedItemsIgnoringCondition(project))
                {
                    var xit = it as MSBuildItemEvaluated;
                    if (xit == null)
                    {
                        xit = CreateEvaluatedItem(e, it);
                        var itemId = e.GetItemMetadata(it, NodeIdPropertyName);
                        MSBuildItemEvaluated evItem;
                        var key = itemId + " " + xit.Include;
                        if (evalItemsNoCond.ContainsKey(key))
                        {
                            continue;                             // xbuild seems to return duplicate items when using wildcards. This is a workaround to avoid the duplicates.
                        }
                        if (!string.IsNullOrEmpty(itemId) && evalItems.TryGetValue(key, out evItem))
                        {
                            evaluatedItemsIgnoringCondition.Add(evItem);
                            evalItemsNoCond [key] = evItem;
                            continue;
                        }
                        MSBuildItem pit;
                        if (!string.IsNullOrEmpty(itemId) && currentItems.TryGetValue(itemId, out pit))
                        {
                            xit.SourceItem        = pit;
                            xit.Condition         = pit.Condition;
                            evalItemsNoCond [key] = xit;
                        }
                    }
                    evaluatedItemsIgnoringCondition.Add(xit);
                }

                // Clear the node id metadata
                foreach (var it in evaluatedItems.Concat(evaluatedItemsIgnoringCondition))
                {
                    ((MSBuildPropertyGroupEvaluated)it.Metadata).RemoveProperty(NodeIdPropertyName);
                }

                targets = e.GetTargets(project).ToArray();
                targetsIgnoringCondition = e.GetTargetsIgnoringCondition(project).ToArray();
            }

            var props = new MSBuildEvaluatedPropertyCollection(msproject);

            evaluatedProperties = props;
            props.SyncCollection(e, project);

            conditionedProperties = engine.GetConditionedProperties(project);
        }
Exemple #5
0
        internal MSBuildProjectInstanceInfo LoadNativeInstance(bool evaluateItems)
        {
            lock (readLock) {
                var supportsMSBuild = UseMSBuildEngine && (GetGlobalPropertyGroup()?.GetValue("UseMSBuildEngine", true) ?? 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);

                        if (evaluateItems)
                        {
                            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);
            }
        }
Exemple #6
0
 internal void Sync(MSBuildEngine engine, object item)
 {
     properties.Clear();
     this.engine = engine;
     sourceItem  = item;
 }
		public void Evaluate ()
		{
			if (projectInstance != null)
				engine.DisposeProjectInstance (projectInstance);

			info = msproject.LoadNativeInstance ();

			engine = info.Engine;
			projectInstance = engine.CreateProjectInstance (info.Project);

			try {
				foreach (var prop in globalProperties)
					engine.SetGlobalProperty (projectInstance, prop.Key, prop.Value);

				engine.Evaluate (projectInstance);

				SyncBuildProject (info.ItemMap, info.Engine, projectInstance);
			} 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 (msproject, ex.Message);
			}
		}
		MSBuildItemEvaluated CreateEvaluatedItem (MSBuildEngine e, object it)
		{
			string name, include, finalItemSpec;
			bool imported;
			e.GetEvaluatedItemInfo (it, out name, out include, out finalItemSpec, out imported);
			var xit = new MSBuildItemEvaluated (msproject, name, include, finalItemSpec);
			xit.IsImported = imported;
			((MSBuildPropertyGroupEvaluated)xit.Metadata).Sync (e, it);
			return xit;
		}
		void SyncBuildProject (Dictionary<string,MSBuildItem> currentItems, MSBuildEngine e, object project)
		{
			evaluatedItemsIgnoringCondition.Clear ();
			evaluatedItems.Clear ();

			if (!OnlyEvaluateProperties) {
				
				var evalItems = new Dictionary<string,MSBuildItemEvaluated> ();
				foreach (var it in e.GetEvaluatedItems (project)) {
					var xit = it as MSBuildItemEvaluated;
					if (xit == null) {
						xit = CreateEvaluatedItem (e, it);
						var itemId = e.GetItemMetadata (it, NodeIdPropertyName);
						var key = itemId + " " + xit.Include;
						if (evalItems.ContainsKey (key))
							continue; // xbuild seems to return duplicate items when using wildcards. This is a workaround to avoid the duplicates.
						MSBuildItem pit;
						if (!string.IsNullOrEmpty (itemId) && currentItems.TryGetValue (itemId, out pit)) {
							xit.SourceItem = pit;
							xit.Condition = pit.Condition;
							evalItems [key] = xit;
						}
					}
					evaluatedItems.Add (xit);
				}

				var evalItemsNoCond = new Dictionary<string,MSBuildItemEvaluated> ();
				foreach (var it in e.GetEvaluatedItemsIgnoringCondition (project)) {
					var xit = it as MSBuildItemEvaluated;
					if (xit == null) {
						xit = CreateEvaluatedItem (e, it);
						var itemId = e.GetItemMetadata (it, NodeIdPropertyName);
						MSBuildItemEvaluated evItem;
						var key = itemId + " " + xit.Include;
						if (evalItemsNoCond.ContainsKey (key))
							continue; // xbuild seems to return duplicate items when using wildcards. This is a workaround to avoid the duplicates.
						if (!string.IsNullOrEmpty (itemId) && evalItems.TryGetValue (key, out evItem)) {
							evaluatedItemsIgnoringCondition.Add (evItem);
							evalItemsNoCond [key] = evItem;
							continue;
						}
						MSBuildItem pit;
						if (!string.IsNullOrEmpty (itemId) && currentItems.TryGetValue (itemId, out pit)) {
							xit.SourceItem = pit;
							xit.Condition = pit.Condition;
							evalItemsNoCond [key] = xit;
						}
					}
					evaluatedItemsIgnoringCondition.Add (xit);
				}

				// Clear the node id metadata
				foreach (var it in evaluatedItems.Concat (evaluatedItemsIgnoringCondition))
					((MSBuildPropertyGroupEvaluated)it.Metadata).RemoveProperty (NodeIdPropertyName);

				targets = e.GetTargets (project).ToArray ();
			}

			var props = new MSBuildEvaluatedPropertyCollection (msproject);
			evaluatedProperties = props;
			props.SyncCollection (e, project);

			conditionedProperties = engine.GetConditionedProperties (project);
		}