Skip to content

Commit

Permalink
Merge pull request #4312 from Thefrank/freebsd
Browse files Browse the repository at this point in the history
Add FreeBSD Platform Detection
  • Loading branch information
devlead authored Oct 26, 2024
2 parents 948bdc7 + 9633034 commit 176bb7f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/Cake.Common.Tests/Unit/EnvironmentAliasesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public void Should_Throw_If_Context_Is_Null()
[Theory]
[InlineData(PlatformFamily.Linux, false)]
[InlineData(PlatformFamily.OSX, false)]
[InlineData(PlatformFamily.FreeBSD, false)]
[InlineData(PlatformFamily.Windows, true)]
public void Should_Return_Correct_Value(PlatformFamily family, bool expected)
{
Expand Down Expand Up @@ -212,6 +213,7 @@ public void Should_Throw_If_Context_Is_Null()
[Theory]
[InlineData(PlatformFamily.Linux, true)]
[InlineData(PlatformFamily.OSX, true)]
[InlineData(PlatformFamily.FreeBSD, true)]
[InlineData(PlatformFamily.Windows, false)]
public void Should_Return_Correct_Value(PlatformFamily family, bool expected)
{
Expand Down Expand Up @@ -242,6 +244,7 @@ public void Should_Throw_If_Context_Is_Null()
[Theory]
[InlineData(PlatformFamily.Linux, true)]
[InlineData(PlatformFamily.OSX, false)]
[InlineData(PlatformFamily.FreeBSD, false)]
[InlineData(PlatformFamily.Windows, false)]
public void Should_Return_Correct_Value(PlatformFamily family, bool expected)
{
Expand Down Expand Up @@ -272,6 +275,7 @@ public void Should_Throw_If_Context_Is_Null()
[Theory]
[InlineData(PlatformFamily.Linux, false)]
[InlineData(PlatformFamily.OSX, true)]
[InlineData(PlatformFamily.FreeBSD, false)]
[InlineData(PlatformFamily.Windows, false)]
public void Should_Return_Correct_Value(PlatformFamily family, bool expected)
{
Expand All @@ -286,5 +290,36 @@ public void Should_Return_Correct_Value(PlatformFamily family, bool expected)
Assert.Equal(expected, result);
}
}

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

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

[Theory]
[InlineData(PlatformFamily.Linux, false)]
[InlineData(PlatformFamily.OSX, false)]
[InlineData(PlatformFamily.FreeBSD, 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.IsRunningOnFreeBSD(context);

// Then
Assert.Equal(expected, result);
}
}
}
}
26 changes: 26 additions & 0 deletions src/Cake.Common/EnviromentAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,32 @@ public static bool IsRunningOnMacOs(this ICakeContext context)
return context.Environment.Platform.IsOSX();
}

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

/// <summary>
/// Determines whether the build script running on a Linux based system.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/Cake.Common/Tools/MSBuild/MSBuildResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ public static FilePath GetMSBuildPath(IFileSystem fileSystem, ICakeEnvironment e

throw new CakeException("Could not resolve MSBuild.");
}
else if (environment.Platform.Family == PlatformFamily.FreeBSD)
{
var freebsdMSBuildPath = new FilePath("/usr/local/bin/msbuild");

if (fileSystem.Exist(freebsdMSBuildPath))
{
return freebsdMSBuildPath;
}

throw new CakeException("Could not resolve MSBuild.");
}

var binPath = settings.ToolVersion == MSBuildToolVersion.Default
? GetHighestAvailableMSBuildVersion(fileSystem, environment, buildPlatform, settings.AllowPreviewVersion)
Expand Down
14 changes: 14 additions & 0 deletions src/Cake.Core/Extensions/CakePlatformExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,19 @@ public static bool IsLinux(this ICakePlatform platform)
}
return EnvironmentHelper.IsLinux(platform.Family);
}

/// <summary>
/// Determines whether the specified platform is a FreeBSD platform.
/// </summary>
/// <param name="platform">The platform.</param>
/// <returns><c>true</c> if the platform is a FreeBSD platform; otherwise <c>false</c>.</returns>
public static bool IsFreeBSD(this ICakePlatform platform)
{
if (platform is null)
{
throw new ArgumentNullException(nameof(platform));
}
return EnvironmentHelper.IsFreeBSD(platform.Family);
}
}
}
7 changes: 6 additions & 1 deletion src/Cake.Core/PlatformFamily.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public enum PlatformFamily
/// Represents the OSX platform family.
/// </summary>
// ReSharper disable once InconsistentNaming
OSX = 3
OSX = 3,

/// <summary>
/// Represents the FreeBSD platform family.
/// </summary>
FreeBSD = 4,
}
}
18 changes: 17 additions & 1 deletion src/Cake.Core/Polyfill/EnvironmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public static PlatformFamily GetPlatformFamily()
catch (PlatformNotSupportedException)
{
}
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")))
{
return PlatformFamily.FreeBSD;
}
}
catch (PlatformNotSupportedException)
{
}

return PlatformFamily.Unknown;
}
Expand Down Expand Up @@ -81,7 +91,8 @@ public static bool IsUnix()
public static bool IsUnix(PlatformFamily family)
{
return family == PlatformFamily.Linux
|| family == PlatformFamily.OSX;
|| family == PlatformFamily.OSX
|| family == PlatformFamily.FreeBSD;
}

public static bool IsOSX(PlatformFamily family)
Expand All @@ -94,6 +105,11 @@ public static bool IsLinux(PlatformFamily family)
return family == PlatformFamily.Linux;
}

public static bool IsFreeBSD(PlatformFamily family)
{
return family == PlatformFamily.FreeBSD;
}

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

0 comments on commit 176bb7f

Please sign in to comment.