From 7c5d3ded50e389005d9c3bc9055357dc93fd496f Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 8 Sep 2020 16:01:26 +0200 Subject: [PATCH] [xharness] Add support for adding PropertyGroups if none are applicable when adding a property. Some .NET test projects (monotouch-test) require this to set properties for some test variations, and .NET projects are quite minimal, making it likely that they won't have a particular property that needs to be set/modified. This makes it possible to execute the various monotouch-test variations for .NET in xharness (some of the variations still fail though, so they're still ignored by default). --- .../Utilities/ProjectFileExtensions.cs | 64 +++++++++++-------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/tests/xharness/Microsoft.DotNet.XHarness.iOS.Shared/Utilities/ProjectFileExtensions.cs b/tests/xharness/Microsoft.DotNet.XHarness.iOS.Shared/Utilities/ProjectFileExtensions.cs index fe6090c6d4aa..1fd3f74c5f9e 100644 --- a/tests/xharness/Microsoft.DotNet.XHarness.iOS.Shared/Utilities/ProjectFileExtensions.cs +++ b/tests/xharness/Microsoft.DotNet.XHarness.iOS.Shared/Utilities/ProjectFileExtensions.cs @@ -351,7 +351,6 @@ public static void AddMonoBundlingExtraArgs (this XmlDocument csproj, string val public static void AddToNode (this XmlDocument csproj, string node, string value, string platform, string configuration) { var nodes = csproj.SelectNodes ($"//*[local-name() = '{node}']"); - var found = false; foreach (XmlNode mea in nodes) { if (!IsNodeApplicable (mea, platform, configuration)) continue; @@ -359,22 +358,18 @@ public static void AddToNode (this XmlDocument csproj, string node, string value if (mea.InnerText.Length > 0 && mea.InnerText [mea.InnerText.Length - 1] != ' ') mea.InnerText += " "; mea.InnerText += value; - found = true; - } - - if (found) return; + } // The project might not have this node, so create one of none was found. - var propertyGroups = csproj.SelectNodes ("//*[local-name() = 'PropertyGroup' and @Condition]"); - foreach (XmlNode pg in propertyGroups) { - if (!EvaluateCondition (pg, platform, configuration)) - continue; + var propertyGroups = csproj.SelectNodes ("//*[local-name() = 'PropertyGroup' and @Condition]").Cast (); + var propertyGroup = propertyGroups.FirstOrDefault (v => EvaluateCondition (v, platform, configuration)); + if (propertyGroup == null) + propertyGroup = csproj.AddPropertyGroup (platform, configuration); - var mea = csproj.CreateElement (node, csproj.GetNamespace ()); - mea.InnerText = value; - pg.AppendChild (mea); - } + var newNode = csproj.CreateElement (node, csproj.GetNamespace ()); + newNode.InnerText = value; + propertyGroup.AppendChild (newNode); } public static string GetMtouchLink (this XmlDocument csproj, string platform, string configuration) @@ -827,25 +822,40 @@ public static void AddAdditionalDefines (this XmlDocument csproj, string value, return; } + var newPropertyGroup = csproj.AddPropertyGroup (platform, configuration); + var defineConstantsElement = csproj.CreateElement ("DefineConstants", csproj.GetNamespace ()); + defineConstantsElement.InnerText = "$(DefineConstants);" + value; + newPropertyGroup.AppendChild (defineConstantsElement); + } + + static XmlNode AddPropertyGroup (this XmlDocument csproj, string platform, string configuration) + { // Create a new PropertyGroup with the desired condition, and add it just after the last PropertyGroup in the csproj. var projectNode = csproj.SelectSingleNode ("//*[local-name() = 'Project']"); - var lastPropertyGroup = csproj.SelectNodes ("//*[local-name() = 'PropertyGroup']").Cast ().Last (); + var lastPropertyGroup = csproj.SelectNodes ("/*[local-name() = 'Project']/*[local-name() = 'PropertyGroup']").Cast ().Last (); var newPropertyGroup = csproj.CreateElement ("PropertyGroup", csproj.GetNamespace ()); - var conditionAttribute = csproj.CreateAttribute ("Condition"); - var condition = ""; - if (!string.IsNullOrEmpty (platform)) - condition = $"'$(Platform)' == '{platform}'"; - if (!string.IsNullOrEmpty (configuration)) { - if (!string.IsNullOrEmpty (condition)) - condition += " And "; - condition += $"'$(Configuration)' == '{configuration}'"; + if (!string.IsNullOrEmpty (platform) || !string.IsNullOrEmpty (configuration)) { + // Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' " + var conditionAttribute = csproj.CreateAttribute ("Condition"); + var left = string.Empty; + var right = string.Empty; + if (!string.IsNullOrEmpty (configuration)) { + left = "$(Configuration)"; + right = configuration; + } + if (!string.IsNullOrEmpty (platform)) { + if (!string.IsNullOrEmpty (left)) { + left += "|"; + right += "|"; + } + left += "$(Platform)"; + right += platform; + } + conditionAttribute.Value = $"'{left}' == '{right}'"; + newPropertyGroup.Attributes.Append (conditionAttribute); } - conditionAttribute.Value = condition; - newPropertyGroup.Attributes.Append (conditionAttribute); - var defineConstantsElement = csproj.CreateElement ("DefineConstants", csproj.GetNamespace ()); - defineConstantsElement.InnerText = "$(DefineConstants);" + value; - newPropertyGroup.AppendChild (defineConstantsElement); projectNode.InsertAfter (newPropertyGroup, lastPropertyGroup); + return newPropertyGroup; } public static void AddTopLevelProperty (this XmlDocument csproj, string property, string value)