Skip to content

Commit

Permalink
Adding target platform intrinsic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sfoslund committed Jun 15, 2020
1 parent ee1c9fd commit 6d983c0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion eng/Packages.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<NuGetPackageVersion>5.4.0-rtm.6292</NuGetPackageVersion>
<NuGetPackageVersion>5.7.0-preview.3.6645</NuGetPackageVersion>
<NuGetBuildTasksVersion Condition="'$(NuGetBuildTasksVersion)' == ''">$(NuGetPackageVersion)</NuGetBuildTasksVersion>
<NuGetCommandsVersion Condition="'$(NuGetCommandsVersion)' == ''">$(NuGetPackageVersion)</NuGetCommandsVersion>
<NuGetProtocolVersion Condition="'$(NuGetProtocolVersion)' == ''">$(NuGetPackageVersion)</NuGetProtocolVersion>
Expand Down
20 changes: 20 additions & 0 deletions src/Build.UnitTests/Evaluation/Expander_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2865,6 +2865,7 @@ public void PropertyFunctionVersionComparisons(string a, string b, int expectedS
[InlineData("net45", ".NETFramework", "4.5")]
[InlineData("netcoreapp3.1", ".NETCoreApp", "3.1")]
[InlineData("netstandard2.1", ".NETStandard", "2.1")]
[InlineData("net5.0-ios12.0", ".NETCoreApp", "5.0")]
[InlineData("foo", "Unsupported", "0.0")]
public void PropertyFunctionTargetFrameworkParsing(string tfm, string expectedIdentifier, string expectedVersion)
{
Expand All @@ -2875,8 +2876,27 @@ public void PropertyFunctionTargetFrameworkParsing(string tfm, string expectedId
AssertSuccess(expander, expectedVersion, $"$([MSBuild]::GetTargetFrameworkVersion('{tfm}'))");
}

[Theory]
[InlineData("net5.0-ios12.0", "ios", "12.0")]
[InlineData("net5.1-android1.1", "android", "1.1")]
[InlineData("net6.0-windows99.99", "windows", "99.99")]
[InlineData("net5.0-ios", "ios", "0.0")]
[InlineData("foo", "", "0.0")]
public void PropertyFunctionTargetPlatformParsing(string tfm, string expectedIdentifier, string expectedVersion)
{
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);

AssertSuccess(expander, expectedIdentifier, $"$([MSBuild]::GetTargetPlatformIdentifier('{tfm}'))");
AssertSuccess(expander, expectedVersion, $"$([MSBuild]::GetTargetPlatformVersion('{tfm}'))");
}

[Theory]
[InlineData("net5.0", "net5.0", true)]
[InlineData("net5.0-windows10.0", "net5.0-windows10.0", true)]
[InlineData("net5.0-ios", "net5.0-andriod", false)]
[InlineData("net5.0-ios12.0", "net5.0-ios11.0", true)]
[InlineData("net5.0-ios11.0", "net5.0-ios12.0", false)]
[InlineData("net45", "net46", false)]
[InlineData("net46", "net45", true)]
[InlineData("netcoreapp3.1", "netcoreapp1.0", true)]
Expand Down
16 changes: 16 additions & 0 deletions src/Build/Evaluation/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3938,6 +3938,22 @@ private bool TryExecuteWellKnownFunction(out object returnVal, object objectInst
return true;
}
}
else if (string.Equals(_methodMethodName, nameof(IntrinsicFunctions.GetTargetPlatformIdentifier), StringComparison.OrdinalIgnoreCase))
{
if (TryGetArg(args, out string arg0))
{
returnVal = IntrinsicFunctions.GetTargetPlatformIdentifier(arg0);
return true;
}
}
else if (string.Equals(_methodMethodName, nameof(IntrinsicFunctions.GetTargetPlatformVersion), StringComparison.OrdinalIgnoreCase))
{
if (TryGetArg(args, out string arg0))
{
returnVal = IntrinsicFunctions.GetTargetPlatformVersion(arg0);
return true;
}
}
}
else if (_receiverType == typeof(Path))
{
Expand Down
10 changes: 10 additions & 0 deletions src/Build/Evaluation/IntrinsicFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,16 @@ internal static bool IsTargetFrameworkCompatible(string target, string candidate
return NuGetFramework.Value.IsCompatible(target, candidate);
}

internal static string GetTargetPlatformIdentifier(string tfm)
{
return NuGetFramework.Value.GetTargetPlatformIdentifier(tfm);
}

internal static string GetTargetPlatformVersion(string tfm)
{
return NuGetFramework.Value.GetTargetPlatformVersion(tfm);
}

public static string GetCurrentToolsDirectory()
{
return BuildEnvironmentHelper.Instance.CurrentMSBuildToolsDirectory;
Expand Down
14 changes: 14 additions & 0 deletions src/Build/Utilities/NuGetFrameworkWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal class NuGetFrameworkWrapper
private static object DefaultCompatibilityProvider;
private static PropertyInfo FrameworkProperty;
private static PropertyInfo VersionProperty;
private static PropertyInfo PlatformProperty;
private static PropertyInfo PlatformVersionProperty;

public NuGetFrameworkWrapper()
{
Expand All @@ -39,6 +41,8 @@ public NuGetFrameworkWrapper()
DefaultCompatibilityProvider = NuGetFrameworkDefaultCompatibilityProvider.GetMethod("get_Instance").Invoke(null, new object[] { });
FrameworkProperty = NuGetFramework.GetProperty("Framework");
VersionProperty = NuGetFramework.GetProperty("Version");
PlatformProperty = NuGetFramework.GetProperty("Platform");
PlatformVersionProperty = NuGetFramework.GetProperty("PlatformVersion");
}
catch
{
Expand All @@ -61,6 +65,16 @@ public string GetTargetFrameworkVersion(string tfm)
return (VersionProperty.GetValue(Parse(tfm)) as Version).ToString(2);
}

public string GetTargetPlatformIdentifier(string tfm)
{
return PlatformProperty.GetValue(Parse(tfm)) as string;
}

public string GetTargetPlatformVersion(string tfm)
{
return (PlatformVersionProperty.GetValue(Parse(tfm)) as Version).ToString(2);
}

public bool IsCompatible(string target, string candidate)
{
return Convert.ToBoolean(IsCompatibleMethod.Invoke(DefaultCompatibilityProvider, new object[] { Parse(target), Parse(candidate) }));
Expand Down

0 comments on commit 6d983c0

Please sign in to comment.