forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetNearestTargetFramework.cs
88 lines (75 loc) · 3.52 KB
/
GetNearestTargetFramework.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Newtonsoft.Json;
using NuGet.Frameworks;
using System.Linq;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Gets the "nearest" framework
/// </summary>
public class GetNearestTargetFramework : TaskBase
{
/// <summary>
/// The target framework of the referring project (in short form or full TFM)
/// </summary>
[Required]
public string ReferringTargetFramework { get; set; }
/// <summary>
/// The target frameworks supported by the project being referenced (in short form or full TFM).
/// </summary>
[Required]
public string[] PossibleTargetFrameworks { get; set; }
/// <summary>
/// The full path to the project with the given possible target frameworks.
/// </summary>
[Required]
public string ProjectFilePath { get; set; }
/// <summary>
/// The entry in <see cref="PossibleTargetFrameworks"/> that is "nearest" to <see cref="ReferringTargetFramework" />.
/// If none of the possible frameworks are compatible with the referring framework.
/// </summary>
[Output]
public string NearestTargetFramework { get; private set; }
protected override void ExecuteCore()
{
if (PossibleTargetFrameworks.Length < 1)
{
Log.LogError(Strings.AtLeastOneTargetFrameworkMustBeSpecified);
return;
}
var referringNuGetFramework = ParseFramework(ReferringTargetFramework);
var possibleNuGetFrameworks = PossibleTargetFrameworks.Select(framework => ParseFramework(framework)).ToList(); // ToList() to force enumeration and error logging.
if (Log.HasLoggedErrors)
{
return;
}
var nearestNuGetFramework = new FrameworkReducer().GetNearest(referringNuGetFramework, possibleNuGetFrameworks);
if (nearestNuGetFramework == null)
{
Log.LogError(Strings.NoCompatibleTargetFramework, ProjectFilePath, ReferringTargetFramework, string.Join("; ", possibleNuGetFrameworks));
return;
}
// Note that there can be more than one spelling of the same target framework (e.g. net45 and net4.5) and
// we must return a value that is spelled exactly the same way as the PossibleTargetFrameworks input. To
// achieve this, we find the index of the returned framework among the set we passed to nuget and use that
// to retrieve a value at the same position in the input.
//
// This is required to guarantee that a project can use whatever spelling appears in $(TargetFrameworks)
// in a condition that compares against $(TargetFramework).
int indexOfNearestFramework = possibleNuGetFrameworks.IndexOf(nearestNuGetFramework);
NearestTargetFramework = PossibleTargetFrameworks[indexOfNearestFramework];
}
private NuGetFramework ParseFramework(string name)
{
var framework = NuGetUtils.ParseFrameworkName(name);
if (framework == null)
{
Log.LogError(Strings.InvalidFrameworkName, framework.ToString());
}
return framework;
}
}
}