Skip to content

Commit

Permalink
Add IsRunningOn for macOS and Linux
Browse files Browse the repository at this point in the history
This is very helpful for when things run on just macOS or just Linux. The Unix variant is nice for the basic Windows/not-Windows, but in some cases, the build script needs to know if Xcode should be used or some other mac-specific tool.
* fixes #741
  • Loading branch information
mattleibow authored and devlead committed Sep 7, 2020
1 parent 788e864 commit 994404e
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
62 changes: 61 additions & 1 deletion src/Cake.Common.Tests/Unit/EnvironmentAliasesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,65 @@ public void Should_Return_Correct_Value(PlatformFamily family, bool expected)
Assert.Equal(expected, result);
}
}

public sealed class TheIsRunningOnLinuxMethod
{
[Fact]
public void Should_Throw_If_Context_Is_Null()
{
// Given, When
var result = Record.Exception(() => EnvironmentAliases.IsRunningOnLinux(null));

// Then
AssertEx.IsArgumentNullException(result, "context");
}

[Theory]
[InlineData(PlatformFamily.Linux, true)]
[InlineData(PlatformFamily.OSX, false)]
[InlineData(PlatformFamily.Windows, false)]
public void Should_Return_Correct_Value(PlatformFamily family, bool expected)
{
// Given
var context = Substitute.For<ICakeContext>();
context.Environment.Returns(new FakeEnvironment(family));

// When
var result = EnvironmentAliases.IsRunningOnLinux(context);

// Then
Assert.Equal(expected, result);
}
}

public sealed class TheIsRunningOnMacMethod
{
[Fact]
public void Should_Throw_If_Context_Is_Null()
{
// Given, When
var result = Record.Exception(() => EnvironmentAliases.IsRunningOnMacOs(null));

// Then
AssertEx.IsArgumentNullException(result, "context");
}

[Theory]
[InlineData(PlatformFamily.Linux, false)]
[InlineData(PlatformFamily.OSX, true)]
[InlineData(PlatformFamily.Windows, false)]
public void Should_Return_Correct_Value(PlatformFamily family, bool expected)
{
// Given
var context = Substitute.For<ICakeContext>();
context.Environment.Returns(new FakeEnvironment(family));

// When
var result = EnvironmentAliases.IsRunningOnMacOs(context);

// Then
Assert.Equal(expected, result);
}
}
}
}
}
54 changes: 53 additions & 1 deletion src/Cake.Common/EnviromentAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,62 @@ public static bool IsRunningOnUnix(this ICakeContext context)
return context.Environment.Platform.IsUnix();
}

/// <summary>
/// Determines whether the build script running on a macOS based system.
/// </summary>
/// <example>
/// <code>
/// if (IsRunningOnMacOs())
/// {
/// Information("macOS!");
/// }
/// </code>
/// </example>
/// <param name="context">The context.</param>
/// <returns>
/// <c>true</c> if the build script running on a macOS based system; otherwise <c>false</c>.
/// </returns>
[CakeMethodAlias]
[CakeAliasCategory("Platform")]
public static bool IsRunningOnMacOs(this ICakeContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return context.Environment.Platform.IsOSX();
}

/// <summary>
/// Determines whether the build script running on a Linux based system.
/// </summary>
/// <example>
/// <code>
/// if (IsRunningOnLinux())
/// {
/// Information("Linux!");
/// }
/// </code>
/// </example>
/// <param name="context">The context.</param>
/// <returns>
/// <c>true</c> if the build script running on a Linux based system; otherwise <c>false</c>.
/// </returns>
[CakeMethodAlias]
[CakeAliasCategory("Platform")]
public static bool IsRunningOnLinux(this ICakeContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return context.Environment.Platform.IsLinux();
}

private static T Convert<T>(string value)
{
var converter = TypeDescriptor.GetConverter(typeof(T));
return (T)converter.ConvertFromInvariantString(value);
}
}
}
}
28 changes: 28 additions & 0 deletions src/Cake.Core/Extensions/CakePlatformExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,33 @@ public static bool IsUnix(this ICakePlatform platform)
}
return EnvironmentHelper.IsUnix(platform.Family);
}

/// <summary>
/// Determines whether the specified platform is a macOS platform.
/// </summary>
/// <param name="platform">The platform.</param>
/// <returns><c>true</c> if the platform is a macOS platform; otherwise <c>false</c>.</returns>
public static bool IsOSX(this ICakePlatform platform)
{
if (platform == null)
{
throw new ArgumentNullException(nameof(platform));
}
return EnvironmentHelper.IsOSX(platform.Family);
}

/// <summary>
/// Determines whether the specified platform is a Linux platform.
/// </summary>
/// <param name="platform">The platform.</param>
/// <returns><c>true</c> if the platform is a Linux platform; otherwise <c>false</c>.</returns>
public static bool IsLinux(this ICakePlatform platform)
{
if (platform == null)
{
throw new ArgumentNullException(nameof(platform));
}
return EnvironmentHelper.IsLinux(platform.Family);
}
}
}
10 changes: 10 additions & 0 deletions src/Cake.Core/Polyfill/EnvironmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ public static bool IsUnix(PlatformFamily family)
|| family == PlatformFamily.OSX;
}

public static bool IsOSX(PlatformFamily family)
{
return family == PlatformFamily.OSX;
}

public static bool IsLinux(PlatformFamily family)
{
return family == PlatformFamily.Linux;
}

public static Runtime GetRuntime()
{
if (IsCoreClr())
Expand Down

0 comments on commit 994404e

Please sign in to comment.