Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CombineLinkerXmlFiles task #1234

Merged
merged 2 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}