-
Notifications
You must be signed in to change notification settings - Fork 564
[.NET 5] NuGet workarounds for Xamarin.Forms projects #4663
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
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
46 changes: 46 additions & 0 deletions
46
...rin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.NuGet.targets
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,46 @@ | ||
| <!-- | ||
| *********************************************************************************************** | ||
| Microsoft.Android.Sdk.NuGet.targets | ||
|
|
||
| This file contains *temporary* workarounds for NuGet in .NET 5. | ||
|
|
||
| *********************************************************************************************** | ||
| --> | ||
|
|
||
| <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
|
|
||
| <UsingTask TaskName="Xamarin.Android.Tasks.FixupNuGetReferences" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" /> | ||
|
|
||
| <PropertyGroup> | ||
| <!-- Clear $(AssetTargetFallback), so only $(PackageTargetFallback) is used. --> | ||
| <AssetTargetFallback></AssetTargetFallback> | ||
| <!-- | ||
| Use $(PackageTargetFallback), even though it is deprecated. | ||
| It doesn't suffer from: https://github.com/NuGet/docs.microsoft.com-nuget/issues/1955 | ||
| --> | ||
| <PackageTargetFallback> | ||
| monoandroid10.0; | ||
| monoandroid90; | ||
| monoandroid81; | ||
| monoandroid80; | ||
| monoandroid70; | ||
| monoandroid60; | ||
| monoandroid50; | ||
| $(PackageTargetFallback); | ||
| </PackageTargetFallback> | ||
| </PropertyGroup> | ||
|
|
||
| <Target Name="_FixupNuGetReferences" AfterTargets="ResolvePackageAssets"> | ||
| <FixupNuGetReferences | ||
| PackageTargetFallback="$(PackageTargetFallback)" | ||
| CopyLocalItems="@(RuntimeCopyLocalItems)"> | ||
| <Output TaskParameter="AssembliesToRemove" ItemName="_AssembliesToRemove" /> | ||
| <Output TaskParameter="AssembliesToAdd" ItemName="Reference" /> | ||
| </FixupNuGetReferences> | ||
| <ItemGroup> | ||
| <RuntimeCopyLocalItems Remove="@(_AssembliesToRemove)" /> | ||
| <ResolvedCompileFileDefinitions Remove="@(_AssembliesToRemove)" /> | ||
| </ItemGroup> | ||
| </Target> | ||
|
|
||
| </Project> |
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
78 changes: 78 additions & 0 deletions
78
src/Xamarin.Android.Build.Tasks/Tasks/FixupNuGetReferences.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,78 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using Microsoft.Build.Framework; | ||
|
|
||
| namespace Xamarin.Android.Tasks | ||
| { | ||
| /// <summary> | ||
| /// This task contains *temporary* workarounds for NuGet in .NET 5. | ||
| /// </summary> | ||
| public class FixupNuGetReferences : AndroidTask | ||
| { | ||
| public override string TaskPrefix => "FNR"; | ||
|
|
||
| [Required] | ||
| public string [] PackageTargetFallback { get; set; } | ||
|
|
||
| public ITaskItem [] CopyLocalItems { get; set; } | ||
|
|
||
| [Output] | ||
| public string [] AssembliesToAdd { get; set; } | ||
|
|
||
| [Output] | ||
| public ITaskItem [] AssembliesToRemove { get; set; } | ||
|
|
||
| public override bool RunTask () | ||
| { | ||
| if (CopyLocalItems == null || CopyLocalItems.Length == 0) | ||
| return true; | ||
|
|
||
| var assembliesToAdd = new Dictionary<string, string> (); | ||
| var assembliesToRemove = new List<ITaskItem> (); | ||
| var fallbackDirectories = new HashSet<string> (); | ||
|
|
||
| foreach (var item in CopyLocalItems) { | ||
| var directory = Path.GetDirectoryName (item.ItemSpec); | ||
| var directoryName = Path.GetFileName (directory); | ||
| Log.LogDebugMessage ($"{directoryName} -> {item.ItemSpec}"); | ||
| if (directoryName == "netstandard2.0") { | ||
| var parent = Directory.GetParent (directory); | ||
| foreach (var nugetDirectory in parent.EnumerateDirectories ()) { | ||
| var name = Path.GetFileName (nugetDirectory.Name); | ||
| foreach (var fallback in PackageTargetFallback) { | ||
| if (!string.Equals (name, fallback, StringComparison.OrdinalIgnoreCase)) | ||
| continue; | ||
| var fallbackDirectory = Path.Combine (parent.FullName, fallback); | ||
| fallbackDirectories.Add (fallbackDirectory); | ||
|
|
||
| // Remove the netstandard assembly, if there is a platform-specific one | ||
| var path = Path.Combine (fallbackDirectory, Path.GetFileName (item.ItemSpec)); | ||
| if (File.Exists (path)) { | ||
| Log.LogDebugMessage ($"Removing: {item.ItemSpec}"); | ||
| assembliesToRemove.Add (item); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Look for any platform-specific assemblies | ||
| foreach (var directory in fallbackDirectories) { | ||
| foreach (var assembly in Directory.GetFiles (directory, "*.dll")) { | ||
| var assemblyName = Path.GetFileName (assembly); | ||
| if (!assembliesToAdd.ContainsKey (assemblyName)) { | ||
| Log.LogDebugMessage ($"Adding: {assembly}"); | ||
| assembliesToAdd.Add (assemblyName, assembly); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| AssembliesToAdd = assembliesToAdd.Values.ToArray (); | ||
| AssembliesToRemove = assembliesToRemove.ToArray (); | ||
|
|
||
| return !Log.HasLoggedErrors; | ||
| } | ||
| } | ||
| } | ||
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
89 changes: 89 additions & 0 deletions
89
...amarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinFormsXASdkProject.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,89 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Text; | ||
|
|
||
| namespace Xamarin.ProjectTools | ||
| { | ||
| public class XamarinFormsXASdkProject : XASdkProject | ||
| { | ||
| static readonly string default_main_activity_cs; | ||
| static readonly string colors_xml; | ||
| static readonly string styles_xml; | ||
| static readonly string Tabbar_xml; | ||
| static readonly string Toolbar_xml; | ||
| static readonly string MainPage_xaml; | ||
| static readonly string MainPage_xaml_cs; | ||
| static readonly string App_xaml; | ||
| static readonly string App_xaml_cs; | ||
|
|
||
| static XamarinFormsXASdkProject () | ||
| { | ||
| var assembly = typeof (XamarinFormsXASdkProject).Assembly; | ||
| using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Forms.MainActivity.cs"))) | ||
| default_main_activity_cs = sr.ReadToEnd (); | ||
| using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Forms.colors.xml"))) | ||
| colors_xml = sr.ReadToEnd (); | ||
| using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Forms.styles.xml"))) | ||
| styles_xml = sr.ReadToEnd (); | ||
| using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.AndroidX.Tabbar.xml"))) | ||
| Tabbar_xml = sr.ReadToEnd (); | ||
| using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.AndroidX.Toolbar.xml"))) | ||
| Toolbar_xml = sr.ReadToEnd (); | ||
| using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Forms.MainPage.xaml"))) | ||
| MainPage_xaml = sr.ReadToEnd (); | ||
| using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Forms.MainPage.xaml.cs"))) | ||
| MainPage_xaml_cs = sr.ReadToEnd (); | ||
| using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Forms.App.xaml"))) | ||
| App_xaml = sr.ReadToEnd (); | ||
| using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Forms.App.xaml.cs"))) | ||
| App_xaml_cs = sr.ReadToEnd (); | ||
| } | ||
|
|
||
|
|
||
| public XamarinFormsXASdkProject (string sdkVersion = "", string outputType = "Exe") | ||
| : base (sdkVersion, outputType) | ||
| { | ||
| PackageReferences.Add (KnownPackages.XamarinForms_4_5_0_617); | ||
|
|
||
| // Workaround for AndroidX, see: https://github.com/xamarin/AndroidSupportComponents/pull/239 | ||
| Imports.Add (new Import (() => "Directory.Build.targets") { | ||
| TextContent = () => | ||
| @"<Project> | ||
| <PropertyGroup> | ||
| <VectorDrawableCheckBuildToolsVersionTaskBeforeTargets /> | ||
| </PropertyGroup> | ||
| </Project>" | ||
| }); | ||
|
|
||
| Sources.Add (new AndroidItem.AndroidResource ("Resources\\values\\colors.xml") { | ||
| TextContent = () => colors_xml, | ||
| }); | ||
| Sources.Add (new AndroidItem.AndroidResource ("Resources\\values\\styles.xml") { | ||
| TextContent = () => styles_xml, | ||
| }); | ||
| Sources.Add (new AndroidItem.AndroidResource ("Resources\\layout\\Tabbar.xml") { | ||
| TextContent = () => Tabbar_xml, | ||
| }); | ||
| Sources.Add (new AndroidItem.AndroidResource ("Resources\\layout\\Toolbar.xml") { | ||
| TextContent = () => Toolbar_xml, | ||
| }); | ||
| Sources.Add (new BuildItem ("EmbeddedResource", "MainPage.xaml") { | ||
| TextContent = MainPageXaml, | ||
| }); | ||
| Sources.Add (new BuildItem.Source ("MainPage.xaml.cs") { | ||
| TextContent = () => ProcessSourceTemplate (MainPage_xaml_cs), | ||
| }); | ||
| Sources.Add (new BuildItem ("EmbeddedResource", "App.xaml") { | ||
| TextContent = () => ProcessSourceTemplate (App_xaml), | ||
| }); | ||
| Sources.Add (new BuildItem.Source ("App.xaml.cs") { | ||
| TextContent = () => ProcessSourceTemplate (App_xaml_cs), | ||
| }); | ||
|
|
||
| MainActivity = default_main_activity_cs; | ||
| } | ||
|
|
||
| protected virtual string MainPageXaml () => ProcessSourceTemplate (MainPage_xaml); | ||
| } | ||
| } |
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
10 changes: 10 additions & 0 deletions
10
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/AndroidX/Tabbar.xml
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,10 @@ | ||
| <androidx.design.widget.TabLayout | ||
| xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:app="http://schemas.android.com/apk/res-auto" | ||
| android:id="@+id/sliding_tabs" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:background="?attr/colorPrimary" | ||
| app:tabIndicatorColor="@android:color/white" | ||
| app:tabGravity="fill" | ||
| app:tabMode="fixed" /> |
6 changes: 6 additions & 0 deletions
6
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/AndroidX/Toolbar.xml
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,6 @@ | ||
| <androidx.appcompat.widget.Toolbar | ||
| xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:id="@+id/toolbar" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:background="?attr/colorPrimary" /> |
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
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.