2016-02-18 11:39:36 -08:00
using System ;
2016-06-24 13:06:13 -05:00
using System.Linq ;
2016-02-18 11:39:36 -08:00
using System.Runtime.InteropServices ;
2016-08-08 15:35:25 -07:00
using Microsoft.DotNet.PlatformAbstractions ;
2016-02-18 11:39:36 -08:00
2016-02-19 17:00:41 -08:00
namespace Microsoft.DotNet.Cli.Build.Framework
2016-02-18 11:39:36 -08:00
{
2016-02-19 17:00:41 -08:00
public static class CurrentPlatform
2016-02-18 11:39:36 -08:00
{
2016-02-19 17:00:41 -08:00
public static BuildPlatform Current
2016-02-18 11:39:36 -08:00
{
2016-02-19 17:00:41 -08:00
get
{
return DetermineCurrentPlatform ( ) ;
}
2016-02-18 11:39:36 -08:00
}
2016-02-19 17:00:41 -08:00
public static bool IsWindows
2016-02-18 11:39:36 -08:00
{
2016-02-19 17:00:41 -08:00
get
{
return RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ;
}
2016-02-18 11:39:36 -08:00
}
2016-02-19 17:00:41 -08:00
public static bool IsOSX
2016-02-18 11:39:36 -08:00
{
2016-02-19 17:00:41 -08:00
get
{
return RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) ;
}
2016-02-18 11:39:36 -08:00
}
2016-02-19 17:00:41 -08:00
public static bool IsUbuntu
2016-02-18 11:39:36 -08:00
{
2016-02-19 17:00:41 -08:00
get
{
2016-04-28 16:30:32 -07:00
var osname = RuntimeEnvironment . OperatingSystem ;
2016-02-19 17:00:41 -08:00
return string . Equals ( osname , "ubuntu" , StringComparison . OrdinalIgnoreCase ) ;
}
2016-02-18 11:39:36 -08:00
}
2016-02-19 17:00:41 -08:00
public static bool IsCentOS
{
get
{
2016-04-28 16:30:32 -07:00
var osname = RuntimeEnvironment . OperatingSystem ;
2016-02-19 17:00:41 -08:00
return string . Equals ( osname , "centos" , StringComparison . OrdinalIgnoreCase ) ;
}
}
2016-03-02 16:20:45 -05:00
public static bool IsRHEL
{
get
{
2016-04-28 16:30:32 -07:00
var osname = RuntimeEnvironment . OperatingSystem ;
2016-03-02 16:20:45 -05:00
return string . Equals ( osname , "rhel" , StringComparison . OrdinalIgnoreCase ) ;
}
}
2016-05-26 23:16:18 -07:00
public static bool IsFedora
{
get
{
var osname = RuntimeEnvironment . OperatingSystem ;
return string . Equals ( osname , "fedora" , StringComparison . OrdinalIgnoreCase ) ;
}
}
2016-05-31 13:54:35 -07:00
public static bool IsOpenSuse
{
get
{
var osname = RuntimeEnvironment . OperatingSystem ;
return string . Equals ( osname , "opensuse" , StringComparison . OrdinalIgnoreCase ) ;
}
}
2016-03-07 12:20:50 -08: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 16:30:32 -07:00
var osname = RuntimeEnvironment . OperatingSystem ;
2016-03-03 18:38:58 +00:00
return string . Equals ( osname , "debian" , StringComparison . OrdinalIgnoreCase ) ;
}
}
2016-03-07 12:20:50 -08:00
public static bool IsLinux
{
get
{
2016-05-31 13:54:35 -07:00
return IsUbuntu | | IsCentOS | | IsRHEL | | IsDebian | | IsFedora | | IsOpenSuse ;
2016-03-07 12:20:50 -08:00
}
}
2016-05-26 12:48:16 -07:00
public static bool IsPlatform ( BuildPlatform platform , string version = null )
{
2016-05-29 22:38:45 -07:00
return IsPlatform ( platform ) & & ( version = = null | | IsVersion ( version ) ) ;
2016-05-26 12:48:16 -07:00
}
2016-06-24 13:06:13 -05:00
public static bool IsAnyPlatform ( params BuildPlatform [ ] platforms )
{
return platforms . Any ( p = > IsPlatform ( p ) ) ;
}
2016-03-07 12:20:50 -08: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-26 23:16:18 -07:00
case BuildPlatform . Fedora :
return IsFedora ;
2016-05-31 13:54:35 -07:00
case BuildPlatform . OpenSuse :
return IsOpenSuse ;
2016-03-07 12:20:50 -08:00
case BuildPlatform . Unix :
return IsUnix ;
case BuildPlatform . Linux :
return IsLinux ;
default :
throw new Exception ( "Unrecognized Platform." ) ;
}
}
2016-05-26 12:48:16 -07:00
public static bool IsVersion ( string version )
{
return RuntimeEnvironment . OperatingSystemVersion . Equals ( version , StringComparison . OrdinalIgnoreCase ) ;
}
2016-02-19 17:00:41 -08:00
private static BuildPlatform DetermineCurrentPlatform ( )
2016-02-18 11:39:36 -08:00
{
2016-02-19 17:00:41 -08: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 16:20:45 -05:00
else if ( IsRHEL )
{
return BuildPlatform . RHEL ;
}
2016-03-03 18:38:58 +00:00
else if ( IsDebian )
{
return BuildPlatform . Debian ;
}
2016-05-26 23:16:18 -07:00
else if ( IsFedora )
{
return BuildPlatform . Fedora ;
}
2016-05-31 13:54:35 -07:00
else if ( IsOpenSuse )
{
return BuildPlatform . OpenSuse ;
}
2016-02-19 17:00:41 -08:00
else
{
return default ( BuildPlatform ) ;
}
2016-02-18 11:39:36 -08:00
}
}
2016-03-02 16:20:45 -05:00
}