Exemple #1
0
        string[] GetImportFiles(ProjectInfo project, MSBuildEvaluationContext context, MSBuildImport import, string extensionsPath)
        {
            if (extensionsPath != null)
            {
                var tempCtx = new MSBuildEvaluationContext(context);
                var mep     = MSBuildProjectService.ToMSBuildPath(null, extensionsPath);
                tempCtx.SetPropertyValue("MSBuildExtensionsPath", mep);
                tempCtx.SetPropertyValue("MSBuildExtensionsPath32", mep);
                tempCtx.SetPropertyValue("MSBuildExtensionsPath64", mep);
                context = tempCtx;
            }

            var pr = context.EvaluateString(import.Project);

            project.Imports [import] = pr;

            if (!string.IsNullOrEmpty(import.Condition) && !SafeParseAndEvaluate(project, context, import.Condition, true))
            {
                return(null);
            }

            var path     = MSBuildProjectService.FromMSBuildPath(project.Project.BaseDirectory, pr);
            var fileName = Path.GetFileName(path);

            if (fileName.IndexOfAny(new [] { '*', '?' }) == -1)
            {
                return(File.Exists(path) ? new [] { path } : null);
            }
            else
            {
                var files = Directory.GetFiles(Path.GetDirectoryName(path), fileName);
                Array.Sort(files);
                return(files);
            }
        }
Exemple #2
0
 void Evaluate(ProjectInfo project, MSBuildEvaluationContext context, MSBuildProperty prop)
 {
     if (string.IsNullOrEmpty(prop.Condition) || SafeParseAndEvaluate(project, context, prop.Condition, true))
     {
         var val = context.EvaluateString(prop.Value);
         project.Properties [prop.Name] = new PropertyInfo {
             Name = prop.Name, Value = prop.Value, FinalValue = val
         };
         context.SetPropertyValue(prop.Name, val);
     }
 }
        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 Evaluate(ProjectInfo project, MSBuildEvaluationContext context, MSBuildTarget target)
 {
     if (SafeParseAndEvaluate(project, context, target.Condition))
     {
         var newTarget = new MSBuildTarget(target.Name, target.Tasks);
         newTarget.AfterTargets         = context.EvaluateString(target.AfterTargets);
         newTarget.Inputs               = context.EvaluateString(target.Inputs);
         newTarget.Outputs              = context.EvaluateString(target.Outputs);
         newTarget.BeforeTargets        = context.EvaluateString(target.BeforeTargets);
         newTarget.DependsOnTargets     = context.EvaluateString(target.DependsOnTargets);
         newTarget.Returns              = context.EvaluateString(target.Returns);
         newTarget.KeepDuplicateOutputs = context.EvaluateString(target.KeepDuplicateOutputs);
         project.Targets.Add(newTarget);
     }
 }
Exemple #5
0
 MSBuildItemEvaluated Evaluate(ProjectInfo project, MSBuildEvaluationContext context, MSBuildItem item)
 {
     return(CreateEvaluatedItem(context, project, project.Project, item, context.EvaluateString(item.Include)));
 }
		void Evaluate (ProjectInfo project, MSBuildEvaluationContext context, MSBuildTarget target)
		{
			bool condIsTrue = SafeParseAndEvaluate (project, context, target.Condition);
			var newTarget = new MSBuildTarget (target.Name, target.Tasks);
			newTarget.AfterTargets = context.EvaluateString (target.AfterTargets);
			newTarget.Inputs = context.EvaluateString (target.Inputs);
			newTarget.Outputs = context.EvaluateString (target.Outputs);
			newTarget.BeforeTargets = context.EvaluateString (target.BeforeTargets);
			newTarget.DependsOnTargets = context.EvaluateString (target.DependsOnTargets);
			newTarget.Returns = context.EvaluateString (target.Returns);
			newTarget.KeepDuplicateOutputs = context.EvaluateString (target.KeepDuplicateOutputs);
			project.TargetsIgnoringCondition.Add (newTarget);
			if (condIsTrue)
				project.Targets.Add (newTarget);
		}
		string[] GetImportFiles (ProjectInfo project, MSBuildEvaluationContext context, MSBuildImport import, string extensionsPath)
		{
			if (extensionsPath != null) {
				var tempCtx = new MSBuildEvaluationContext (context);
				var mep = MSBuildProjectService.ToMSBuildPath (null, extensionsPath);
				tempCtx.SetPropertyValue ("MSBuildExtensionsPath", mep);
				tempCtx.SetPropertyValue ("MSBuildExtensionsPath32", mep);
				tempCtx.SetPropertyValue ("MSBuildExtensionsPath64", mep);
				context = tempCtx;
			}

			var pr = context.EvaluateString (import.Project);
			project.Imports [import] = pr;

			if (!string.IsNullOrEmpty (import.Condition) && !SafeParseAndEvaluate (project, context, import.Condition, true))
				return null;

			var path = MSBuildProjectService.FromMSBuildPath (project.Project.BaseDirectory, pr);
			var fileName = Path.GetFileName (path);

			if (fileName.IndexOfAny (new [] { '*', '?' }) == -1) {
				return File.Exists (path) ? new [] { path } : null;
			}
			else {
				var files = Directory.GetFiles (Path.GetDirectoryName (path), fileName);
				Array.Sort (files);
				return files;
			}
		}
		MSBuildItemEvaluated Evaluate (ProjectInfo project, MSBuildEvaluationContext context, MSBuildItem item)
		{
			return CreateEvaluatedItem (context, project, project.Project, item, context.EvaluateString (item.Include));
		}
		void Evaluate (ProjectInfo project, MSBuildEvaluationContext context, MSBuildProperty prop)
		{
			if (string.IsNullOrEmpty (prop.Condition) || SafeParseAndEvaluate (project, context, prop.Condition, true)) {
				var val = context.EvaluateString (prop.Value);
				project.Properties [prop.Name] = new PropertyInfo { Name = prop.Name, Value = prop.Value, FinalValue = val };
				context.SetPropertyValue (prop.Name, val);
			}
		}
		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;
		}
		void Evaluate (ProjectInfo project, MSBuildEvaluationContext context, MSBuildItemGroup items)
		{
			bool conditionIsTrue = true;

			if (!string.IsNullOrEmpty (items.Condition))
				conditionIsTrue = SafeParseAndEvaluate (project, context, items.Condition);

			foreach (var item in items.Items) {

				var include = context.EvaluateString (item.Include);
				var exclude = context.EvaluateString (item.Exclude);

				var it = CreateEvaluatedItem (context, project, project.Project, item, include);

				var trueCond = conditionIsTrue && (string.IsNullOrEmpty (it.Condition) || SafeParseAndEvaluate (project, context, it.Condition));

				var excludeRegex = !string.IsNullOrEmpty (exclude) ? new Regex (ExcludeToRegex (exclude)) : null;

				if (it.Include.IndexOf (';') == -1)
					AddItem (project, context, item, it, it.Include, excludeRegex, trueCond);
				else {
					foreach (var inc in it.Include.Split (new [] {';'}, StringSplitOptions.RemoveEmptyEntries))
						AddItem (project, context, item, it, inc, excludeRegex, trueCond);
				}
			}
		}
		internal static bool ExecuteStringTransform (List<MSBuildItemEvaluated> evaluatedItemsCollection, MSBuildEvaluationContext context, string transformExp, out string items)
		{
			// This method works mostly like ExecuteTransform, but instead of returning a list of items, it returns a string as result.
			// Since there is no need to create full blown evaluated items, it can be more efficient than ExecuteTransform.

			items = "";

			string itemName, expression, itemFunction; object [] itemFunctionArgs;
			if (!ParseTransformExpression (context, transformExp, out itemName, out expression, out itemFunction, out itemFunctionArgs))
				return false;

			var transformItems = evaluatedItemsCollection.Where (i => i.Name == itemName).ToArray ();
			if (itemFunction != null) {
				string result; bool ignoreMetadata;
				if (ExecuteSummaryItemFunction (transformItems, itemFunction, itemFunctionArgs, out result)) {
					// The item function returns a value. Just return it.
					items = result;
					return true;
				} else if (ExecuteTransformItemListFunction (ref transformItems, itemFunction, itemFunctionArgs, out ignoreMetadata)) {
					var sb = new StringBuilder ();
					for (int n = 0; n < transformItems.Length; n++) {
						if (n > 0)
							sb.Append (';');
						sb.Append (transformItems[n].Include);
					}	
					items = sb.ToString ();
					return true;
				}
			}

			var sbi = new StringBuilder ();

			int count = 0;
			foreach (var eit in transformItems) {
				context.SetItemContext (eit.Include, null, eit.Metadata);
				try {
					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;

					if (count++ > 0)
						sbi.Append (';');
					sbi.Append (evaluatedInclude);

				} finally {
					context.ClearItemContext ();
				}
			}
			items = sbi.ToString ();
			return true;
		}
		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;
		}
		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;
		}