From 18a86a8dd3c520f30a102e50bb1052410259b1b8 Mon Sep 17 00:00:00 2001 From: Glenn <5834289+glennawatson@users.noreply.github.com> Date: Mon, 24 Feb 2020 11:13:08 +1100 Subject: [PATCH] Fix: requirement on json.net and also ffimage generation (#114) * Fix: requirement on json.net and also ffimage generation * removed unused project --- .../Generators/InstanceEventGenerator.cs | 4 +- .../NuGet/NuGetPackageHelper.cs | 22 +++++ .../ObservablesForEventGenerator.cs | 28 ++++++ src/Pharmacist.Core/Pharmacist.Core.csproj | 1 - .../Pharmacist.MsBuild.NuGet.csproj | 2 +- .../PharmacistNuGetTask.cs | 90 ++++++++++++++----- .../Pharmacist.MsBuild.TargetFramework.csproj | 1 - 7 files changed, 119 insertions(+), 29 deletions(-) diff --git a/src/Pharmacist.Core/Generation/Generators/InstanceEventGenerator.cs b/src/Pharmacist.Core/Generation/Generators/InstanceEventGenerator.cs index d4a8c3e..6a43681 100644 --- a/src/Pharmacist.Core/Generation/Generators/InstanceEventGenerator.cs +++ b/src/Pharmacist.Core/Generation/Generators/InstanceEventGenerator.cs @@ -50,7 +50,7 @@ private static ClassDeclarationSyntax GenerateStaticClass(string namespaceName, .WithLeadingTrivia(XmlSyntaxFactory.GenerateSummarySeeAlsoComment("A class that contains extension methods to wrap events for classes contained within the {0} namespace.", namespaceName)) .WithMembers(List(declarations.Select(declaration => { - var eventsClassName = IdentifierName(declaration.Name + "Events"); + var eventsClassName = IdentifierName("Rx" + declaration.Name + "Events"); return MethodDeclaration(eventsClassName, Identifier("Events")) .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))) .WithParameterList(ParameterList(SingletonSeparatedList( @@ -70,7 +70,7 @@ private static ConstructorDeclarationSyntax GenerateEventWrapperClassConstructor { const string dataParameterName = "data"; var constructor = ConstructorDeclaration( - Identifier(typeDefinition.Name + "Events")) + Identifier("Rx" + typeDefinition.Name + "Events")) .WithModifiers( TokenList( Token(SyntaxKind.PublicKeyword))) diff --git a/src/Pharmacist.Core/NuGet/NuGetPackageHelper.cs b/src/Pharmacist.Core/NuGet/NuGetPackageHelper.cs index 29f3372..681096e 100644 --- a/src/Pharmacist.Core/NuGet/NuGetPackageHelper.cs +++ b/src/Pharmacist.Core/NuGet/NuGetPackageHelper.cs @@ -125,6 +125,28 @@ public static async Task DownloadPackageFilesAndFolder( return await DownloadPackageFilesAndFolder(packageIdentities, frameworks, downloadResource, getDependencies, packageFolders, packageOutputDirectory, token).ConfigureAwait(false); } + /// + /// Gets the best matching PackageIdentity for the specified LibraryRange. + /// + /// The library range to find the best patch for. + /// Optional v3 nuget source. Will default to default nuget.org servers. + /// A optional cancellation token. + /// The best matching PackageIdentity to the specified version range. + public static async Task GetBestMatch(LibraryRange identity, PackageSource? nugetSource = null, CancellationToken token = default) + { + if (identity == null) + { + throw new ArgumentNullException(nameof(identity)); + } + + // Use the provided nuget package source, or use nuget.org + var sourceRepository = new SourceRepository(nugetSource ?? new PackageSource(DefaultNuGetSource), Providers); + + var findPackageResource = await sourceRepository.GetResourceAsync(token).ConfigureAwait(false); + + return await GetBestMatch(identity, findPackageResource, token).ConfigureAwait(false); + } + /// /// Gets the best matching PackageIdentity for the specified LibraryRange. /// diff --git a/src/Pharmacist.Core/ObservablesForEventGenerator.cs b/src/Pharmacist.Core/ObservablesForEventGenerator.cs index f03c5d7..f7a16b1 100644 --- a/src/Pharmacist.Core/ObservablesForEventGenerator.cs +++ b/src/Pharmacist.Core/ObservablesForEventGenerator.cs @@ -228,6 +228,34 @@ public static async Task WriteHeader(TextWriter writer, IReadOnlyCollection + /// Writes the header for a output. + /// + /// The writer where to output to. + /// The packages included in the output. + /// A task to monitor the progress. + public static async Task WriteHeader(TextWriter writer, IReadOnlyCollection packageIdentities) + { + if (writer == null) + { + throw new ArgumentNullException(nameof(writer)); + } + + if (packageIdentities == null) + { + throw new ArgumentNullException(nameof(packageIdentities)); + } + + await WriteHeader(writer).ConfigureAwait(false); + + foreach (var packageIdentity in packageIdentities) + { + await writer.WriteLineAsync($"// Package included: {packageIdentity}").ConfigureAwait(false); + } + + await writer.FlushAsync().ConfigureAwait(false); + } + /// /// Writes the header for a output. /// diff --git a/src/Pharmacist.Core/Pharmacist.Core.csproj b/src/Pharmacist.Core/Pharmacist.Core.csproj index 555724b..9068fef 100644 --- a/src/Pharmacist.Core/Pharmacist.Core.csproj +++ b/src/Pharmacist.Core/Pharmacist.Core.csproj @@ -18,7 +18,6 @@ - diff --git a/src/Pharmacist.MsBuild.NuGet/Pharmacist.MsBuild.NuGet.csproj b/src/Pharmacist.MsBuild.NuGet/Pharmacist.MsBuild.NuGet.csproj index 29006d9..30842a8 100644 --- a/src/Pharmacist.MsBuild.NuGet/Pharmacist.MsBuild.NuGet.csproj +++ b/src/Pharmacist.MsBuild.NuGet/Pharmacist.MsBuild.NuGet.csproj @@ -11,6 +11,7 @@ Produces from NuGet packages System.Reactive Observables for all events within the NuGet packages inside the project.. $(TargetsForTfmSpecificContentInPackage);PackBuildOutputs $(NoWarn);CA1819 + latest @@ -36,6 +37,5 @@ - diff --git a/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs b/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs index 6e019a2..5c78290 100644 --- a/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs +++ b/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs @@ -8,12 +8,14 @@ using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; +using System.Text; using System.Text.RegularExpressions; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using Newtonsoft.Json; using NuGet.Frameworks; using NuGet.LibraryModel; +using NuGet.Packaging.Core; using NuGet.Versioning; using Pharmacist.Core; @@ -30,6 +32,7 @@ namespace Pharmacist.MsBuild.NuGet public class PharmacistNuGetTask : Task, IEnableLogger { private static readonly Regex _versionRegex = new Regex(@"(\d+\.)?(\d+\.)?(\d+\.)?(\*|\d+)", RegexOptions.Compiled | RegexOptions.CultureInvariant); + private static readonly Regex _packageRegex = new Regex("^(.*)/(.*)$", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); private static readonly Dictionary _guidToFramework = new Dictionary() { [new Guid("EFBA0AD7-5A72-4C68-AF49-83D382785DCF")] = "MonoAndroid", @@ -85,7 +88,7 @@ [new Guid("A3F8F2AB-B479-4A4A-A458-A89E7DC349F1")] = "Xamarin.Mac", /// public override bool Execute() { - var jsonSettings = new JsonSerializerSettings() { Formatting = Formatting.Indented }; + var lockFile = OutputFile + ".lock"; var funcLogManager = new FuncLogManager(type => new WrappingFullLogger(new WrappingPrefixLogger(new MsBuildLogger(Log, LogLevel.Debug), type))); Locator.CurrentMutable.RegisterConstant(funcLogManager, typeof(ILogManager)); @@ -104,28 +107,14 @@ public override bool Execute() var packages = GetPackages(); - if (File.Exists(LockFile) && File.Exists(OutputFile)) + var lockPackages = ReadPackages(lockFile); + + if (lockPackages != null && lockPackages.Count == packages.Count && lockPackages.All(packages.Contains)) { - try - { - var fileContents = File.ReadAllText(LockFile); - var lockedLibraries = JsonConvert.DeserializeObject>(fileContents, jsonSettings); - if (lockedLibraries != null && lockedLibraries.Count == packages.Count && lockedLibraries.All(packages.Contains)) - { - return true; - } - } - catch (JsonException ex) - { - Log.LogWarningFromException(ex); - } - catch (Exception ex) - { - Log.LogWarningFromException(ex); - } + return true; } - using (var writer = new StreamWriter(Path.Combine(OutputFile))) + using (var writer = new StreamWriter(Path.Combine(OutputFile), false, Encoding.UTF8)) { ObservablesForEventGenerator.WriteHeader(writer, packages).ConfigureAwait(false).GetAwaiter().GetResult(); @@ -140,7 +129,8 @@ public override bool Execute() } } - File.WriteAllText(LockFile, JsonConvert.SerializeObject(packages, jsonSettings)); + WritePackages(packages, lockFile); + return true; } @@ -175,9 +165,9 @@ private IReadOnlyCollection GetTargetFrameworks() return nugetFrameworks; } - private List GetPackages() + private List GetPackages() { - var packages = new List(); + var packages = new List(); // Include all package references that aren't ourselves. foreach (var packageReference in PackageReferences) @@ -195,11 +185,63 @@ private List GetPackages() continue; } - var packageIdentity = new LibraryRange(include, nuGetVersion, LibraryDependencyTarget.Package); + var libraryRange = new LibraryRange(include, nuGetVersion, LibraryDependencyTarget.Package); + var packageIdentity = NuGetPackageHelper.GetBestMatch(libraryRange).GetAwaiter().GetResult(); packages.Add(packageIdentity); } return packages; } + + private void WritePackages(List packageIdentities, string lockFileName) + { + using var streamWriter = new StreamWriter(lockFileName, false, Encoding.UTF8); + foreach (var packageIdentity in packageIdentities) + { + streamWriter.WriteLine($"{packageIdentity.Id}/{packageIdentity.Version}"); + } + } + + private List ReadPackages(string lockFileName) + { + if (string.IsNullOrWhiteSpace(lockFileName)) + { + throw new ArgumentException("Cannot have a empty lock file name", nameof(lockFileName)); + } + + var packageIdentities = new List(); + + if (File.Exists(lockFileName) == false) + { + return packageIdentities; + } + + try + { + using var streamReader = new StreamReader(lockFileName, Encoding.UTF8, true); + + string line; + + while ((line = streamReader.ReadLine()) != null) + { + if (string.IsNullOrWhiteSpace(line)) + { + continue; + } + + var match = _packageRegex.Match(line); + + var packageIdentity = new PackageIdentity(match.Groups[1].Value, NuGetVersion.Parse(match.Groups[2].Value)); + + packageIdentities.Add(packageIdentity); + } + } + catch + { + packageIdentities = new List(); + } + + return packageIdentities; + } } } diff --git a/src/Pharmacist.MsBuild.TargetFramework/Pharmacist.MsBuild.TargetFramework.csproj b/src/Pharmacist.MsBuild.TargetFramework/Pharmacist.MsBuild.TargetFramework.csproj index dd0b91a..5c3959d 100644 --- a/src/Pharmacist.MsBuild.TargetFramework/Pharmacist.MsBuild.TargetFramework.csproj +++ b/src/Pharmacist.MsBuild.TargetFramework/Pharmacist.MsBuild.TargetFramework.csproj @@ -33,6 +33,5 @@ -