Skip to content

Commit

Permalink
Add intrinsic property function IsOSPlatform(string os)
Browse files Browse the repository at this point in the history
Usage:
    $([MSBuild]::IsOSPlatform(Windows))

Based on @dsplaisted's suggestion in issue #539 .
  • Loading branch information
Ankit Jain committed Apr 11, 2016
1 parent 5e01f07 commit aa280a7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/XMakeBuildEngine/Evaluation/IntrinsicFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

/// <summary>
Expand Down
30 changes: 30 additions & 0 deletions src/XMakeBuildEngine/UnitTests/Evaluation/Expander_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2826,6 +2826,36 @@ public void PropertyFunctionStaticMethodIntrinsicMaths()
Assert.Equal((~-43).ToString(), result);
}

private void CheckOSPlatform(Func<string, bool> 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<ProjectPropertyInstance> pg = new PropertyDictionary<ProjectPropertyInstance>();
Expander<ProjectPropertyInstance, ProjectItemInstance> expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg);

Func<string, bool> 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);
}
}

/// <summary>
/// Expand a property reference that has whitespace around the property name (should result in empty)
/// </summary>
Expand Down

0 comments on commit aa280a7

Please sign in to comment.