From aa280a7b98384b58286f7e505c2714208a80c2dd Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Mon, 11 Apr 2016 18:55:24 -0400 Subject: [PATCH] Add intrinsic property function `IsOSPlatform(string os)` Usage: $([MSBuild]::IsOSPlatform(Windows)) Based on @dsplaisted's suggestion in issue #539 . --- .../Evaluation/IntrinsicFunctions.cs | 11 +++++++ .../UnitTests/Evaluation/Expander_Tests.cs | 30 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/XMakeBuildEngine/Evaluation/IntrinsicFunctions.cs b/src/XMakeBuildEngine/Evaluation/IntrinsicFunctions.cs index a675b1e7ce7..86bdc501dce 100644 --- a/src/XMakeBuildEngine/Evaluation/IntrinsicFunctions.cs +++ b/src/XMakeBuildEngine/Evaluation/IntrinsicFunctions.cs @@ -364,6 +364,17 @@ internal static bool DoesTaskHostExist(string runtime, string architecture) return false; } + internal static bool IsOSPlatform(string os) + { + switch (os.ToLower()) + { + case "windows": return NativeMethodsShared.IsWindows; + case "osx": return NativeMethodsShared.IsOSX; + case "unix": return NativeMethodsShared.IsUnix; + default: return false; + } + } + #region Debug only intrinsics /// diff --git a/src/XMakeBuildEngine/UnitTests/Evaluation/Expander_Tests.cs b/src/XMakeBuildEngine/UnitTests/Evaluation/Expander_Tests.cs index 4892248ccf9..04b51ff83cb 100644 --- a/src/XMakeBuildEngine/UnitTests/Evaluation/Expander_Tests.cs +++ b/src/XMakeBuildEngine/UnitTests/Evaluation/Expander_Tests.cs @@ -2826,6 +2826,36 @@ public void PropertyFunctionStaticMethodIntrinsicMaths() Assert.Equal((~-43).ToString(), result); } + private void CheckOSPlatform(Func isOSPlatform, bool isWindows, bool isOSX, bool isUnix) + { + Assert.Equal(isWindows, isOSPlatform("Windows")); + Assert.Equal(isOSX, isOSPlatform("OSX")); + Assert.Equal(isUnix, isOSPlatform("Unix")); + } + + [Fact] + public void PropertyFunctionStaticMethodIntrinsicIsOSPlatform() + { + PropertyDictionary pg = new PropertyDictionary(); + Expander expander = new Expander(pg); + + Func isOSPlatform = os => bool.Parse(expander.ExpandIntoStringLeaveEscaped(@"$([MSBuild]::IsOSPlatform(" + os + "))", + ExpanderOptions.ExpandProperties, MockElementLocation.Instance)); + + if (NativeMethodsShared.IsWindows) + { + CheckOSPlatform(isOSPlatform, isWindows:true, isOSX:false, isUnix:false); + } + else if (NativeMethodsShared.IsOSX) + { + CheckOSPlatform(isOSPlatform, isWindows:false, isOSX:true, isUnix:true); + } + else + { + CheckOSPlatform(isOSPlatform, isWindows:false, isOSX:false, isUnix:true); + } + } + /// /// Expand a property reference that has whitespace around the property name (should result in empty) ///