diff --git a/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs b/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs index 79dee28..11e5fba 100644 --- a/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs +++ b/src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs @@ -7,10 +7,10 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; - +using System.Linq; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; - +using NuGet.Frameworks; using NuGet.LibraryModel; using NuGet.Versioning; @@ -41,10 +41,20 @@ public class PharmacistNuGetTask : Task, IEnableLogger [Required] public ITaskItem[] PackageReferences { get; set; } + /// + /// Gets or sets the guids of the project types. + /// + public string ProjectTypeGuids { get; set; } + + /// + /// Gets or sets the version of the project type. + /// + public string TargetFrameworkVersion { get; set; } + /// /// Gets or sets the target framework. /// - public string TargetFramework { get; set; } = DefaultTargetFramework; + public string TargetFramework { get; set; } /// /// Gets or sets the output file. @@ -64,40 +74,22 @@ public override bool Execute() return false; } - if (string.IsNullOrWhiteSpace(TargetFramework)) + var nugetFrameworks = GetTargetFrameworks(); + if (nugetFrameworks == null) { - TargetFramework = DefaultTargetFramework; + Log.LogError("Neither TargetFramework nor ProjectTypeGuids have been correctly set."); + return false; } using (var writer = new StreamWriter(Path.Combine(OutputFile))) { - var packages = new List(); - - // Include all package references that aren't ourselves. - foreach (var packageReference in PackageReferences) - { - var include = packageReference.GetMetadata("PackageName"); - - if (ExclusionPackageReferenceSet.Contains(include)) - { - continue; - } - - if (!VersionRange.TryParse(packageReference.GetMetadata("Version"), out var nuGetVersion)) - { - this.Log().Error($"Package {include} does not have a valid Version."); - continue; - } - - var packageIdentity = new LibraryRange(include, nuGetVersion, LibraryDependencyTarget.Package); - packages.Add(packageIdentity); - } + var packages = GetPackages(); ObservablesForEventGenerator.WriteHeader(writer, packages).ConfigureAwait(false).GetAwaiter().GetResult(); try { - ObservablesForEventGenerator.ExtractEventsFromNuGetPackages(writer, packages, TargetFramework.ToFrameworks()).GetAwaiter().GetResult(); + ObservablesForEventGenerator.ExtractEventsFromNuGetPackages(writer, packages, nugetFrameworks).GetAwaiter().GetResult(); } catch (Exception ex) { @@ -108,5 +100,56 @@ public override bool Execute() return true; } + + private IReadOnlyCollection GetTargetFrameworks() + { + IReadOnlyCollection nugetFrameworks; + if (!string.IsNullOrWhiteSpace(TargetFramework)) + { + nugetFrameworks = TargetFramework.ToFrameworks(); + } + else + { + if (string.IsNullOrWhiteSpace(ProjectTypeGuids) || string.IsNullOrWhiteSpace(TargetFrameworkVersion)) + { + return null; + } + + var splitProjectTypeGuids = ProjectTypeGuids + .Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries) + .Select(x => new Guid(x.Trim())); + + nugetFrameworks = new List { splitProjectTypeGuids.GetTargetFramework(TargetFrameworkVersion) }; + } + + return nugetFrameworks; + } + + private List GetPackages() + { + var packages = new List(); + + // Include all package references that aren't ourselves. + foreach (var packageReference in PackageReferences) + { + var include = packageReference.GetMetadata("PackageName"); + + if (ExclusionPackageReferenceSet.Contains(include)) + { + continue; + } + + if (!VersionRange.TryParse(packageReference.GetMetadata("Version"), out var nuGetVersion)) + { + this.Log().Error($"Package {include} does not have a valid Version."); + continue; + } + + var packageIdentity = new LibraryRange(include, nuGetVersion, LibraryDependencyTarget.Package); + packages.Add(packageIdentity); + } + + return packages; + } } } diff --git a/src/Pharmacist.MsBuild.NuGet/ProjectGuidToTargetFramework.cs b/src/Pharmacist.MsBuild.NuGet/ProjectGuidToTargetFramework.cs new file mode 100644 index 0000000..2317d40 --- /dev/null +++ b/src/Pharmacist.MsBuild.NuGet/ProjectGuidToTargetFramework.cs @@ -0,0 +1,38 @@ +// 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 2bd89ee..62283b2 100644 --- a/src/Pharmacist.MsBuild.NuGet/targets/Pharmacist.MSBuild.targets +++ b/src/Pharmacist.MsBuild.NuGet/targets/Pharmacist.MSBuild.targets @@ -45,6 +45,8 @@