forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CombineLinkerXmlFiles task (dotnet/linker#1234)
* Add CombineLinkerXmlFiles task This task combines multiple linker xml files into a single, combined xml file. * Fix formatting Commit migrated from dotnet/linker@12dc3cc
- Loading branch information
Showing
3 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/tools/illink/src/ILLink.Tasks/CombineLinkerXmlFiles.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/tools/illink/test/ILLink.Tasks.Tests/CombineLinkerXmlFilesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |