Skip to content

Commit

Permalink
[xharness] Add support for adding PropertyGroups for unknown platform…
Browse files Browse the repository at this point in the history
…/configuration values in a csproj. Fixes xamarin/maccore@2277. (#9347)

Fixes xamarin/maccore#2277.
  • Loading branch information
rolfbjarne authored and mandel-macaque committed Oct 21, 2020
1 parent df35a56 commit 8b82663
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<XmlNode> ().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)
Expand Down

0 comments on commit 8b82663

Please sign in to comment.