using System.Runtime.InteropServices;
namespace Xdg.Directories;
static internal class Helpers
{
///
/// Return a specified value depending on the running operating system
///
/// Environment variable to test initially
/// Windows directory
/// macOS Directory
/// Linux/BSD Directory
/// Env if set, otherwise variable depending on operating system
/// If running on another operating system
internal static string? OS(string Env, string? Windows, string? MacOS, string? Unix)
{
string? env = GetEnvironmentVariable(Env);
if (env is not null)
return env;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return Windows;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return MacOS;
#if NETSTANDARD
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
#elif NET
else if (OperatingSystem.IsLinux() || OperatingSystem.IsFreeBSD())
#endif
return Unix;
else
throw new NotSupportedException("Platforms not supported by .NET Standard are not supported.");
}
///
/// Return a specified value depending on the running operating system
///
/// Environment variable to test initially
/// Windows directories
/// macOS Directories
/// Linux/BSD Directories
/// Env if set, otherwise variable depending on operating system
/// If running on another operating system
internal static IList? OS(string Env, IList? Windows, IList? MacOS, IList? Unix)
{
string? env = GetEnvironmentVariable(Env);
if (env is not null)
return env.Split(':')!;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return Windows;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return MacOS;
#if NETSTANDARD
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
#elif NET
else if (OperatingSystem.IsLinux() || OperatingSystem.IsFreeBSD())
#endif
return Unix;
else
throw new NotSupportedException("Platforms not supported by .NET Standard are not supported.");
}
///
/// Returns B if and only if A is empty.
///
/// Value to test
/// Alternative if A is null or empty
/// B if A is null or empty, A if not
internal static string AorB(string A, string B)
=> A is { Length: 0 } ? B : A;
}