Skip to content

[Xamarin.Android.Build.Tasks] Add support for '<AndroidNamespaceReplacement>'. - #6643

Merged
jonpryor merged 5 commits into
mainfrom
namepsace-replacements
Feb 2, 2022
Merged

[Xamarin.Android.Build.Tasks] Add support for '<AndroidNamespaceReplacement>'.#6643
jonpryor merged 5 commits into
mainfrom
namepsace-replacements

Conversation

@jpobst

@jpobstjpobst commented Jan 13, 2022

Copy link
Copy Markdown
Contributor

Context: dotnet/java-interop#727

Add an MSBuild equivalent to the <ns-replace> metadata feature added in dotnet/java-interop#727.

This takes namespace transforms like:

<ItemGroup>
<AndroidNamespaceReplacementInclude='Androidx'Replacement='AndroidX' />
<AndroidNamespaceReplacementInclude='Com'Replacement='' />
<AndroidNamespaceReplacementInclude='Com.Google.'Replacement='Google' />
</ItemGroup>

And places them into a generated msbuild-metadata.xml file that is passed to generator:

<metadata>
<ns-replacesource='Androidx'replacement='AndroidX' />
<ns-replacesource='Com'replacement='' />
<ns-replacesource='Com.Google.'replacement='Google' />
</metadata>

Today, this only supports <ns-replace> metadata, but it could be expanded in the future if we feel that allowing other metadata is desirable. (I am personally skeptical 😁.)

@jpobst
jpobstforce-pushed the namepsace-replacements branch 5 times, most recently from b1ba3b9 to 3776da5CompareJanuary 20, 2022 17:15
@jpobst
jpobst marked this pull request as ready for review January 20, 2022 22:23
@jonpryor

Copy link
Copy Markdown
Contributor

The RemoveEventHandlerResolution failures are fixed by 599fffd.

// ex: obj/Debug/generated/msbuild-metadata.xml
var transform_file = Path.Combine (OutputDirectory, "..", "msbuild-metadata.xml");

var xml = new XDocument ();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure you need the XDocument at all? XElement.WriteTo() also works…

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Updated.

@jonpryor

Copy link
Copy Markdown
Contributor

@jpobst: the new item group should also be documented in: https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/building-apps/build-items.md

xml.Add (root);

foreach (var nt in NamespaceTransforms)
xml.Root.Add (new XElement ("ns-replace", new XAttribute ("source", nt.ItemSpec), new XAttribute ("replacement", nt.GetMetadata ("replacement"))));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we emit a warning/error if %(Replacement) is blank? I'm just thinking if someone put Replace="AndroidX" on accident, they won't know what's wrong?

@jpobstjpobstJan 20, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

%(Replacement) being blank is valid and useful. I guess the question is should we require users to provide the blank attribute Replacement=""? Perhaps we should?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Added and documented new error.

{
var cmd = GetCommandLineBuilder ();

if (NamespaceTransforms?.Any () == true) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is probably fine, but feels weird. Would this be just as good?

Suggested change
if(NamespaceTransforms?.Any()==true){
if(NamespaceTransforms!=null&&NamespaceTransforms.Length>0){

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, those are equivalent. I think Any and null-check operator is generally considered the modern "best practice".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I generally avoid using System.Linq;, but there are some cases where OrderBy() is ok.

This is fine, though.

Comment threadsrc/Xamarin.Android.Build.Tasks/Tasks/Generator.cs
@jpobst
jpobstforce-pushed the namepsace-replacements branch 3 times, most recently from d5e6aca to 476d740CompareJanuary 25, 2022 15:53
@jpobst
jpobstforce-pushed the namepsace-replacements branch from 476d740 to 3607637CompareJanuary 31, 2022 16:19
@jonpryor
jonpryor merged commit 398afd4 into mainFeb 2, 2022
@jonpryor
jonpryor deleted the namepsace-replacements branch February 2, 2022 01:59
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Jan 24, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@jpobst@jonpryor@dellis1972@jonathanpeppers