2017-03-03 05:04:03 +00:00
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
2016-02-18 19:39:36 +00:00
|
|
|
using System;
|
2016-06-24 18:06:13 +00:00
|
|
|
using System.Linq;
|
2016-02-18 19:39:36 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2016-08-08 22:35:25 +00:00
|
|
|
using Microsoft.DotNet.PlatformAbstractions;
|
2016-02-18 19:39:36 +00:00
|
|
|
|
2016-02-20 01:00:41 +00:00
|
|
|
namespace Microsoft.DotNet.Cli.Build.Framework
|
2016-02-18 19:39:36 +00:00
|
|
|
{
|
2016-02-20 01:00:41 +00:00
|
|
|
public static class CurrentPlatform
|
2016-02-18 19:39:36 +00:00
|
|
|
{
|
2016-02-20 01:00:41 +00:00
|
|
|
public static BuildPlatform Current
|
2016-02-18 19:39:36 +00:00
|
|
|
{
|
2016-02-20 01:00:41 +00:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return DetermineCurrentPlatform();
|
|
|
|
}
|
2016-02-18 19:39:36 +00:00
|
|
|
}
|
|
|
|
|
2016-02-20 01:00:41 +00:00
|
|
|
public static bool IsWindows
|
2016-02-18 19:39:36 +00:00
|
|
|
{
|
2016-02-20 01:00:41 +00:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
|
|
|
}
|
2016-02-18 19:39:36 +00:00
|
|
|
}
|
|
|
|
|
2016-02-20 01:00:41 +00:00
|
|
|
public static bool IsOSX
|
2016-02-18 19:39:36 +00:00
|
|
|
{
|
2016-02-20 01:00:41 +00:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
|
|
|
}
|
2016-02-18 19:39:36 +00:00
|
|
|
}
|
|
|
|
|
2016-02-20 01:00:41 +00:00
|
|
|
public static bool IsUbuntu
|
2016-02-18 19:39:36 +00:00
|
|
|
{
|
2016-02-20 01:00:41 +00:00
|
|
|
get
|
|
|
|
{
|
2016-04-28 23:30:32 +00:00
|
|
|
var osname = RuntimeEnvironment.OperatingSystem;
|
2016-02-20 01:00:41 +00:00
|
|
|
return string.Equals(osname, "ubuntu", StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
2016-02-18 19:39:36 +00:00
|
|
|
}
|
|
|
|
|
2016-02-20 01:00:41 +00:00
|
|
|
public static bool IsCentOS
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2016-04-28 23:30:32 +00:00
|
|
|
var osname = RuntimeEnvironment.OperatingSystem;
|
2016-02-20 01:00:41 +00:00
|
|
|
return string.Equals(osname, "centos", StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 21:20:45 +00:00
|
|
|
public static bool IsRHEL
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2016-04-28 23:30:32 +00:00
|
|
|
var osname = RuntimeEnvironment.OperatingSystem;
|
2016-03-02 21:20:45 +00:00
|
|
|
return string.Equals(osname, "rhel", StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 06:16:18 +00:00
|
|
|
public static bool IsFedora
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var osname = RuntimeEnvironment.OperatingSystem;
|
|
|
|
return string.Equals(osname, "fedora", StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 20:54:35 +00:00
|
|
|
public static bool IsOpenSuse
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var osname = RuntimeEnvironment.OperatingSystem;
|
|
|
|
return string.Equals(osname, "opensuse", StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 20:20:50 +00:00
|
|
|
public static bool IsUnix
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return IsLinux || IsOSX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-03 18:38:58 +00:00
|
|
|
public static bool IsDebian
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2016-04-28 23:30:32 +00:00
|
|
|
var osname = RuntimeEnvironment.OperatingSystem;
|
2016-03-03 18:38:58 +00:00
|
|
|
return string.Equals(osname, "debian", StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 20:20:50 +00:00
|
|
|
public static bool IsLinux
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2016-05-31 20:54:35 +00:00
|
|
|
return IsUbuntu || IsCentOS || IsRHEL || IsDebian || IsFedora || IsOpenSuse;
|
2016-03-07 20:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 19:48:16 +00:00
|
|
|
public static bool IsPlatform(BuildPlatform platform, string version = null)
|
|
|
|
{
|
2016-05-30 05:38:45 +00:00
|
|
|
return IsPlatform(platform) && (version == null || IsVersion(version));
|
2016-05-26 19:48:16 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 18:06:13 +00:00
|
|
|
public static bool IsAnyPlatform(params BuildPlatform[] platforms)
|
|
|
|
{
|
|
|
|
return platforms.Any(p => IsPlatform(p));
|
|
|
|
}
|
|
|
|
|
2016-03-07 20:20:50 +00:00
|
|
|
public static bool IsPlatform(BuildPlatform platform)
|
|
|
|
{
|
|
|
|
switch (platform)
|
|
|
|
{
|
|
|
|
case BuildPlatform.Windows:
|
|
|
|
return IsWindows;
|
|
|
|
case BuildPlatform.Ubuntu:
|
|
|
|
return IsUbuntu;
|
|
|
|
case BuildPlatform.OSX:
|
|
|
|
return IsOSX;
|
|
|
|
case BuildPlatform.CentOS:
|
|
|
|
return IsCentOS;
|
2016-03-08 23:42:15 +00:00
|
|
|
case BuildPlatform.RHEL:
|
|
|
|
return IsRHEL;
|
2016-03-09 23:35:21 +00:00
|
|
|
case BuildPlatform.Debian:
|
|
|
|
return IsDebian;
|
2016-05-27 06:16:18 +00:00
|
|
|
case BuildPlatform.Fedora:
|
|
|
|
return IsFedora;
|
2016-05-31 20:54:35 +00:00
|
|
|
case BuildPlatform.OpenSuse:
|
|
|
|
return IsOpenSuse;
|
2016-03-07 20:20:50 +00:00
|
|
|
case BuildPlatform.Unix:
|
|
|
|
return IsUnix;
|
|
|
|
case BuildPlatform.Linux:
|
|
|
|
return IsLinux;
|
|
|
|
default:
|
|
|
|
throw new Exception("Unrecognized Platform.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 19:48:16 +00:00
|
|
|
public static bool IsVersion(string version)
|
|
|
|
{
|
|
|
|
return RuntimeEnvironment.OperatingSystemVersion.Equals(version, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
|
2016-02-20 01:00:41 +00:00
|
|
|
private static BuildPlatform DetermineCurrentPlatform()
|
2016-02-18 19:39:36 +00:00
|
|
|
{
|
2016-02-20 01:00:41 +00:00
|
|
|
if (IsWindows)
|
|
|
|
{
|
|
|
|
return BuildPlatform.Windows;
|
|
|
|
}
|
|
|
|
else if (IsOSX)
|
|
|
|
{
|
|
|
|
return BuildPlatform.OSX;
|
|
|
|
}
|
|
|
|
else if (IsUbuntu)
|
|
|
|
{
|
|
|
|
return BuildPlatform.Ubuntu;
|
|
|
|
}
|
|
|
|
else if (IsCentOS)
|
|
|
|
{
|
|
|
|
return BuildPlatform.CentOS;
|
|
|
|
}
|
2016-03-02 21:20:45 +00:00
|
|
|
else if (IsRHEL)
|
|
|
|
{
|
|
|
|
return BuildPlatform.RHEL;
|
|
|
|
}
|
2016-03-03 18:38:58 +00:00
|
|
|
else if (IsDebian)
|
|
|
|
{
|
|
|
|
return BuildPlatform.Debian;
|
|
|
|
}
|
2016-05-27 06:16:18 +00:00
|
|
|
else if (IsFedora)
|
|
|
|
{
|
|
|
|
return BuildPlatform.Fedora;
|
|
|
|
}
|
2016-05-31 20:54:35 +00:00
|
|
|
else if (IsOpenSuse)
|
|
|
|
{
|
|
|
|
return BuildPlatform.OpenSuse;
|
|
|
|
}
|
2016-02-20 01:00:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return default(BuildPlatform);
|
|
|
|
}
|
2016-02-18 19:39:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-02 21:20:45 +00:00
|
|
|
}
|