Skip to content
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

feature: add project type guid support to support older csproj format #100

Merged
merged 3 commits into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 70 additions & 27 deletions src/Pharmacist.MsBuild.NuGet/PharmacistNuGetTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -41,10 +41,20 @@ public class PharmacistNuGetTask : Task, IEnableLogger
[Required]
public ITaskItem[] PackageReferences { get; set; }

/// <summary>
/// Gets or sets the guids of the project types.
/// </summary>
public string ProjectTypeGuids { get; set; }

/// <summary>
/// Gets or sets the version of the project type.
/// </summary>
public string TargetFrameworkVersion { get; set; }

/// <summary>
/// Gets or sets the target framework.
/// </summary>
public string TargetFramework { get; set; } = DefaultTargetFramework;
public string TargetFramework { get; set; }

/// <summary>
/// Gets or sets the output file.
Expand All @@ -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<LibraryRange>();

// 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)
{
Expand All @@ -108,5 +100,56 @@ public override bool Execute()

return true;
}

private IReadOnlyCollection<NuGetFramework> GetTargetFrameworks()
{
IReadOnlyCollection<NuGetFramework> 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<NuGetFramework> { splitProjectTypeGuids.GetTargetFramework(TargetFrameworkVersion) };
}

return nugetFrameworks;
}

private List<LibraryRange> GetPackages()
{
var packages = new List<LibraryRange>();

// 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;
}
}
}
38 changes: 38 additions & 0 deletions src/Pharmacist.MsBuild.NuGet/ProjectGuidToTargetFramework.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Converts the project guid format into a target framework value.
/// </summary>
internal static class ProjectGuidToTargetFramework
{
private static readonly Dictionary<Guid, string> _guidToFramework = new Dictionary<Guid, string>()
{
[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<Guid> projectGuids, string projectVersionId)
{
foreach (var projectGuid in projectGuids)
{
if (_guidToFramework.TryGetValue(projectGuid, out var targetFrameworkValue))
{
return new NuGetFramework(targetFrameworkValue, new Version(projectVersionId));
}
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
<Target Name="GeneratePharmacist" BeforeTargets="CoreCompile">
<PharmacistNuGetTask PackageReferences="@(PharmacistPackageReference -> WithMetadataValue('InProject', True) )"
TargetFramework="$(TargetFramework)"
TargetFrameworkVersion="$(TargetFrameworkVersion)"
ProjectTypeGuids="$(ProjectTypeGuids)"
OutputFile="$(IntermediateOutputPath)\Pharmacist.NuGet.g.cs" />

<Message Text="Processed Pharmacist Packages" />
Expand Down