-
Notifications
You must be signed in to change notification settings - Fork 533
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
[Xamarin.Android.Build.Tasks] reduce System.Linq usage in ManifestDocument #9281
Merged
jonathanpeppers
merged 1 commit into
dotnet:main
from
jonathanpeppers:ManifestDocumentLinq
Sep 4, 2024
Merged
Changes from all commits
Commits
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 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 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 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 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 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 |
---|---|---|
|
@@ -572,33 +572,45 @@ XElement CreateApplicationElement (XElement manifest, string applicationClass, L | |
{ | ||
var application = manifest.Descendants ("application").FirstOrDefault (); | ||
|
||
List<ApplicationAttribute> assemblyAttr = | ||
Assemblies.Select (path => ApplicationAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), cache)) | ||
.Where (attr => attr != null) | ||
.ToList (); | ||
List<MetaDataAttribute> metadata = | ||
Assemblies.SelectMany (path => MetaDataAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), cache)) | ||
.Where (attr => attr != null) | ||
.ToList (); | ||
var properties = | ||
Assemblies.SelectMany (path => PropertyAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), cache)) | ||
.Where (attr => attr != null) | ||
.ToList (); | ||
var usesLibraryAttr = | ||
Assemblies.SelectMany (path => UsesLibraryAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), cache)) | ||
.Where (attr => attr != null); | ||
var usesConfigurationAttr = | ||
Assemblies.SelectMany (path => UsesConfigurationAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), cache)) | ||
.Where (attr => attr != null); | ||
List<ApplicationAttribute> assemblyAttr = []; | ||
List<MetaDataAttribute> metadata = []; | ||
List<PropertyAttribute> properties = []; | ||
List<UsesLibraryAttribute> usesLibraryAttr = []; | ||
List<UsesConfigurationAttribute> usesConfigurationAttr = []; | ||
foreach (var assemblyPath in Assemblies) { | ||
var assembly = Resolver.GetAssembly (assemblyPath); | ||
if (ApplicationAttribute.FromCustomAttributeProvider (assembly, cache) is ApplicationAttribute a) { | ||
assemblyAttr.Add (a); | ||
} | ||
foreach (var m in MetaDataAttribute.FromCustomAttributeProvider (assembly, cache)) { | ||
if (m is null) | ||
continue; | ||
metadata.Add (m); | ||
} | ||
foreach (var p in PropertyAttribute.FromCustomAttributeProvider (assembly, cache)) { | ||
if (p is null) | ||
continue; | ||
properties.Add (p); | ||
} | ||
foreach (var u in UsesLibraryAttribute.FromCustomAttributeProvider (assembly, cache)) { | ||
if (u is null) | ||
continue; | ||
usesLibraryAttr.Add (u); | ||
} | ||
foreach (var uc in UsesConfigurationAttribute.FromCustomAttributeProvider (assembly, cache)) { | ||
if (uc is null) | ||
continue; | ||
usesConfigurationAttr.Add (uc); | ||
} | ||
} | ||
|
||
if (assemblyAttr.Count > 1) | ||
throw new InvalidOperationException ("There can be only one [assembly:Application] attribute defined."); | ||
|
||
List<ApplicationAttribute> typeAttr = new List<ApplicationAttribute> (); | ||
List<UsesLibraryAttribute> typeUsesLibraryAttr = new List<UsesLibraryAttribute> (); | ||
List<UsesConfigurationAttribute> typeUsesConfigurationAttr = new List<UsesConfigurationAttribute> (); | ||
List<ApplicationAttribute> typeAttr = []; | ||
foreach (TypeDefinition t in subclasses) { | ||
ApplicationAttribute aa = ApplicationAttribute.FromCustomAttributeProvider (t, cache); | ||
if (aa == null) | ||
if (aa is null) | ||
continue; | ||
|
||
if (!t.IsSubclassOf ("Android.App.Application", cache)) | ||
|
@@ -608,7 +620,7 @@ XElement CreateApplicationElement (XElement manifest, string applicationClass, L | |
metadata.AddRange (MetaDataAttribute.FromCustomAttributeProvider (t, cache)); | ||
properties.AddRange (PropertyAttribute.FromCustomAttributeProvider (t, cache)); | ||
|
||
typeUsesLibraryAttr.AddRange (UsesLibraryAttribute.FromCustomAttributeProvider (t, cache)); | ||
usesLibraryAttr.AddRange (UsesLibraryAttribute.FromCustomAttributeProvider (t, cache)); | ||
} | ||
|
||
if (typeAttr.Count > 1) | ||
|
@@ -619,12 +631,6 @@ XElement CreateApplicationElement (XElement manifest, string applicationClass, L | |
throw new InvalidOperationException ("Application cannot have both a type with an [Application] attribute and an [assembly:Application] attribute."); | ||
|
||
ApplicationAttribute appAttr = assemblyAttr.SingleOrDefault () ?? typeAttr.SingleOrDefault (); | ||
var ull1 = usesLibraryAttr ?? Array.Empty<UsesLibraryAttribute> (); | ||
var ull2 = typeUsesLibraryAttr.AsEnumerable () ?? Array.Empty<UsesLibraryAttribute> (); | ||
var usesLibraryAttrs = ull1.Concat (ull2); | ||
var ucl1 = usesConfigurationAttr ?? Array.Empty<UsesConfigurationAttribute>(); | ||
var ucl2 = typeUsesConfigurationAttr.AsEnumerable () ?? Array.Empty<UsesConfigurationAttribute> (); | ||
var usesConfigurationattrs = ucl1.Concat (ucl2); | ||
bool needManifestAdd = true; | ||
|
||
if (appAttr != null) { | ||
|
@@ -643,14 +649,18 @@ XElement CreateApplicationElement (XElement manifest, string applicationClass, L | |
application = new XElement ("application"); | ||
else | ||
needManifestAdd = false; | ||
application.Add (metadata.Select (md => md.ToElement (PackageName, cache))); | ||
application.Add (properties.Select (md => md.ToElement (PackageName, cache))); | ||
foreach (var m in metadata) { | ||
application.Add (m.ToElement (PackageName, cache)); | ||
} | ||
foreach (var p in properties) { | ||
application.Add (p.ToElement (PackageName, cache)); | ||
} | ||
|
||
if (needManifestAdd) | ||
manifest.Add (application); | ||
|
||
AddUsesLibraries (application, usesLibraryAttrs, cache); | ||
AddUsesConfigurations (application, usesConfigurationattrs, cache); | ||
AddUsesLibraries (application, usesLibraryAttr, cache); | ||
AddUsesConfigurations (application, usesConfigurationAttr, cache); | ||
Comment on lines
-652
to
+663
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. I just used the original list instead. |
||
|
||
if (applicationClass != null && application.Attribute (androidNs + "name") == null) | ||
application.Add (new XAttribute (androidNs + "name", applicationClass)); | ||
|
@@ -780,16 +790,18 @@ XElement ToElement<TAttribute> (TypeDefinition type, string name, Func<TypeDefin | |
if (attr == null) | ||
return null; | ||
|
||
IEnumerable<MetaDataAttribute> metadata = MetaDataAttribute.FromCustomAttributeProvider (type, cache); | ||
IEnumerable<IntentFilterAttribute> intents = IntentFilterAttribute.FromTypeDefinition (type, cache); | ||
var properties = PropertyAttribute.FromCustomAttributeProvider (type, cache); | ||
|
||
XElement element = toElement (attr); | ||
if (element.Attribute (attName) == null) | ||
element.Add (new XAttribute (attName, name)); | ||
element.Add (metadata.Select (md => md.ToElement (PackageName, cache))); | ||
element.Add (intents.Select (intent => intent.ToElement (PackageName))); | ||
element.Add (properties.Select (md => md.ToElement (PackageName, cache))); | ||
foreach (var m in MetaDataAttribute.FromCustomAttributeProvider (type, cache)) { | ||
element.Add (m.ToElement (PackageName, cache)); | ||
} | ||
foreach (var i in IntentFilterAttribute.FromTypeDefinition (type, cache)) { | ||
element.Add (i.ToElement (PackageName)); | ||
} | ||
foreach (var p in PropertyAttribute.FromCustomAttributeProvider (type, cache)) { | ||
element.Add (p.ToElement (PackageName, cache)); | ||
} | ||
if (update != null) | ||
update (attr, element); | ||
return element; | ||
|
@@ -801,18 +813,21 @@ XElement ToProviderElement (TypeDefinition type, string name, TypeDefinitionCach | |
if (attr == null) | ||
return null; | ||
|
||
IEnumerable<MetaDataAttribute> metadata = MetaDataAttribute.FromCustomAttributeProvider (type, cache); | ||
IEnumerable<GrantUriPermissionAttribute> grants = GrantUriPermissionAttribute.FromTypeDefinition (type, cache); | ||
IEnumerable<IntentFilterAttribute> intents = IntentFilterAttribute.FromTypeDefinition (type, cache); | ||
var properties = PropertyAttribute.FromCustomAttributeProvider (type, cache); | ||
|
||
XElement element = attr.ToElement (PackageName, cache); | ||
if (element.Attribute (attName) == null) | ||
element.Add (new XAttribute (attName, name)); | ||
element.Add (metadata.Select (md => md.ToElement (PackageName, cache))); | ||
element.Add (grants.Select (intent => intent.ToElement (PackageName, cache))); | ||
element.Add (intents.Select (intent => intent.ToElement (PackageName))); | ||
element.Add (properties.Select (md => md.ToElement (PackageName, cache))); | ||
foreach (var m in MetaDataAttribute.FromCustomAttributeProvider (type, cache)) { | ||
element.Add (m.ToElement (PackageName, cache)); | ||
} | ||
foreach (var g in GrantUriPermissionAttribute.FromTypeDefinition (type, cache)) { | ||
element.Add (g.ToElement (PackageName, cache)); | ||
} | ||
foreach (var i in IntentFilterAttribute.FromTypeDefinition (type, cache)) { | ||
element.Add (i.ToElement (PackageName)); | ||
} | ||
foreach (var p in PropertyAttribute.FromCustomAttributeProvider (type, cache)) { | ||
element.Add (p.ToElement (PackageName, cache)); | ||
} | ||
|
||
return element; | ||
} | ||
|
@@ -886,13 +901,13 @@ void AddUsesPermissions (XElement application, TypeDefinitionCache cache) | |
if (!application.Parent.Descendants ("uses-permission").Any (x => (string)x.Attribute (attName) == upa.Name)) | ||
application.AddBeforeSelf (upa.ToElement (PackageName, cache)); | ||
} | ||
void AddUsesConfigurations (XElement application, IEnumerable<UsesConfigurationAttribute> configs, TypeDefinitionCache cache) | ||
void AddUsesConfigurations (XElement application, List<UsesConfigurationAttribute> configs, TypeDefinitionCache cache) | ||
{ | ||
foreach (var uca in configs) | ||
application.Add (uca.ToElement (PackageName, cache)); | ||
} | ||
|
||
void AddUsesLibraries (XElement application, IEnumerable<UsesLibraryAttribute> libraries, TypeDefinitionCache cache) | ||
void AddUsesLibraries (XElement application, List<UsesLibraryAttribute> libraries, TypeDefinitionCache cache) | ||
Comment on lines
-889
to
+910
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. These might allow the C# compiler to automatically turn the |
||
{ | ||
// Add unique libraries to the manifest | ||
foreach (var ula in libraries) | ||
|
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.
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.
It seems like this made lots of locals before, and we didn't really need them?
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 looks like this code above was just to concatinate the lists. But you already do that in the new foreach loop replacement.