From 536120cf28c7eae8ac87a291c3c699bf6fdf779b Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Fri, 2 Mar 2018 15:41:58 +0000 Subject: [PATCH 1/8] [Xamarin.Android.Build.Tasks] Better support for netstandard libraries. Fixes #1154, #1162 Netstandard packages sometimes ship with both reference and implementation assemblies. The Nuget build task `ResolveNuGetPackageAssets` only resolves the `ref` version of the assemblies. There does not seem to be away way to FORCE Nuget to resolve the lib one. How .net Core manages to do this is still a mistery. That said the Nuget `ResolveNuGetPackageAssets` does give us a hint as to how to use the `project.assets.json` file to figure out what `lib` version of the package we should be including. This commit reworks `ResolveAssemblies` to attempt to map the `ref` to a `lib` if we find a Referenece Assembly. Historically we just issue a warning (which will probably be ignored), but now we will use the `project.assets.json` file to find the implementation version of the `ref` assembly. We need to be using `Nuget.ProjectModel` since it an API for querying the `project.assets.json`. We make use of the Nuget build properties `$(ProjectLockFile)` for the location of the `project.assets.json` , `$(NuGetPackageRoot)` for the root folder of the Nuget packages and `$(NuGetTargetMoniker)` for resolving which `TargetFrameworks` we are looking for. All of these properties should be set by Nuget. If they are not we should fallback to the default behaviour and just issue the warning. { "version": 3, "targets": { "MonoAndroid,Version=v8.1": { "System.IO.Packaging/4.4.0": { "type": "package", "compile": { "ref/netstandard1.3/System.IO.Packaging.dll": {} }, "runtime": { "lib/netstandard1.3/System.IO.Packaging.dll": {} } }, } } } The code above is a cut down sample of the `project.assets.json`. So our code will first resolve the `targets`. We use `$(NuGetTargetMoniker)` to do this. For an android project this should have a value of `MonoAndroid,Version=v8.1`. Note we do NOT need to worry about the version here. When Nuget restores the packages it creates the file so it will use the correct version. Next we try to find the `System.IO.Packaging`. We need to look at the `lockFile.Libraries` to get information about the install path in the Nuget folder, and then `target.Libraries` to pick out the `runtime` item. Once we have resolved the path we need to then combine that with the `$(NuGetPackageRoot)` to get the full path to the new library. If at any point during all of this code we don't get what we expect (i.e a null) we should abort and just issue the warning. The only real concern with this is if the format of the `project.assets.json` file changes. It is not ment to be edited by a human so there is the possibiltity that the Nuget team will decide to either change the schema or even migrate to a new file format. Hopefully we can just update the `Nuget` nuggets if that happens. --- .../Tasks/ResolveAssemblies.cs | 55 ++++++++++++++++++- .../PackagingTest.cs | 34 +++++++++++- .../Xamarin.Android.Build.Tests.csproj | 2 +- .../Xamarin.Android.Build.Tasks.csproj | 48 +++++++++++++++- .../Xamarin.Android.Common.targets | 3 + .../packages.config | 13 +++++ 6 files changed, 149 insertions(+), 6 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs index 45ddd4d28bf..e273bcb8e42 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs @@ -10,6 +10,9 @@ using MonoDroid.Tuner; using System.IO; using Xamarin.Android.Tools; +using NuGet.Common; +using NuGet.Frameworks; +using NuGet.ProjectModel; using Java.Interop.Tools.Cecil; @@ -24,6 +27,12 @@ public class ResolveAssemblies : Task [Required] public string ReferenceAssembliesDirectory { get; set; } + public string ProjectAssetFile { get; set; } + + public string TargetMoniker { get; set; } + + public string NuGetPackageRoot { get; set; } + public string I18nAssemblies { get; set; } public string LinkMode { get; set; } @@ -57,6 +66,9 @@ bool Execute (DirectoryAssemblyResolver resolver) Log.LogDebugMessage (" I18nAssemblies: {0}", I18nAssemblies); Log.LogDebugMessage (" LinkMode: {0}", LinkMode); Log.LogDebugTaskItems (" Assemblies:", Assemblies); + Log.LogDebugMessage (" ProjectAssetFile: {0}", ProjectAssetFile); + Log.LogDebugMessage (" NuGetPackageRoot: {0}", NuGetPackageRoot); + Log.LogDebugMessage (" TargetMoniker: {0}", TargetMoniker); foreach (var dir in ReferenceAssembliesDirectory.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) resolver.SearchDirectories.Add (dir); @@ -65,6 +77,11 @@ bool Execute (DirectoryAssemblyResolver resolver) var topAssemblyReferences = new List (); + LockFile lockFile = null; + if (!string.IsNullOrEmpty (ProjectAssetFile) && File.Exists (ProjectAssetFile)) { + lockFile = LockFileUtilities.GetLockFile (ProjectAssetFile, NullLogger.Instance); + } + try { foreach (var assembly in Assemblies) { var assembly_path = Path.GetDirectoryName (assembly.ItemSpec); @@ -77,8 +94,13 @@ bool Execute (DirectoryAssemblyResolver resolver) if (assemblyDef == null) throw new InvalidOperationException ("Failed to load assembly " + assembly.ItemSpec); if (MonoAndroidHelper.IsReferenceAssembly (assemblyDef)) { - Log.LogWarning ($"Ignoring {assembly_path} as it is a Reference Assembly"); - continue; + // Resolve "runtime" library + if (lockFile != null) + assemblyDef = ResolveRuntimeAssemblyForReferenceAssembly (lockFile, resolver, assemblyDef.Name); + if (lockFile == null || assemblyDef == null) { + Log.LogWarning ($"Ignoring {assembly_path} as it is a Reference Assembly"); + continue; + } } topAssemblyReferences.Add (assemblyDef); assemblies.Add (Path.GetFullPath (assemblyDef.MainModule.FullyQualifiedName)); @@ -120,6 +142,35 @@ bool Execute (DirectoryAssemblyResolver resolver) readonly List do_not_package_atts = new List (); int indent = 2; + AssemblyDefinition ResolveRuntimeAssemblyForReferenceAssembly (LockFile lockFile, DirectoryAssemblyResolver resolver, AssemblyNameDefinition assemblyNameDefinition) + { + if (string.IsNullOrEmpty(TargetMoniker) || string.IsNullOrEmpty (NuGetPackageRoot) || !Directory.Exists (NuGetPackageRoot)) + return null; + + var framework = NuGetFramework.Parse (TargetMoniker); + if (framework == null) { + Log.LogWarning ($"Could not parse '{TargetMoniker}'"); + return null; + } + var target = lockFile.GetTarget (framework, string.Empty); + if (target == null) { + Log.LogWarning ($"Could not resolve target for '{TargetMoniker}'"); + return null; + } + var libraryPath = lockFile.Libraries.FirstOrDefault (x => x.Name == assemblyNameDefinition.Name); + if (libraryPath == null) + return null; + var library = target.Libraries.FirstOrDefault (x => x.Name == assemblyNameDefinition.Name); + if (library == null) + return null; + var runtime = library.RuntimeAssemblies.FirstOrDefault (); + if (runtime == null) + return null; + var path = Path.Combine (NuGetPackageRoot, libraryPath.Path, runtime.Path); + Log.LogDebugMessage ($"Attempting to load {path}"); + return resolver.Load (path, forceLoad: true); + } + void AddAssemblyReferences (DirectoryAssemblyResolver resolver, ICollection assemblies, AssemblyDefinition assembly, bool topLevel) { var fqname = assembly.MainModule.FullyQualifiedName; diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs index e77fda6c915..9b463baf586 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs @@ -280,9 +280,14 @@ public partial class App : Application public App() { - JsonConvert.DeserializeObject(""test""); - package = Package.Open (""""); + try { + JsonConvert.DeserializeObject(""test""); + package = Package.Open (""""); + } catch { + } InitializeComponent(); + + MainPage = new ContentPage (); } protected override void OnStart() @@ -320,6 +325,7 @@ protected override void OnResume() IsRelease = true, UseLatestPlatformSdk = true, References = { + new BuildItem.Reference ("Java.Interop.Export"), new BuildItem.Reference ("Mono.Android.Export"), new BuildItem.ProjectReference ($"..\\{netStandardProject.ProjectName}\\{netStandardProject.ProjectName}.csproj", netStandardProject.ProjectName, netStandardProject.ProjectGuid), @@ -337,6 +343,29 @@ protected override void OnResume() KnownPackages.XamarinForms_2_3_4_231, } }; + app.MainActivity = @"using System; +using Android.App; +using Android.Content; +using Android.Runtime; +using Android.Views; +using Android.Widget; +using Android.OS; +using XamFormsSample; + +namespace App1 +{ + [Activity (Label = ""App1"", MainLauncher = true, Icon = ""@drawable/icon"")] + public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { + protected override void OnCreate (Bundle bundle) + { + base.OnCreate (bundle); + + global::Xamarin.Forms.Forms.Init (this, bundle); + + LoadApplication (new App ()); + } + } + }"; app.SetProperty (KnownProperties.AndroidSupportedAbis, "x86;armeabi-v7a"); var expectedFiles = new string [] { "Java.Interop.dll", @@ -346,6 +375,7 @@ protected override void OnResume() "System.dll", "System.Runtime.Serialization.dll", "System.IO.Packaging.dll", + "System.IO.Compression.dll", "Mono.Android.Export.dll", "App1.dll", "FormsViewGroup.dll", diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Xamarin.Android.Build.Tests.csproj b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Xamarin.Android.Build.Tests.csproj index cd5bbf7b8bc..452cf421100 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Xamarin.Android.Build.Tests.csproj +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Xamarin.Android.Build.Tests.csproj @@ -7,7 +7,7 @@ Library Xamarin.Android.Build.Tests Xamarin.Android.Build.Tests - v4.5.1 + v4.6.2 diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj index a7da157e62e..d10ad9a8bc3 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj @@ -9,7 +9,7 @@ Xamarin.Android.Tasks Xamarin.Android.Build.Tasks 512 - v4.5.1 + v4.6.2 2.0 @@ -50,6 +50,52 @@ ..\..\packages\FSharp.Core.3.1.2.5\lib\net40\FSharp.Core.dll + + ..\..\packages\Newtonsoft.Json.11.0.1\lib\net45\Newtonsoft.Json.dll + + + ..\..\packages\NuGet.Frameworks.4.6.0\lib\net46\NuGet.Frameworks.dll + + + ..\..\packages\NuGet.Common.4.6.0\lib\net46\NuGet.Common.dll + + + + ..\..\packages\NuGet.Configuration.4.6.0\lib\net46\NuGet.Configuration.dll + + + + ..\..\packages\NuGet.Versioning.4.6.0\lib\net46\NuGet.Versioning.dll + + + ..\..\packages\NuGet.LibraryModel.4.6.0\lib\net46\NuGet.LibraryModel.dll + + + ..\..\packages\NuGet.Packaging.Core.4.6.0\lib\net46\NuGet.Packaging.Core.dll + + + ..\..\packages\NuGet.Packaging.4.6.0\lib\net46\NuGet.Packaging.dll + + + ..\..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll + + + + + ..\..\packages\System.Runtime.InteropServices.4.3.0\lib\net462\System.Runtime.InteropServices.dll + + + ..\..\packages\NuGet.Protocol.4.6.0\lib\net46\NuGet.Protocol.dll + + + + + + + ..\..\packages\NuGet.DependencyResolver.Core.4.6.0\lib\net46\NuGet.DependencyResolver.Core.dll + + + ..\..\packages\NuGet.ProjectModel.4.6.0\lib\net46\NuGet.ProjectModel.dll diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index cf3a4495ca1..283a0ec82f0 100755 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -1591,6 +1591,9 @@ because xbuild doesn't support framework reference assemblies. Assemblies="@(FilteredAssemblies)" I18nAssemblies="$(MandroidI18n)" LinkMode="$(AndroidLinkMode)" + ProjectAssetFile="$(ProjectLockFile)" + NuGetPackageRoot="$(NuGetPackageRoot)" + TargetMoniker="$(NuGetTargetMoniker)" ReferenceAssembliesDirectory="$(TargetFrameworkDirectory)"> diff --git a/src/Xamarin.Android.Build.Tasks/packages.config b/src/Xamarin.Android.Build.Tasks/packages.config index 25cedecb4d5..78c0fa586fb 100644 --- a/src/Xamarin.Android.Build.Tasks/packages.config +++ b/src/Xamarin.Android.Build.Tasks/packages.config @@ -3,4 +3,17 @@ + + + + + + + + + + + + + \ No newline at end of file From df6c979906321cf758e3d42cbd43487e9667eb9c Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Tue, 13 Mar 2018 16:25:49 +0000 Subject: [PATCH 2/8] Removed JAva.Interop.Export --- .../Tests/Xamarin.Android.Build.Tests/PackagingTest.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs index 9b463baf586..19f5704550e 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs @@ -325,7 +325,6 @@ protected override void OnResume() IsRelease = true, UseLatestPlatformSdk = true, References = { - new BuildItem.Reference ("Java.Interop.Export"), new BuildItem.Reference ("Mono.Android.Export"), new BuildItem.ProjectReference ($"..\\{netStandardProject.ProjectName}\\{netStandardProject.ProjectName}.csproj", netStandardProject.ProjectName, netStandardProject.ProjectGuid), From 098d8614ec3f20625b56c118067735998e6d8ee3 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Tue, 13 Mar 2018 16:53:12 +0000 Subject: [PATCH 3/8] ff --- .../Utilities/NuGetLogger.cs | 70 +++++++++++++++++++ .../Xamarin.Android.Build.Tasks.csproj | 1 + 2 files changed, 71 insertions(+) create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs b/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs new file mode 100644 index 00000000000..2306124dccb --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs @@ -0,0 +1,70 @@ +using System; +using NuGet.Common; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks { + + class NuGetLogger : ILogger { + TaskLoggingHelper log; + + public NuGetLogger (TaskLoggingHelper log) + { + this.log = log; + } + + public void Log (LogLevel level, string data) + { + log.LogMessage (data); + } + + public void Log (ILogMessage message) + { + log.LogMessage (message.Message); + } + + public System.Threading.Tasks.Task LogAsync (LogLevel level, string data) + { + return System.Threading.Tasks.Task.Run (() => Log (level, data)); + } + + public System.Threading.Tasks.Task LogAsync (ILogMessage message) + { + return System.Threading.Tasks.Task.Run (() => Log (message)); + } + + public void LogDebug (string data) + { + Log (LogLevel.Debug, data); + } + + public void LogError (string data) + { + Log (LogLevel.Debug, data); + } + + public void LogInformation (string data) + { + Log (LogLevel.Debug, data); + } + + public void LogInformationSummary (string data) + { + Log (LogLevel.Debug, data); + } + + public void LogMinimal (string data) + { + Log (LogLevel.Debug, data); + } + + public void LogVerbose (string data) + { + Log (LogLevel.Debug, data); + } + + public void LogWarning (string data) + { + Log (LogLevel.Debug, data); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj index d10ad9a8bc3..a48277a0481 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj @@ -553,6 +553,7 @@ + desugar_deploy.jar PreserveNewest From 44519173ede18d6ef09acf3c9a60a562b738a1d0 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Tue, 13 Mar 2018 17:13:31 +0000 Subject: [PATCH 4/8] Add NuGetLogger --- src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs | 2 +- src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs index e273bcb8e42..4f154ebaebb 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs @@ -79,7 +79,7 @@ bool Execute (DirectoryAssemblyResolver resolver) LockFile lockFile = null; if (!string.IsNullOrEmpty (ProjectAssetFile) && File.Exists (ProjectAssetFile)) { - lockFile = LockFileUtilities.GetLockFile (ProjectAssetFile, NullLogger.Instance); + lockFile = LockFileUtilities.GetLockFile (ProjectAssetFile, new NuGetLogger(Log)); } try { diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs b/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs index 2306124dccb..ac50a17284a 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs @@ -14,12 +14,12 @@ public NuGetLogger (TaskLoggingHelper log) public void Log (LogLevel level, string data) { - log.LogMessage (data); + log.LogDebugMessage (data); } public void Log (ILogMessage message) { - log.LogMessage (message.Message); + log.LogDebugMessage (message.Message); } public System.Threading.Tasks.Task LogAsync (LogLevel level, string data) From 5a93b8571f9937620e9c49103a8193e23e7d9bfc Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Thu, 15 Mar 2018 10:15:38 +0000 Subject: [PATCH 5/8] Added Licence Info --- src/Xamarin.Android.Build.Tasks.tpnitems | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Xamarin.Android.Build.Tasks.tpnitems b/src/Xamarin.Android.Build.Tasks.tpnitems index a085e3423e3..61c2f7350ce 100644 --- a/src/Xamarin.Android.Build.Tasks.tpnitems +++ b/src/Xamarin.Android.Build.Tasks.tpnitems @@ -78,5 +78,47 @@ https://github.com/IronyProject/Irony + + + The MIT License (MIT) + + Copyright (c) 2007 James Newton-King + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + associated documentation files (the "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the + following conditions: + + The above copyright notice and this permission notice shall be included in all copies or substantial + portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT + LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + https://github.com/JamesNK/Newtonsoft.Json + + + + Copyright (c) .NET Foundation and Contributors. + + All rights reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); you may not use + these files except in compliance with the License. You may obtain a copy of the + License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software distributed + under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. See the License for the + specific language governing permissions and limitations under the License. + + https://github.com/NuGet/NuGet.Client + From 26c37013a220d5dab8e329223e66134b86eb04ce Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Thu, 15 Mar 2018 13:13:06 +0000 Subject: [PATCH 6/8] Rework the NuGetLogger --- .../Utilities/NuGetLogger.cs | 60 ++++--------------- 1 file changed, 12 insertions(+), 48 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs b/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs index ac50a17284a..dceb6b06ca9 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs @@ -1,10 +1,11 @@ using System; using NuGet.Common; using Microsoft.Build.Utilities; +using TPL = System.Threading.Tasks; namespace Xamarin.Android.Tasks { - class NuGetLogger : ILogger { + class NuGetLogger : LoggerBase { TaskLoggingHelper log; public NuGetLogger (TaskLoggingHelper log) @@ -12,59 +13,22 @@ public NuGetLogger (TaskLoggingHelper log) this.log = log; } - public void Log (LogLevel level, string data) - { - log.LogDebugMessage (data); + public override void Log(ILogMessage message) { + log.LogDebugMessage ("{0}", message.Message); } - public void Log (ILogMessage message) - { - log.LogDebugMessage (message.Message); + public override void Log(LogLevel level, string data) { + log.LogDebugMessage ("{0}", data); } - public System.Threading.Tasks.Task LogAsync (LogLevel level, string data) - { - return System.Threading.Tasks.Task.Run (() => Log (level, data)); + public override TPL.Task LogAsync(ILogMessage message) { + Log(message); + return TPL.Task.FromResult(0); } - public System.Threading.Tasks.Task LogAsync (ILogMessage message) - { - return System.Threading.Tasks.Task.Run (() => Log (message)); - } - - public void LogDebug (string data) - { - Log (LogLevel.Debug, data); - } - - public void LogError (string data) - { - Log (LogLevel.Debug, data); - } - - public void LogInformation (string data) - { - Log (LogLevel.Debug, data); - } - - public void LogInformationSummary (string data) - { - Log (LogLevel.Debug, data); - } - - public void LogMinimal (string data) - { - Log (LogLevel.Debug, data); - } - - public void LogVerbose (string data) - { - Log (LogLevel.Debug, data); - } - - public void LogWarning (string data) - { - Log (LogLevel.Debug, data); + public override TPL.Task LogAsync(LogLevel level, string data) { + Log(level, data); + return TPL.Task.FromResult(0); } } } From b66647affe27e5a179052a4e9b28a5b2c497bca2 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Thu, 15 Mar 2018 13:41:56 +0000 Subject: [PATCH 7/8] Rework Nuget Fix --- .../Tasks/ResolveAssemblies.cs | 62 +++++++++++-------- .../Utilities/NuGetLogger.cs | 20 +++--- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs index 4f154ebaebb..cf9c352fa2a 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs @@ -18,7 +18,7 @@ namespace Xamarin.Android.Tasks { - public class ResolveAssemblies : Task + public class ResolveAssemblies : AsyncTask { // The user's assemblies to package [Required] @@ -54,21 +54,30 @@ public class ResolveAssemblies : Task public override bool Execute () { - using (var resolver = new DirectoryAssemblyResolver (this.CreateTaskLogger (), loadDebugSymbols: false)) { - return Execute (resolver); - } + System.Threading.Tasks.Task.Run (() => { + using (var resolver = new DirectoryAssemblyResolver (this.CreateTaskLogger (), loadDebugSymbols: false)) { + return Execute (resolver); + } + }, Token).ContinueWith ((t) => { + if (t.Exception != null) { + var ex = t.Exception.GetBaseException (); + LogError (ex.Message + Environment.NewLine + ex.StackTrace); + } + Complete (); + }); + return base.Execute (); } bool Execute (DirectoryAssemblyResolver resolver) { - Log.LogDebugMessage ("ResolveAssemblies Task"); - Log.LogDebugMessage (" ReferenceAssembliesDirectory: {0}", ReferenceAssembliesDirectory); - Log.LogDebugMessage (" I18nAssemblies: {0}", I18nAssemblies); - Log.LogDebugMessage (" LinkMode: {0}", LinkMode); - Log.LogDebugTaskItems (" Assemblies:", Assemblies); - Log.LogDebugMessage (" ProjectAssetFile: {0}", ProjectAssetFile); - Log.LogDebugMessage (" NuGetPackageRoot: {0}", NuGetPackageRoot); - Log.LogDebugMessage (" TargetMoniker: {0}", TargetMoniker); + LogDebugMessage ("ResolveAssemblies Task"); + LogDebugMessage (" ReferenceAssembliesDirectory: {0}", ReferenceAssembliesDirectory); + LogDebugMessage (" I18nAssemblies: {0}", I18nAssemblies); + LogDebugMessage (" LinkMode: {0}", LinkMode); + LogDebugTaskItems (" Assemblies:", Assemblies); + LogDebugMessage (" ProjectAssetFile: {0}", ProjectAssetFile); + LogDebugMessage (" NuGetPackageRoot: {0}", NuGetPackageRoot); + LogDebugMessage (" TargetMoniker: {0}", TargetMoniker); foreach (var dir in ReferenceAssembliesDirectory.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) resolver.SearchDirectories.Add (dir); @@ -76,10 +85,13 @@ bool Execute (DirectoryAssemblyResolver resolver) var assemblies = new HashSet (); var topAssemblyReferences = new List (); + var logger = new NuGetLogger((s) => { + LogDebugMessage ("{0}", s); + }); LockFile lockFile = null; if (!string.IsNullOrEmpty (ProjectAssetFile) && File.Exists (ProjectAssetFile)) { - lockFile = LockFileUtilities.GetLockFile (ProjectAssetFile, new NuGetLogger(Log)); + lockFile = LockFileUtilities.GetLockFile (ProjectAssetFile, logger); } try { @@ -98,7 +110,7 @@ bool Execute (DirectoryAssemblyResolver resolver) if (lockFile != null) assemblyDef = ResolveRuntimeAssemblyForReferenceAssembly (lockFile, resolver, assemblyDef.Name); if (lockFile == null || assemblyDef == null) { - Log.LogWarning ($"Ignoring {assembly_path} as it is a Reference Assembly"); + LogWarning ($"Ignoring {assembly_path} as it is a Reference Assembly"); continue; } } @@ -106,14 +118,14 @@ bool Execute (DirectoryAssemblyResolver resolver) assemblies.Add (Path.GetFullPath (assemblyDef.MainModule.FullyQualifiedName)); } } catch (Exception ex) { - Log.LogError ("Exception while loading assemblies: {0}", ex); + LogError ("Exception while loading assemblies: {0}", ex); return false; } try { foreach (var assembly in topAssemblyReferences) AddAssemblyReferences (resolver, assemblies, assembly, true); } catch (Exception ex) { - Log.LogError ("Exception while loading assemblies: {0}", ex); + LogError ("Exception while loading assemblies: {0}", ex); return false; } @@ -131,10 +143,10 @@ bool Execute (DirectoryAssemblyResolver resolver) ResolvedUserAssemblies = ResolvedAssemblies.Where (p => !MonoAndroidHelper.IsFrameworkAssembly (p.ItemSpec, true)).ToArray (); ResolvedDoNotPackageAttributes = do_not_package_atts.ToArray (); - Log.LogDebugTaskItems (" [Output] ResolvedAssemblies:", ResolvedAssemblies); - Log.LogDebugTaskItems (" [Output] ResolvedUserAssemblies:", ResolvedUserAssemblies); - Log.LogDebugTaskItems (" [Output] ResolvedFrameworkAssemblies:", ResolvedFrameworkAssemblies); - Log.LogDebugTaskItems (" [Output] ResolvedDoNotPackageAttributes:", ResolvedDoNotPackageAttributes); + LogDebugTaskItems (" [Output] ResolvedAssemblies:", ResolvedAssemblies); + LogDebugTaskItems (" [Output] ResolvedUserAssemblies:", ResolvedUserAssemblies); + LogDebugTaskItems (" [Output] ResolvedFrameworkAssemblies:", ResolvedFrameworkAssemblies); + LogDebugTaskItems (" [Output] ResolvedDoNotPackageAttributes:", ResolvedDoNotPackageAttributes); return !Log.HasLoggedErrors; } @@ -149,12 +161,12 @@ AssemblyDefinition ResolveRuntimeAssemblyForReferenceAssembly (LockFile lockFile var framework = NuGetFramework.Parse (TargetMoniker); if (framework == null) { - Log.LogWarning ($"Could not parse '{TargetMoniker}'"); + LogWarning ($"Could not parse '{TargetMoniker}'"); return null; } var target = lockFile.GetTarget (framework, string.Empty); if (target == null) { - Log.LogWarning ($"Could not resolve target for '{TargetMoniker}'"); + LogWarning ($"Could not resolve target for '{TargetMoniker}'"); return null; } var libraryPath = lockFile.Libraries.FirstOrDefault (x => x.Name == assemblyNameDefinition.Name); @@ -167,7 +179,7 @@ AssemblyDefinition ResolveRuntimeAssemblyForReferenceAssembly (LockFile lockFile if (runtime == null) return null; var path = Path.Combine (NuGetPackageRoot, libraryPath.Path, runtime.Path); - Log.LogDebugMessage ($"Attempting to load {path}"); + LogDebugMessage ($"Attempting to load {path}"); return resolver.Load (path, forceLoad: true); } @@ -183,11 +195,11 @@ void AddAssemblyReferences (DirectoryAssemblyResolver resolver, ICollection a.AttributeType.FullName == "Java.Interop.DoNotPackageAttribute")) { string file = (string) att.ConstructorArguments.First ().Value; if (string.IsNullOrWhiteSpace (file)) - Log.LogError ("In referenced assembly {0}, Java.Interop.DoNotPackageAttribute requires non-null file name.", assembly.FullName); + LogError ("In referenced assembly {0}, Java.Interop.DoNotPackageAttribute requires non-null file name.", assembly.FullName); do_not_package_atts.Add (Path.GetFileName (file)); } - Log.LogMessage (MessageImportance.Low, "{0}Adding assembly reference for {1}, recursively...", new string (' ', indent), assembly.Name); + LogMessage ("{0}Adding assembly reference for {1}, recursively...", new string (' ', indent), assembly.Name); indent += 2; // Add this assembly if (!topLevel && assemblies.All (a => new AssemblyNameDefinition (a, null).Name != assembly.Name.Name)) diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs b/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs index dceb6b06ca9..873d6811c35 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/NuGetLogger.cs @@ -6,28 +6,28 @@ namespace Xamarin.Android.Tasks { class NuGetLogger : LoggerBase { - TaskLoggingHelper log; + Action log; - public NuGetLogger (TaskLoggingHelper log) + public NuGetLogger (Action log) { this.log = log; } - public override void Log(ILogMessage message) { - log.LogDebugMessage ("{0}", message.Message); + public override void Log (ILogMessage message) { + log (message.Message); } - public override void Log(LogLevel level, string data) { - log.LogDebugMessage ("{0}", data); + public override void Log (LogLevel level, string data) { + log (data); } - public override TPL.Task LogAsync(ILogMessage message) { - Log(message); + public override TPL.Task LogAsync (ILogMessage message) { + Log (message); return TPL.Task.FromResult(0); } - public override TPL.Task LogAsync(LogLevel level, string data) { - Log(level, data); + public override TPL.Task LogAsync (LogLevel level, string data) { + Log (level, data); return TPL.Task.FromResult(0); } } From b8ee9c3983257ef0b9973593089fc1704b5d3239 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Tue, 20 Mar 2018 15:40:39 +0000 Subject: [PATCH 8/8] Fixed error in csproj --- .../Xamarin.Android.Build.Tasks.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj index a48277a0481..9193e809861 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj @@ -50,6 +50,7 @@ ..\..\packages\FSharp.Core.3.1.2.5\lib\net40\FSharp.Core.dll + ..\..\packages\Newtonsoft.Json.11.0.1\lib\net45\Newtonsoft.Json.dll