From 1e64ca1f3297b3bface1a075f37afd22b04d72e8 Mon Sep 17 00:00:00 2001 From: Glenn <5834289+glennawatson@users.noreply.github.com> Date: Sun, 5 Jan 2020 17:23:57 +1100 Subject: [PATCH] fix: not reading all versions (#102) --- .../Generation/ReflectionExtensions.cs | 4 +- .../DelegateTemplateNamespaceResolver.cs | 2 +- .../PharmacistNuGetTask.cs | 44 ++++++++++++++----- .../ProjectGuidToTargetFramework.cs | 38 ---------------- .../targets/Pharmacist.MSBuild.targets | 1 + 5 files changed, 36 insertions(+), 53 deletions(-) delete mode 100644 src/Pharmacist.MsBuild.NuGet/ProjectGuidToTargetFramework.cs diff --git a/src/Pharmacist.Core/Generation/ReflectionExtensions.cs b/src/Pharmacist.Core/Generation/ReflectionExtensions.cs index 55a9486..7f2ae66 100644 --- a/src/Pharmacist.Core/Generation/ReflectionExtensions.cs +++ b/src/Pharmacist.Core/Generation/ReflectionExtensions.cs @@ -72,7 +72,7 @@ public static IReadOnlyCollection GetReferenceTypeDefinitionsWi /// /// The compilation to get the type definitions from. /// The list of type definitions. - public static IEnumerable GetPublicNonGenericTypeDefinitions(this ICompilation compilation) + public static IEnumerable GetPublicTypeDefinitions(this ICompilation compilation) { return _publicNonGenericTypeMapping.GetOrAdd( compilation, @@ -179,7 +179,7 @@ private static IEnumerable GetPublicTypeDefinitionsWithEvents(I compilation, comp => { - return comp.GetPublicNonGenericTypeDefinitions() + return comp.GetPublicTypeDefinitions() .Where(x => x.Events.Any(eventInfo => eventInfo.Accessibility == Accessibility.Public)) .ToList(); diff --git a/src/Pharmacist.Core/Generation/Resolvers/DelegateTemplateNamespaceResolver.cs b/src/Pharmacist.Core/Generation/Resolvers/DelegateTemplateNamespaceResolver.cs index 9380b53..de6acab 100644 --- a/src/Pharmacist.Core/Generation/Resolvers/DelegateTemplateNamespaceResolver.cs +++ b/src/Pharmacist.Core/Generation/Resolvers/DelegateTemplateNamespaceResolver.cs @@ -35,7 +35,7 @@ internal class DelegateTemplateNamespaceResolver : INamespaceResolver public IEnumerable Create(ICompilation compilation) { - IEnumerable<(ITypeDefinition typeDefinition, bool isAbstract, IEnumerable methods)> values = compilation.GetPublicNonGenericTypeDefinitions() + IEnumerable<(ITypeDefinition typeDefinition, bool isAbstract, IEnumerable methods)> values = compilation.GetPublicTypeDefinitions() .Where( x => x.Kind != TypeKind.Interface && (!IsMulticastDelegateDerived(x) diff --git a/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs b/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs index 11e5fba..43e6986 100644 --- a/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs +++ b/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs @@ -8,6 +8,7 @@ using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; +using System.Text.RegularExpressions; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using NuGet.Frameworks; @@ -27,7 +28,14 @@ namespace Pharmacist.MsBuild.NuGet [SuppressMessage("Design", "CA1031: Catch specific exceptions", Justification = "Final logging location for exceptions.")] public class PharmacistNuGetTask : Task, IEnableLogger { - private const string DefaultTargetFramework = "netstandard2.0"; + private static readonly Regex _versionRegex = new Regex(@"(\d+\.)?(\d+\.)?(\d+\.)?(\*|\d+)", RegexOptions.Compiled | RegexOptions.CultureInvariant); + private static readonly Dictionary _guidToFramework = new Dictionary() + { + [new Guid("EFBA0AD7-5A72-4C68-AF49-83D382785DCF")] = "MonoAndroid", + [new Guid("6BC8ED88-2882-458C-8E55-DFD12B67127B")] = "Xamarin.iOS", + [new Guid("A5A43C5B-DE2A-4C0C-9213-0A381AF9435A")] = "uap", + [new Guid("A3F8F2AB-B479-4A4A-A458-A89E7DC349F1")] = "Xamarin.Mac", + }; private static readonly ISet ExclusionPackageReferenceSet = new HashSet(StringComparer.InvariantCultureIgnoreCase) { @@ -51,6 +59,11 @@ public class PharmacistNuGetTask : Task, IEnableLogger /// public string TargetFrameworkVersion { get; set; } + /// + /// Gets or sets the version of the project type associated with UWP projects. + /// + public string TargetPlatformVersion { get; set; } + /// /// Gets or sets the target framework. /// @@ -103,23 +116,30 @@ public override bool Execute() private IReadOnlyCollection GetTargetFrameworks() { - IReadOnlyCollection nugetFrameworks; if (!string.IsNullOrWhiteSpace(TargetFramework)) { - nugetFrameworks = TargetFramework.ToFrameworks(); + return TargetFramework.ToFrameworks(); } - else + + var nugetFrameworks = new List(); + + if (string.IsNullOrWhiteSpace(ProjectTypeGuids)) { - if (string.IsNullOrWhiteSpace(ProjectTypeGuids) || string.IsNullOrWhiteSpace(TargetFrameworkVersion)) - { - return null; - } + return null; + } - var splitProjectTypeGuids = ProjectTypeGuids - .Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries) - .Select(x => new Guid(x.Trim())); + var projectGuids = ProjectTypeGuids + .Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries) + .Select(x => new Guid(x.Trim())); - nugetFrameworks = new List { splitProjectTypeGuids.GetTargetFramework(TargetFrameworkVersion) }; + var versionText = string.IsNullOrWhiteSpace(TargetFrameworkVersion) ? TargetFrameworkVersion : TargetPlatformVersion; + foreach (var projectGuid in projectGuids) + { + if (_guidToFramework.TryGetValue(projectGuid, out var targetFrameworkValue)) + { + var versionMatch = new Version(_versionRegex.Match(versionText).Value); + nugetFrameworks.Add(new NuGetFramework(targetFrameworkValue, versionMatch)); + } } return nugetFrameworks; diff --git a/src/Pharmacist.MsBuild.NuGet/ProjectGuidToTargetFramework.cs b/src/Pharmacist.MsBuild.NuGet/ProjectGuidToTargetFramework.cs deleted file mode 100644 index 2317d40..0000000 --- a/src/Pharmacist.MsBuild.NuGet/ProjectGuidToTargetFramework.cs +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved. -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for full license information. - -using System; -using System.Collections.Generic; -using NuGet.Frameworks; - -namespace Pharmacist.MsBuild.NuGet -{ - /// - /// Converts the project guid format into a target framework value. - /// - internal static class ProjectGuidToTargetFramework - { - private static readonly Dictionary _guidToFramework = new Dictionary() - { - [new Guid("EFBA0AD7-5A72-4C68-AF49-83D382785DCF")] = "MonoAndroid", - [new Guid("6BC8ED88-2882-458C-8E55-DFD12B67127B")] = "Xamarin.iOS", - [new Guid("A5A43C5B-DE2A-4C0C-9213-0A381AF9435A")] = "uap", - [new Guid("A3F8F2AB-B479-4A4A-A458-A89E7DC349F1")] = "Xamarin.Mac", - }; - - public static NuGetFramework GetTargetFramework(this IEnumerable projectGuids, string projectVersionId) - { - foreach (var projectGuid in projectGuids) - { - if (_guidToFramework.TryGetValue(projectGuid, out var targetFrameworkValue)) - { - return new NuGetFramework(targetFrameworkValue, new Version(projectVersionId)); - } - } - - return null; - } - } -} diff --git a/src/Pharmacist.MsBuild.NuGet/targets/Pharmacist.MSBuild.targets b/src/Pharmacist.MsBuild.NuGet/targets/Pharmacist.MSBuild.targets index 62283b2..6a58ba1 100644 --- a/src/Pharmacist.MsBuild.NuGet/targets/Pharmacist.MSBuild.targets +++ b/src/Pharmacist.MsBuild.NuGet/targets/Pharmacist.MSBuild.targets @@ -47,6 +47,7 @@ TargetFramework="$(TargetFramework)" TargetFrameworkVersion="$(TargetFrameworkVersion)" ProjectTypeGuids="$(ProjectTypeGuids)" + TargetPlatformVersion="$(TargetPlatformVersion)" OutputFile="$(IntermediateOutputPath)\Pharmacist.NuGet.g.cs" />