Skip to content
Merged
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
88 changes: 80 additions & 8 deletions ILRepack/ConfigMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ internal static void Process(ILRepack repack)
validConfigFiles.RemoveAt(0);
foreach (var configFile in validConfigFiles)
{
MergeElements(firstFile.Root, configFile.Root);
MergeElements(firstFile.Root, configFile.Root, new());
}

CleanupMultipleStartups(firstFile.Root);

var mergedAssemblies = new HashSet<string>(repack.MergedAssemblies.Select(a => a.Name.Name));

CleanupRedirectedAssemblies(firstFile.Root, mergedAssemblies);

firstFile.Save(repack.Options.OutputFile + ".config");
}
catch (Exception e)
Expand All @@ -56,8 +63,63 @@ internal static void Process(ILRepack repack)
}
}

private static void CleanupMultipleStartups(XElement root)
{
var startups = root.Elements("startup");

foreach (var s in startups.Skip(1))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need a .ToArray() here? or does it work fine?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, you might be right.
My test case had 2 startup elements :)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wanna send a follow up or file a bug? or verify that it works fine

{
s.Remove();
}
}

private static void CleanupRedirectedAssemblies(XElement root, HashSet<string> mergedAssemblies)
{
var runtime = root.Element("runtime");
if (runtime == null)
return;

var ns = "urn:schemas-microsoft-com:asm.v1";

var bindings = runtime.Elements(XName.Get("assemblyBinding", ns)).ToArray();

foreach (var binding in bindings)
{
var dependents = binding.Elements(XName.Get("dependentAssembly", ns)).ToArray();

foreach (var dependent in dependents)
{
var identity = dependent.Element(XName.Get("assemblyIdentity", ns));

if (identity == null) continue;

var name = identity.Attribute("name");

if (mergedAssemblies.Contains(name.Value))
{
dependent.Remove();
}
}

if (binding.Elements().Count() == 0)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do !Any()

{
binding.Remove();
}
}
}

// determine which elements we consider the same
private static bool AreEquivalent(XElement a, XElement b)
private static bool ShouldMerge(XElement a, XElement b)
{
if (a.Name == XName.Get("assemblyBinding", "urn:schemas-microsoft-com:asm.v1"))
{
return false;
}

return SameElement(a, b);
}

private static bool SameElement(XElement a, XElement b)
{
if (a.Name != b.Name) return false;
if (!a.HasAttributes && !b.HasAttributes) return true;
Expand All @@ -69,31 +131,41 @@ private static bool AreEquivalent(XElement a, XElement b)
}

// Merge "merged" document B into "source" A
private static void MergeElements(XElement parentA, XElement parentB)
private static void MergeElements(XElement parentA, XElement parentB, HashSet<XElement> state)
{
// merge per-element content from parentB into parentA
//
foreach (XNode childB in parentB.DescendantNodes())
foreach (XNode nodeB in parentB.DescendantNodes())
{
if (childB is XText) continue;
if (nodeB is not XElement childB) continue;

if (state.Contains(childB)) continue;

// merge childB with first equivalent childA
// equivalent childB1, childB2,.. will be combined
//
bool isMatchFound = false;
foreach (XElement childA in parentA.Descendants())
{
if (AreEquivalent(childA, (XElement)childB))
if (ShouldMerge(childA, childB))
{
MergeElements(childA, (XElement)childB);
MergeElements(childA, childB, state);
isMatchFound = true;
break;
}
}

// if there is no equivalent childA, add childB into parentA
//
if (!isMatchFound) parentA.Add(childB);
if (isMatchFound)
return;

parentA.Add(childB);

foreach (var e in childB.DescendantsAndSelf())
{
state.Add(e);
}
}
}
}
Expand Down