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 471adf162ceb..fbb858602982 100644 --- a/tests/xharness/Microsoft.DotNet.XHarness.iOS.Shared/Utilities/ProjectFileExtensions.cs +++ b/tests/xharness/Microsoft.DotNet.XHarness.iOS.Shared/Utilities/ProjectFileExtensions.cs @@ -824,7 +824,25 @@ public static void AddAdditionalDefines (this XmlDocument csproj, string value, return; } - throw new Exception ("Could not find where to add a new DefineConstants node"); + // 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 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}'"; + } + 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); } public static void AddTopLevelProperty (this XmlDocument csproj, string property, string value)