Skip to content

Commit

Permalink
Add CombineLinkerXmlFiles task (#1234)
Browse files Browse the repository at this point in the history
* Add CombineLinkerXmlFiles task

This task combines multiple linker xml files into a single, combined xml file.

* Fix formatting
  • Loading branch information
eerhardt authored Jun 3, 2020
1 parent 83b3efb commit 12dc3cc
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/ILLink.Tasks/CombineLinkerXmlFiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Xml;
using System.Xml.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace ILLink.Tasks
{
/// <summary>
/// Combines multiple linker xml files into a single xml file.
/// </summary>
public class CombineLinkerXmlFiles : Task
{
/// <summary>
/// The individual linker xml files that will be combined into one.
/// </summary>
[Required]
public ITaskItem[] LinkerXmlFiles { get; set; }

/// <summary>
/// The path to the file to generate.
/// </summary>
[Required]
public string CombinedLinkerXmlFile { get; set; }

public override bool Execute ()
{
var combined = new XElement ("linker");
foreach (var linkerXmlFile in LinkerXmlFiles) {
XDocument subFile = XDocument.Load (linkerXmlFile.ItemSpec);

foreach (var element in subFile.Root.Elements ()) {
combined.Add (element);
}
}

var xdoc = new XDocument (combined);

XmlWriterSettings xws = new XmlWriterSettings {
Indent = true,
OmitXmlDeclaration = true
};

using (XmlWriter xw = XmlWriter.Create (CombinedLinkerXmlFile, xws)) {
xdoc.Save (xw);
}

return true;
}
}
}
3 changes: 2 additions & 1 deletion src/ILLink.Tasks/ILLink.Tasks.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Keep these in sync with _ILLinkTasksTFM in Sdk.props. -->
<!-- Keep the netcoreapp TFM in sync with the Mono.Linker.csproj condition below. -->
Expand All @@ -22,6 +22,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="CombineLinkerXmlFiles.cs" />
<Compile Include="LinkTask.cs" />
<Compile Include="CheckEmbeddedRootDescriptor.cs" />
<Compile Include="CompareSizes.cs" />
Expand Down
66 changes: 66 additions & 0 deletions test/ILLink.Tasks.Tests/CombineLinkerXmlFilesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Xml.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Xunit;

namespace ILLink.Tasks.Tests
{
public class CombineLinkerXmlFilesTests
{
[Fact]
public void TestCombineLinkerXmlFiles ()
{
CreateLinkerXml (
new XElement ("linker",
new XElement ("assembly",
new XAttribute ("fullname", "assembly1"),
new XElement ("type",
new XAttribute ("fullname", "Namespace1.Type1"),
new XAttribute ("required", "false")))),
"doc1.xml");

CreateLinkerXml (
new XElement ("linker",
new XElement ("assembly",
new XAttribute ("fullname", "assembly2"),
new XElement ("type",
new XAttribute ("fullname", "*"),
new XAttribute ("required", "true")))),
"doc2.xml");

var xmlFiles = new ITaskItem[] {
new TaskItem ("doc1.xml"),
new TaskItem ("doc2.xml"),
};

var combiner = new CombineLinkerXmlFiles () {
LinkerXmlFiles = xmlFiles,
CombinedLinkerXmlFile = "combined_output.xml"
};

Assert.True (combiner.Execute ());

XDocument combined = XDocument.Load ("combined_output.xml");

string expectedXml = new XElement ("linker",
new XElement ("assembly",
new XAttribute ("fullname", "assembly1"),
new XElement ("type",
new XAttribute ("fullname", "Namespace1.Type1"),
new XAttribute ("required", "false"))),
new XElement ("assembly",
new XAttribute ("fullname", "assembly2"),
new XElement ("type",
new XAttribute ("fullname", "*"),
new XAttribute ("required", "true")))).ToString ();
Assert.Equal (expectedXml, combined.Root.ToString ());
}

private static void CreateLinkerXml (XElement root, string path)
{
var doc = new XDocument ();
doc.Add (root);
doc.Save (path);
}
}
}

0 comments on commit 12dc3cc

Please sign in to comment.