Skip to content

Commit

Permalink
Display default platform version for TFMs
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutch committed Apr 26, 2024
1 parent 493c72c commit e1775b9
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 81 deletions.
4 changes: 4 additions & 0 deletions MonoDevelop.MSBuild.Tests/FrameworkInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public void UnknownPlatform (string name)
[TestCase ("net4.8.1", ".NET Framework 4.8.1")]
[TestCase ("wpa81", "Windows Phone (UWP) 8.1")]
[TestCase ("net6.0-android9000.0", ".NET 6.0 with platform-specific APIs for Android 9000.0")]
[TestCase ("net6.0-android", ".NET 6.0 with platform-specific APIs for Android 31.0")]
[TestCase ("net6.0-android32.0", ".NET 6.0 with platform-specific APIs for Android 32.0")]
[TestCase ("net7.0-android", ".NET 7.0 with platform-specific APIs for Android 33.0")]
[TestCase ("net6.0", ".NET 6.0")]
public void FrameworkDescription (string tfm, string description)
{
Expand All @@ -74,7 +76,9 @@ public void FrameworkDescription (string tfm, string description)
[TestCase ("net4.8.1", ".NETFramework,Version=v4.8.1")]
[TestCase ("wpa81", "WindowsPhoneApp,Version=v8.1")]
[TestCase ("net6.0-android9000.0", ".NETCoreApp,Version=v6.0 | Android 9000.0")]
[TestCase ("net6.0-android", ".NETCoreApp,Version=v6.0 | Android 31.0")]
[TestCase ("net6.0-android32.0", ".NETCoreApp,Version=v6.0 | Android 32.0")]
[TestCase ("net7.0-android", ".NETCoreApp,Version=v7.0 | Android 33.0")]
[TestCase ("net6.0", ".NETCoreApp,Version=v6.0")]
public void FrameworkTitle (string tfm, string description)
{
Expand Down
97 changes: 97 additions & 0 deletions MonoDevelop.MSBuild/Schema/FrameworkInfoProvider.PlatformId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#nullable enable

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace MonoDevelop.MSBuild.Schema
{
partial class FrameworkInfoProvider
{
static class KnownPlatform
{
public const string Windows = "Windows";
public const string Android = "Android";
public const string iOS = "iOS";
public const string macOS = "macOS";
public const string tvOS = "tvOS";
public const string MacCatalyst = "MacCatalyst";
public const string Tizen = "Tizen";
public const string Browser = "Browser";

static class LowerId
{
public const string Windows = "windows";
public const string Android = "android";
public const string iOS = "ios";
public const string macOS = "macos";
public const string tvOS = "tvos";
public const string MacCatalyst = "maccatalyst";
public const string Tizen = "tizen";
public const string Browser = "browser";
}

public static string ToLowerCase (string platform) => platform switch {
Windows => LowerId.Windows,
Android => LowerId.Android,
iOS => LowerId.iOS,
macOS => LowerId.macOS,
tvOS => LowerId.tvOS,
MacCatalyst => LowerId.MacCatalyst,
Tizen => LowerId.Tizen,
Browser => LowerId.Browser,
_ => platform.ToLowerInvariant ()
};

public static string ToCanonicalCase (string platform)
{
return CanonicalCaseFromLower (platform)
?? CanonicalCaseFromLower (platform.ToLowerInvariant ())
?? platform;

static string? CanonicalCaseFromLower (string platform) => platform switch {
LowerId.Windows => Windows,
LowerId.Android => Android,
LowerId.iOS => iOS,
LowerId.macOS => macOS,
LowerId.tvOS => tvOS,
LowerId.MacCatalyst => MacCatalyst,
LowerId.Tizen => Tizen,
LowerId.Browser => Browser,
_ => null
};
}

public static bool TryGetDefaultPlatformVersion (int netcoreappMajorVersion, string platform, [NotNullWhen (true)] out Version? defaultPlatformVersion)
{
return defaultPlatformVersions.TryGetValue ((netcoreappMajorVersion, ToLowerCase (platform)), out defaultPlatformVersion);
}

static readonly Dictionary<(int, string), Version> defaultPlatformVersions = new () {
{ (6, LowerId.Android), new Version (31, 0) },
{ (7, LowerId.Android), new Version (33, 0) },
{ (8, LowerId.Android), new Version (34, 0) },
{ (6, LowerId.iOS), new Version (15, 0) },
{ (7, LowerId.iOS), new Version (16, 1) },
{ (8, LowerId.iOS), new Version (17, 2) },
{ (5, LowerId.MacCatalyst), new Version (15, 0) },
{ (7, LowerId.MacCatalyst), new Version (16, 1) },
{ (8, LowerId.MacCatalyst), new Version (17, 2) },
{ (6, LowerId.macOS), new Version (12, 0) },
{ (7, LowerId.macOS), new Version (13, 0) },
{ (8, LowerId.macOS), new Version (14, 2) },
{ (6, LowerId.tvOS), new Version (15, 1) },
{ (7, LowerId.tvOS), new Version (16, 1) },
{ (8, LowerId.tvOS), new Version (17, 1) },
{ (7, LowerId.Tizen), new Version (7, 0) },
{ (8, LowerId.Tizen), new Version (8, 0) },
{ (6, LowerId.Windows), new Version (7, 0) },
{ (7, LowerId.Windows), new Version (7, 0) },
{ (8, LowerId.Windows), new Version (7, 0) },
};
}
}
}
120 changes: 39 additions & 81 deletions MonoDevelop.MSBuild/Schema/FrameworkInfoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text;

using MonoDevelop.MSBuild.Language.Typesystem;
using MonoDevelop.MSBuild.Util;

using NuGet.Frameworks;

Expand All @@ -25,7 +26,7 @@ namespace MonoDevelop.MSBuild.Schema
// for reference, see https://learn.microsoft.com/en-us/dotnet/standard/frameworks
//
// FIXME: this should really be something that schemas can extend
class FrameworkInfoProvider
partial class FrameworkInfoProvider
{
public static FrameworkInfoProvider Instance { get; } = new FrameworkInfoProvider ();

Expand All @@ -35,17 +36,6 @@ class FrameworkInfoProvider
// shortName is a value that can be used for the TargetFramework property. not all frameworks have this.
readonly record struct KnownFramework(string? ShortName, string Identifier, Version Version, string? Profile = null, string? Platform = null, Version? PlatformVersion = null);

static class Platform
{
public const string Windows = "Windows";
public const string Android = "Android";
public const string iOS = "iOS";
public const string macOS = "macOS";
public const string tvOS = "tvOS";
public const string MacCatalyst = "MacCatalyst";
public const string Tizen = "Tizen";
public const string Browser = "Browser";
}

static class FxID
{
Expand Down Expand Up @@ -169,41 +159,41 @@ public FrameworkInfoProvider ()
AddNetCore (5, 0, "net5.0-windows");

AddNetCore (6, 0, "net6.0");
AddNetCore (6, 0, "net6.0-windows", Platform.Windows);
AddNetCore (6, 0, "net6.0-android", Platform.Android);
AddNetCore (6, 0, "net6.0-ios", Platform.iOS);
AddNetCore (6, 0, "net6.0-maccatalyst", Platform.MacCatalyst);
AddNetCore (6, 0, "net6.0-macos", Platform.macOS);
AddNetCore (6, 0, "net6.0-tvos", Platform.tvOS);
AddNetCore (6, 0, "net6.0-windows", KnownPlatform.Windows);
AddNetCore (6, 0, "net6.0-android", KnownPlatform.Android);
AddNetCore (6, 0, "net6.0-ios", KnownPlatform.iOS);
AddNetCore (6, 0, "net6.0-maccatalyst", KnownPlatform.MacCatalyst);
AddNetCore (6, 0, "net6.0-macos", KnownPlatform.macOS);
AddNetCore (6, 0, "net6.0-tvos", KnownPlatform.tvOS);

AddNetCore (7, 0, "net7.0");
AddNetCore (7, 0, "net7.0-windows", Platform.Windows);
AddNetCore (7, 0, "net7.0-android", Platform.Android);
AddNetCore (7, 0, "net7.0-ios", Platform.iOS);
AddNetCore (7, 0, "net7.0-maccatalyst", Platform.MacCatalyst);
AddNetCore (7, 0, "net7.0-macos", Platform.macOS);
AddNetCore (7, 0, "net7.0-tvos", Platform.tvOS);
AddNetCore (7, 0, "net7.0-tizen", Platform.Tizen);
AddNetCore (7, 0, "net7.0-windows", KnownPlatform.Windows);
AddNetCore (7, 0, "net7.0-android", KnownPlatform.Android);
AddNetCore (7, 0, "net7.0-ios", KnownPlatform.iOS);
AddNetCore (7, 0, "net7.0-maccatalyst", KnownPlatform.MacCatalyst);
AddNetCore (7, 0, "net7.0-macos", KnownPlatform.macOS);
AddNetCore (7, 0, "net7.0-tvos", KnownPlatform.tvOS);
AddNetCore (7, 0, "net7.0-tizen", KnownPlatform.Tizen);

AddNetCore (8, 0, "net8.0");
AddNetCore (8, 0, "net8.0-windows", Platform.Windows);
AddNetCore (8, 0, "net8.0-android", Platform.Android);
AddNetCore (8, 0, "net8.0-ios", Platform.iOS);
AddNetCore (8, 0, "net8.0-maccatalyst", Platform.MacCatalyst);
AddNetCore (8, 0, "net8.0-macos", Platform.macOS);
AddNetCore (8, 0, "net8.0-tvos", Platform.tvOS);
AddNetCore (7, 0, "net8.0-tizen", Platform.Tizen);
AddNetCore (7, 0, "net8.0-browser", Platform.Browser);
AddNetCore (8, 0, "net8.0-windows", KnownPlatform.Windows);
AddNetCore (8, 0, "net8.0-android", KnownPlatform.Android);
AddNetCore (8, 0, "net8.0-ios", KnownPlatform.iOS);
AddNetCore (8, 0, "net8.0-maccatalyst", KnownPlatform.MacCatalyst);
AddNetCore (8, 0, "net8.0-macos", KnownPlatform.macOS);
AddNetCore (8, 0, "net8.0-tvos", KnownPlatform.tvOS);
AddNetCore (7, 0, "net8.0-tizen", KnownPlatform.Tizen);
AddNetCore (7, 0, "net8.0-browser", KnownPlatform.Browser);

AddNetCore (9, 0, "net9.0");
AddNetCore (9, 0, "net9.0-windows", Platform.Windows);
AddNetCore (9, 0, "net9.0-android", Platform.Android);
AddNetCore (9, 0, "net9.0-ios", Platform.iOS);
AddNetCore (9, 0, "net9.0-maccatalyst", Platform.MacCatalyst);
AddNetCore (9, 0, "net9.0-macos", Platform.macOS);
AddNetCore (9, 0, "net9.0-tvos", Platform.tvOS);
AddNetCore (7, 0, "net9.0-tizen", Platform.Tizen);
AddNetCore (7, 0, "net9.0-browser", Platform.Browser);
AddNetCore (9, 0, "net9.0-windows", KnownPlatform.Windows);
AddNetCore (9, 0, "net9.0-android", KnownPlatform.Android);
AddNetCore (9, 0, "net9.0-ios", KnownPlatform.iOS);
AddNetCore (9, 0, "net9.0-maccatalyst", KnownPlatform.MacCatalyst);
AddNetCore (9, 0, "net9.0-macos", KnownPlatform.macOS);
AddNetCore (9, 0, "net9.0-tvos", KnownPlatform.tvOS);
AddNetCore (7, 0, "net9.0-tizen", KnownPlatform.Tizen);
AddNetCore (7, 0, "net9.0-browser", KnownPlatform.Browser);

AddLegacy (null, FxID.MonoAndroid, 1, 0);
AddLegacy (null, FxID.MonoAndroid, 2, 3);
Expand Down Expand Up @@ -292,29 +282,6 @@ public FrameworkInfoProvider ()
}
}

static readonly Dictionary<(int, string), Version> defaultPlatformVersions = new () {
{ (6, Platform.Android), new Version (31, 0) },
{ (7, Platform.Android), new Version (33, 0) },
{ (8, Platform.Android), new Version (34, 0) },
{ (6, Platform.iOS), new Version (15, 0) },
{ (7, Platform.iOS), new Version (16, 1) },
{ (8, Platform.iOS), new Version (17, 2) },
{ (5, Platform.MacCatalyst), new Version (15, 0) },
{ (7, Platform.MacCatalyst), new Version (16, 1) },
{ (8, Platform.MacCatalyst), new Version (17, 2) },
{ (6, Platform.macOS), new Version (12, 0) },
{ (7, Platform.macOS), new Version (13, 0) },
{ (8, Platform.macOS), new Version (14, 2) },
{ (6, Platform.tvOS), new Version (15, 1) },
{ (7, Platform.tvOS), new Version (16, 1) },
{ (8, Platform.tvOS), new Version (17, 1) },
{ (7, Platform.Tizen), new Version (7, 0) },
{ (8, Platform.Tizen), new Version (8, 0) },
{ (6, Platform.Windows), new Version (7, 0) },
{ (7, Platform.Windows), new Version (7, 0) },
{ (8, Platform.Windows), new Version (7, 0) },
};

public FrameworkNameValidationResult ValidateFrameworkShortName (string shortName, out string? frameworkComponent, out Version? versionComponent, out string? platformComponent, out string? profileComponent, out Version? platformVersionComponent)
{
frameworkComponent = platformComponent = profileComponent = null;
Expand Down Expand Up @@ -634,27 +601,18 @@ public static string GetDisplayTitle (NuGetFramework reference)
return sb.ToString ();
}

static string FormatPlatformNameForTitle (NuGetFramework reference) => reference.Platform.ToLowerInvariant () switch {
"windows" => Platform.Windows,
"android" => Platform.Android,
"ios" => Platform.iOS,
"macos" => Platform.macOS,
"tvos" => Platform.tvOS,
"maccatalyst" => Platform.MacCatalyst,
"tizen" => Platform.Tizen,
"browser" => Platform.Browser,
_ => reference.Platform
};
static string FormatPlatformNameForTitle (NuGetFramework reference) => KnownPlatform.ToCanonicalCase (reference.Platform);

static bool TryGetPlatformVersionForDisplay (NuGetFramework fx, [NotNullWhen (true)] out string? displayVersion)
{
var version = fx.PlatformVersion;
if (version?.Major > 0
|| (fx.Framework == FrameworkIdentifiers.NetCoreApp && fx.Version.Major > 5 && fx.Platform is not null && defaultPlatformVersions.TryGetValue ((fx.Version.Major, fx.Platform), out version)))
{
displayVersion = FormatDisplayVersion (version);
return true;
if (fx.IsNet5Era && !string.IsNullOrEmpty (fx.Platform)) {
var platformVersion = fx.PlatformVersion;
if (platformVersion?.Major > 0 || KnownPlatform.TryGetDefaultPlatformVersion (fx.Version.Major, fx.Platform, out platformVersion)) {
displayVersion = FormatDisplayVersion (platformVersion);
return true;
}
}

displayVersion = null;
return false;
}
Expand Down

0 comments on commit e1775b9

Please sign in to comment.