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);
        }
        static MSBuildItemEvaluated CreateEvaluatedItem(MSBuildEvaluationContext context, MSBuildProject project, MSBuildItem sourceItem, string include)
        {
            var it = new MSBuildItemEvaluated(project, sourceItem.Name, sourceItem.Include, include);
            var md = new Dictionary <string, MSBuildPropertyEvaluated> ();

            foreach (var c in sourceItem.Metadata.GetProperties())
            {
                md [c.Name] = new MSBuildPropertyEvaluated(project, c.Name, c.Value, context.EvaluateString(c.Value));
            }
            ((MSBuildPropertyGroupEvaluated)it.Metadata).SetProperties(md);
            it.SourceItem = sourceItem;
            it.Condition  = sourceItem.Condition;
            return(it);
        }
        void UpdateMetadata(MSBuildItemEvaluated it)
        {
            if (evaluatedItemDefinitions == null)
            {
                return;
            }

            if (!evaluatedItemDefinitions.TryGetValue(it.Name, out MSBuildPropertyGroupEvaluated itemDefProps))
            {
                return;
            }

            var props = (MSBuildPropertyGroupEvaluated)it.Metadata;

            foreach (var evaluatedProp in itemDefProps.GetProperties())
            {
                if (!props.HasProperty(evaluatedProp.Name))
                {
                    props.SetProperty(evaluatedProp.Name, evaluatedProp);
                }
            }
        }
Exemplo n.º 4
0
 static void AddItem(ProjectInfo project, MSBuildEvaluationContext context, MSBuildItem item, MSBuildItemEvaluated it, string include, Regex excludeRegex, bool trueCond)
 {
     if (include.IndexOf('*') != -1)
     {
         var path = include;
         if (path == "**" || path.EndsWith("\\**"))
         {
             path = path + "/*";
         }
         var subpath = path.Split('\\');
         foreach (var eit in ExpandWildcardFilePath(project, project.Project, context, item, project.Project.BaseDirectory, FilePath.Null, false, subpath, 0))
         {
             if (excludeRegex != null && excludeRegex.IsMatch(eit.Include))
             {
                 continue;
             }
             project.EvaluatedItemsIgnoringCondition.Add(eit);
             if (trueCond)
             {
                 project.EvaluatedItems.Add(eit);
             }
         }
     }
     else if (include != it.Include)
     {
         if (excludeRegex != null && excludeRegex.IsMatch(include))
         {
             return;
         }
         it = CreateEvaluatedItem(context, project, project.Project, item, include);
         project.EvaluatedItemsIgnoringCondition.Add(it);
         if (trueCond)
         {
             project.EvaluatedItems.Add(it);
         }
     }
     else
     {
         if (excludeRegex != null && excludeRegex.IsMatch(include))
         {
             return;
         }
         project.EvaluatedItemsIgnoringCondition.Add(it);
         if (trueCond)
         {
             project.EvaluatedItems.Add(it);
         }
     }
 }
Exemplo n.º 5
0
		static MSBuildItemEvaluated CreateEvaluatedItem (MSBuildEvaluationContext context, ProjectInfo pinfo, MSBuildProject project, MSBuildItem sourceItem, string include)
		{
			var it = new MSBuildItemEvaluated (project, sourceItem.Name, sourceItem.Include, include);
			var md = new Dictionary<string,IMSBuildPropertyEvaluated> ();
			foreach (var c in sourceItem.Metadata.GetProperties ()) {
				if (string.IsNullOrEmpty (c.Condition) || SafeParseAndEvaluate (pinfo, context, c.Condition, true))
					md [c.Name] = new MSBuildPropertyEvaluated (project, c.Name, c.Value, context.EvaluateString (c.Value));
			}
			((MSBuildPropertyGroupEvaluated)it.Metadata).SetProperties (md);
			it.SourceItem = sourceItem;
			it.Condition = sourceItem.Condition;
			return it;
		}
Exemplo n.º 6
0
		static void AddItem (ProjectInfo project, MSBuildEvaluationContext context, MSBuildItem item, MSBuildItemEvaluated it, string include, Regex excludeRegex, bool trueCond)
		{
			if (include.IndexOf ('*') != -1) {
				var path = include;
				if (path == "**" || path.EndsWith ("\\**"))
					path = path + "/*";
				var subpath = path.Split ('\\');
				foreach (var eit in ExpandWildcardFilePath (project, project.Project, context, item, project.Project.BaseDirectory, FilePath.Null, false, subpath, 0)) {
					if (excludeRegex != null && excludeRegex.IsMatch (eit.Include))
						continue;
					project.EvaluatedItemsIgnoringCondition.Add (eit);
					if (trueCond)
						project.EvaluatedItems.Add (eit);
				}
			}
			else if (include != it.Include) {
				if (excludeRegex != null && excludeRegex.IsMatch (include))
					return;
				it = CreateEvaluatedItem (context, project, project.Project, item, include);
				project.EvaluatedItemsIgnoringCondition.Add (it);
				if (trueCond)
					project.EvaluatedItems.Add (it);
			}
			else {
				if (excludeRegex != null && excludeRegex.IsMatch (include))
					return;
				project.EvaluatedItemsIgnoringCondition.Add (it);
				if (trueCond)
					project.EvaluatedItems.Add (it);
			}
		}
Exemplo n.º 7
0
		static bool ExecuteTransformItemListFunction (ref MSBuildItemEvaluated[] transformItems, string itemFunction, object[] itemFunctionArgs, out bool ignoreMetadata)
		{
			switch (itemFunction) {
			case "Reverse":
				ignoreMetadata = false;
				transformItems = transformItems.Reverse ().ToArray ();
				return true;
			case "HasMetadata":
				ignoreMetadata = false;
				if (itemFunctionArgs.Length != 1)
					return false;
				transformItems = transformItems.Where (it => it.Metadata.HasProperty (itemFunctionArgs [0].ToString ())).ToArray ();
				return true;
			case "WithMetadataValue":
				ignoreMetadata = false;
				if (itemFunctionArgs.Length != 2)
					return false;
				transformItems = transformItems.Where (it => string.Compare (it.Metadata.GetValue (itemFunctionArgs [0].ToString ()), itemFunctionArgs [1].ToString (), true) == 0).ToArray ();
				return true;
			case "ClearMetadata":
				ignoreMetadata = true;
				return true;
			case "Distinct":
			case "DistinctWithCase":
				var values = new HashSet<string> (itemFunction == "Distinct" ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);
				var result = new List<MSBuildItemEvaluated> ();
				foreach (var it in transformItems) {
					if (values.Add (it.Include))
						result.Add (it);
				}
				transformItems = result.ToArray ();
				ignoreMetadata = true;
				return true;
			}
			ignoreMetadata = false;
			return false;
		}
		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;
		}
Exemplo n.º 9
0
		static bool ExecuteSummaryItemFunction (MSBuildItemEvaluated [] transformItems, string itemFunction, object [] itemFunctionArgs, out string result)
		{
			result = null;
			switch (itemFunction) {
			case "Count":
				result = transformItems.Length.ToString ();
				return true;
			case "AnyHaveMetadataValue":
				if (itemFunctionArgs.Length != 2)
					return false;
				result = transformItems.Any (it => string.Compare (it.Metadata.GetValue (itemFunctionArgs [0].ToString ()), itemFunctionArgs [1].ToString (), true) == 0).ToString ().ToLower ();
				return true;
			}
			return false;
		}
Exemplo n.º 10
0
		static bool ExecuteTransformIncludeItemFunction (MSBuildEvaluationContext context, MSBuildItemEvaluated item, string itemFunction, object [] itemFunctionArgs, out string evaluatedInclude, out bool skip)
		{
			evaluatedInclude = null;
			skip = false;

			switch (itemFunction) {
			case "DirectoryName":
				var path = MSBuildProjectService.FromMSBuildPath (context.Project.BaseDirectory, item.Include);
				evaluatedInclude = Path.GetDirectoryName (path);
				return true;
			case "Metadata":
				if (itemFunctionArgs.Length != 1)
					return false;
				var p = item.Metadata.GetProperty (itemFunctionArgs [0].ToString ());
				if (p == null) {
					skip = true;
					return true;
				} else {
					evaluatedInclude = p.Value;
					return true;
				}
			}
			if (knownStringItemFunctions.Contains (itemFunction)) {
				object res;
				if (context.EvaluateMember (itemFunction, typeof (string), itemFunction, item.Include, itemFunctionArgs, out res)) {
					evaluatedInclude = res.ToString ();
					return true;
				}
			}
			return false;
		}
Exemplo n.º 11
0
		static bool ExecuteTransform (ProjectInfo project, MSBuildEvaluationContext context, MSBuildItem item, string transformExp, out List<MSBuildItemEvaluated> items)
		{
			bool ignoreMetadata = false;

			items = new List<MSBuildItemEvaluated> ();
			string itemName, expression, itemFunction; object [] itemFunctionArgs;

			// This call parses the transforms and extracts: the name of the item list to transform, the whole transform expression (or null if there isn't). If the expression can be
			// parsed as an item funciton, then it returns the function name and the list of arguments. Otherwise those parameters are null.
			if (!ParseTransformExpression (context, transformExp, out itemName, out expression, out itemFunction, out itemFunctionArgs))
				return false;

			// Get the items mathing the referenced item list
			var transformItems = project.EvaluatedItems.Where (i => i.Name == itemName).ToArray ();

			if (itemFunction != null) {
				// First of all, try to execute the function as a summary function, that is, a function that returns a single value for
				// the whole list (such as Count).
				// After that, try executing as a list transformation function: a function that changes the order or filters out items from the list.

				string result;
				if (ExecuteSummaryItemFunction (transformItems, itemFunction, itemFunctionArgs, out result)) {
					// The item function returns a value. Just create an item with that value
					var newItem = new MSBuildItemEvaluated (project.Project, item.Name, item.Include, result);
					project.EvaluatedItemsIgnoringCondition.Add (newItem);
					items.Add (newItem);
					return true;
				} else if (ExecuteTransformItemListFunction (ref transformItems, itemFunction, itemFunctionArgs, out ignoreMetadata)) {
					expression = null;
					itemFunction = null;
				}
			}

			foreach (var eit in transformItems) {
				// Some item functions cause the erasure of metadata. Take that into account now.
				context.SetItemContext (eit.Include, null, ignoreMetadata || item == null ? null : eit.Metadata);
				try {
					// If there is a function that transforms the include of the item, it needs to be applied now. Otherwise just use the transform expression
					// as include, or the transformed item include if there is no expression.

					string evaluatedInclude; bool skip;
					if (itemFunction != null && ExecuteTransformIncludeItemFunction (context, eit, itemFunction, itemFunctionArgs, out evaluatedInclude, out skip)) {
						if (skip) continue;
					} else if (expression != null)
						evaluatedInclude = context.EvaluateString (expression);
					else
						evaluatedInclude = eit.Include;

					var newItem = new MSBuildItemEvaluated (project.Project, item.Name, item.Include, evaluatedInclude);
					if (!ignoreMetadata) {
						var md = new Dictionary<string, IMSBuildPropertyEvaluated> ();
						// Add metadata from the evaluated item
						var col = (MSBuildPropertyGroupEvaluated)eit.Metadata;
						foreach (var p in col.GetRegisteredProperties ()) {
							md [p.Name] = new MSBuildPropertyEvaluated (project.Project, p.Name, p.UnevaluatedValue, p.Value);
						}
						// Now override metadata from the new item definition
						foreach (var c in item.Metadata.GetProperties ()) {
							if (string.IsNullOrEmpty (c.Condition) || SafeParseAndEvaluate (project, context, c.Condition, true))
								md [c.Name] = new MSBuildPropertyEvaluated (project.Project, c.Name, c.Value, context.EvaluateString (c.Value));
						}
						((MSBuildPropertyGroupEvaluated)newItem.Metadata).SetProperties (md);
					}
					newItem.SourceItem = item;
					newItem.Condition = item.Condition;
					items.Add (newItem);
				} finally {
					context.ClearItemContext ();
				}
			}
			return true;
		}
Exemplo n.º 12
0
		static void AddItem (ProjectInfo project, MSBuildEvaluationContext context, MSBuildItem item, MSBuildItemEvaluated it, string include, Regex excludeRegex, bool trueCond)
		{
			// Don't add the result from any item that has an empty include. MSBuild never returns those.
			if (include == string.Empty)
				return;
			
			if (IsIncludeTransform (include)) {
				// This is a transform
				List<MSBuildItemEvaluated> evalItems;
				var transformExp = include.Substring (2, include.Length - 3);
				if (ExecuteTransform (project, context, item, transformExp, out evalItems)) {
					foreach (var newItem in evalItems) {
						project.EvaluatedItemsIgnoringCondition.Add (newItem);
						if (trueCond)
							project.EvaluatedItems.Add (newItem);
					}
				}
			} else if (include.IndexOf ('*') != -1) {
				var path = include;
				if (path == "**" || path.EndsWith ("\\**"))
					path = path + "/*";
				var subpath = path.Split ('\\');
				foreach (var eit in ExpandWildcardFilePath (project, project.Project, context, item, project.Project.BaseDirectory, FilePath.Null, false, subpath, 0)) {
					if (excludeRegex != null && excludeRegex.IsMatch (eit.Include))
						continue;
					project.EvaluatedItemsIgnoringCondition.Add (eit);
					if (trueCond)
						project.EvaluatedItems.Add (eit);
				}
			} else if (include != it.Include) {
				if (excludeRegex != null && excludeRegex.IsMatch (include))
					return;
				it = CreateEvaluatedItem (context, project, project.Project, item, include);
				project.EvaluatedItemsIgnoringCondition.Add (it);
				if (trueCond)
					project.EvaluatedItems.Add (it);
			} else {
				if (excludeRegex != null && excludeRegex.IsMatch (include))
					return;
				project.EvaluatedItemsIgnoringCondition.Add (it);
				if (trueCond)
					project.EvaluatedItems.Add (it);
			}
		}
		static MSBuildItemEvaluated CreateEvaluatedItem (MSBuildEvaluationContext context, MSBuildProject project, MSBuildItem sourceItem, string include)
		{
			var it = new MSBuildItemEvaluated (project, sourceItem.Name, sourceItem.Include, include);
			var md = new Dictionary<string,MSBuildPropertyEvaluated> ();
			foreach (var c in sourceItem.Metadata.GetProperties ()) {
				md [c.Name] = new MSBuildPropertyEvaluated (project, c.Name, c.Value, context.EvaluateString (c.Value));
			}
			((MSBuildPropertyGroupEvaluated)it.Metadata).SetProperties (md);
			it.SourceItem = sourceItem;
			it.Condition = sourceItem.Condition;
			return it;
		}