-
Notifications
You must be signed in to change notification settings - Fork 566
[TrimmableTypeMap] Extract TrimmableTypeMapGenerator from MSBuild task #11034
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
Open
simonrozsival
wants to merge
14
commits into
main
Choose a base branch
from
dev/simonrozsival/extract-trimmable-typemap-generator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
21facfc
[TrimmableTypeMap] Extract TrimmableTypeMapGenerator from MSBuild task
simonrozsival b272b68
Keep per-assembly acw-map generation in MSBuild task
simonrozsival 4be0136
Address review: restore assembly filtering, add input validation
simonrozsival 4f5c013
Split tests: generator logic → TrimmableTypeMap.Tests, MSBuild → Buil…
simonrozsival 79c1d05
Restore AcwMapDirectory doc comment, scan all assemblies
simonrozsival 68f2409
Move ParseTargetFrameworkVersion to GenerateTrimmableTypeMap task
simonrozsival 1183f9d
Address review fixes
simonrozsival ebfab2b
Remove all IO from TrimmableTypeMapGenerator
simonrozsival 7f8afd7
Drop file-path scanner overloads, simplify null checks
simonrozsival 5a931c3
Remove file-path writing overloads from generator library
simonrozsival 2e2461f
Address review: dispose MemoryStreams, add doc-comments, fix formatting
simonrozsival 11698ab
Restore dropped test coverage: incrementality, TFV parsing, no-peers
simonrozsival 1f816bb
Fix CS0246: CecilTypeDefinitionCache -> TypeDefinitionCache
jonathanpeppers 38848b5
Fix CS0104: disambiguate AssemblyDefinition between Cecil and SRM
simonrozsival 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
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
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
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
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
18 changes: 18 additions & 0 deletions
18
src/Microsoft.Android.Sdk.TrimmableTypeMap/NullableExtensions.cs
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Microsoft.Android.Sdk.TrimmableTypeMap; | ||
|
|
||
| // The static methods in System.String are not NRT annotated in netstandard2.0, | ||
| // so we need our own extension methods to make them nullable aware. | ||
| static class NullableExtensions | ||
| { | ||
| public static bool IsNullOrEmpty ([NotNullWhen (false)] this string? str) | ||
| { | ||
| return string.IsNullOrEmpty (str); | ||
| } | ||
|
|
||
| public static bool IsNullOrWhiteSpace ([NotNullWhen (false)] this string? str) | ||
| { | ||
| return string.IsNullOrWhiteSpace (str); | ||
| } | ||
| } |
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
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
83 changes: 83 additions & 0 deletions
83
src/Microsoft.Android.Sdk.TrimmableTypeMap/TrimmableTypeMapGenerator.cs
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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Reflection.PortableExecutable; | ||
|
|
||
| namespace Microsoft.Android.Sdk.TrimmableTypeMap; | ||
|
|
||
| public class TrimmableTypeMapGenerator | ||
| { | ||
| readonly Action<string> log; | ||
|
|
||
| public TrimmableTypeMapGenerator (Action<string> log) | ||
| { | ||
| this.log = log ?? throw new ArgumentNullException (nameof (log)); | ||
| } | ||
|
|
||
| public TrimmableTypeMapResult Execute ( | ||
| IReadOnlyList<(string Name, PEReader Reader)> assemblies, | ||
| Version systemRuntimeVersion, | ||
| HashSet<string> frameworkAssemblyNames) | ||
| { | ||
simonrozsival marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _ = assemblies ?? throw new ArgumentNullException (nameof (assemblies)); | ||
| _ = systemRuntimeVersion ?? throw new ArgumentNullException (nameof (systemRuntimeVersion)); | ||
| _ = frameworkAssemblyNames ?? throw new ArgumentNullException (nameof (frameworkAssemblyNames)); | ||
|
|
||
| var allPeers = ScanAssemblies (assemblies); | ||
| if (allPeers.Count == 0) { | ||
| log ("No Java peer types found, skipping typemap generation."); | ||
| return new TrimmableTypeMapResult ([], [], allPeers); | ||
| } | ||
|
|
||
| var generatedAssemblies = GenerateTypeMapAssemblies (allPeers, systemRuntimeVersion); | ||
| var jcwPeers = allPeers.Where (p => | ||
| !frameworkAssemblyNames.Contains (p.AssemblyName) | ||
| || p.JavaName.StartsWith ("mono/", StringComparison.Ordinal)).ToList (); | ||
| log ($"Generating JCW files for {jcwPeers.Count} types (filtered from {allPeers.Count} total)."); | ||
| var generatedJavaSources = GenerateJcwJavaSources (jcwPeers); | ||
| return new TrimmableTypeMapResult (generatedAssemblies, generatedJavaSources, allPeers); | ||
| } | ||
|
|
||
| List<JavaPeerInfo> ScanAssemblies (IReadOnlyList<(string Name, PEReader Reader)> assemblies) | ||
| { | ||
| using var scanner = new JavaPeerScanner (); | ||
| var peers = scanner.Scan (assemblies); | ||
| log ($"Scanned {assemblies.Count} assemblies, found {peers.Count} Java peer types."); | ||
| return peers; | ||
| } | ||
|
|
||
| List<GeneratedAssembly> GenerateTypeMapAssemblies (List<JavaPeerInfo> allPeers, Version systemRuntimeVersion) | ||
| { | ||
| var peersByAssembly = allPeers.GroupBy (p => p.AssemblyName, StringComparer.Ordinal).OrderBy (g => g.Key, StringComparer.Ordinal); | ||
| var generatedAssemblies = new List<GeneratedAssembly> (); | ||
| var perAssemblyNames = new List<string> (); | ||
| var generator = new TypeMapAssemblyGenerator (systemRuntimeVersion); | ||
| foreach (var group in peersByAssembly) { | ||
| string assemblyName = $"_{group.Key}.TypeMap"; | ||
| perAssemblyNames.Add (assemblyName); | ||
| var peers = group.ToList (); | ||
| var stream = new MemoryStream (); | ||
| generator.Generate (peers, stream, assemblyName); | ||
| stream.Position = 0; | ||
| generatedAssemblies.Add (new GeneratedAssembly (assemblyName, stream)); | ||
| log ($" {assemblyName}: {peers.Count} types"); | ||
simonrozsival marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| var rootStream = new MemoryStream (); | ||
| var rootGenerator = new RootTypeMapAssemblyGenerator (systemRuntimeVersion); | ||
| rootGenerator.Generate (perAssemblyNames, rootStream); | ||
| rootStream.Position = 0; | ||
| generatedAssemblies.Add (new GeneratedAssembly ("_Microsoft.Android.TypeMaps", rootStream)); | ||
| log ($" Root: {perAssemblyNames.Count} per-assembly refs"); | ||
| log ($"Generated {generatedAssemblies.Count} typemap assemblies."); | ||
| return generatedAssemblies; | ||
| } | ||
|
|
||
| List<GeneratedJavaSource> GenerateJcwJavaSources (List<JavaPeerInfo> allPeers) | ||
| { | ||
| var jcwGenerator = new JcwJavaSourceGenerator (); | ||
| var sources = jcwGenerator.GenerateContent (allPeers); | ||
| log ($"Generated {sources.Count} JCW Java source files."); | ||
| return sources.ToList (); | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/Microsoft.Android.Sdk.TrimmableTypeMap/TrimmableTypeMapTypes.cs
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
|
|
||
| namespace Microsoft.Android.Sdk.TrimmableTypeMap; | ||
|
|
||
| public record TrimmableTypeMapResult ( | ||
| IReadOnlyList<GeneratedAssembly> GeneratedAssemblies, | ||
| IReadOnlyList<GeneratedJavaSource> GeneratedJavaSources, | ||
| IReadOnlyList<JavaPeerInfo> AllPeers); | ||
|
|
||
| public record GeneratedAssembly (string Name, MemoryStream Content); | ||
|
|
||
| public record GeneratedJavaSource (string RelativePath, string Content); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.