-
Notifications
You must be signed in to change notification settings - Fork 227
Fix several issues with config merger #408
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -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) | ||
|
|
@@ -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)) | ||
| { | ||
| 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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
@@ -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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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