diff --git a/PowerShellGitHubDev.psm1 b/PowerShellGitHubDev.psm1 index a5925a3e3..0f460a37c 100644 --- a/PowerShellGitHubDev.psm1 +++ b/PowerShellGitHubDev.psm1 @@ -1,7 +1,5 @@ # Use the .NET Core APIs to determine the current platform; if a runtime # exception is thrown, we are on FullCLR, not .NET Core. -# -# TODO: import Microsoft.PowerShell.Platform instead try { $Runtime = [System.Runtime.InteropServices.RuntimeInformation] $OSPlatform = [System.Runtime.InteropServices.OSPlatform] @@ -10,11 +8,15 @@ try { $IsLinux = $Runtime::IsOSPlatform($OSPlatform::Linux) $IsOSX = $Runtime::IsOSPlatform($OSPlatform::OSX) $IsWindows = $Runtime::IsOSPlatform($OSPlatform::Windows) -} catch [System.Management.Automation.RuntimeException] { - $IsCore = $false - $IsLinux = $false - $IsOSX = $false - $IsWindows = $true +} catch { + # If these are already set, then they're read-only and we're done + try { + $IsCore = $false + $IsLinux = $false + $IsOSX = $false + $IsWindows = $true + } + catch { } } @@ -122,9 +124,9 @@ function Start-PSBuild { # FullCLR only builds a library, so there is no runtime component if ($FullCLR) { - $script:Output = [IO.Path]::Combine($Top, "bin", $Configuration, $Framework, $Executable) + $script:Output = [IO.Path]::Combine($Top, "bin", $Configuration, $Framework, $Executable) } else { - $script:Output = [IO.Path]::Combine($Top, "bin", $Configuration, $Framework, $Runtime, $Executable) + $script:Output = [IO.Path]::Combine($Top, "bin", $Configuration, $Framework, $Runtime, $Executable) } Write-Verbose "script:Output is $script:Output" diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index 5d9fe6800..2b7211b0e 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -1362,7 +1362,7 @@ namespace Microsoft.PowerShell.Commands continue; } - if (Platform.IsWindows() && !Force) + if (Platform.IsWindows && !Force) { // Check if the process is owned by current user if (!IsProcessOwnedByCurrentUser(process)) @@ -2097,7 +2097,7 @@ namespace Microsoft.PowerShell.Commands } //LoadUserProfile. - if (Platform.IsWindows()) + if (Platform.IsWindows) { startInfo.LoadUserProfile = _loaduserprofile; } @@ -2193,7 +2193,7 @@ namespace Microsoft.PowerShell.Commands #endif //Starts the Process Process process; - if (Platform.IsWindows()) + if (Platform.IsWindows) { process = start(startInfo); } @@ -2238,7 +2238,7 @@ namespace Microsoft.PowerShell.Commands { if (!process.HasExited) { - if (Platform.IsWindows()) + if (Platform.IsWindows) { waithandle = new ManualResetEvent(false); diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/SetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/SetDateCommand.cs index 1cd50f686..cf5868ce2 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/SetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/SetDateCommand.cs @@ -117,7 +117,7 @@ namespace Microsoft.PowerShell.Commands if ( ShouldProcess( dateToUse.ToString() ) ) { - if (Platform.IsWindows()) + if (Platform.IsWindows) { #pragma warning disable 56523 diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Platform/Microsoft.PowerShell.Platform.psd1 b/src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Platform/Microsoft.PowerShell.Platform.psd1 deleted file mode 100644 index 29fc928a0..000000000 Binary files a/src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Platform/Microsoft.PowerShell.Platform.psd1 and /dev/null differ diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Platform/Microsoft.PowerShell.Platform.psm1 b/src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Platform/Microsoft.PowerShell.Platform.psm1 deleted file mode 100644 index ae9616874..000000000 --- a/src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Platform/Microsoft.PowerShell.Platform.psm1 +++ /dev/null @@ -1,18 +0,0 @@ -# Use the .NET Core APIs to determine the current platform; if a runtime -# exception is thrown, we are on FullCLR, not .NET Core. -try { - $Runtime = [System.Runtime.InteropServices.RuntimeInformation] - $OSPlatform = [System.Runtime.InteropServices.OSPlatform] - - $IsCore = $true - $IsLinux = $Runtime::IsOSPlatform($OSPlatform::Linux) - $IsOSX = $Runtime::IsOSPlatform($OSPlatform::OSX) - $IsWindows = $Runtime::IsOSPlatform($OSPlatform::Windows) -} catch [System.Management.Automation.RuntimeException] { - $IsCore = $false - $IsLinux = $false - $IsOSX = $false - $IsWindows = $true -} - -Export-ModuleMember -Variable IsCore, IsLinux, IsOSX, IsWindows diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/PSL_profile.ps1 b/src/Microsoft.PowerShell.CoreConsoleHost/PSL_profile.ps1 index 959f832ba..de4c4f2da 100644 --- a/src/Microsoft.PowerShell.CoreConsoleHost/PSL_profile.ps1 +++ b/src/Microsoft.PowerShell.CoreConsoleHost/PSL_profile.ps1 @@ -1,5 +1,3 @@ function prompt { "PS " + $(get-location) + "> " } - -Import-Module Microsoft.PowerShell.Platform diff --git a/src/System.Management.Automation/CoreCLR/CorePsExtensions.cs b/src/System.Management.Automation/CoreCLR/CorePsExtensions.cs index ec69e4fa4..d66748dfa 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsExtensions.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsExtensions.cs @@ -828,7 +828,7 @@ namespace System.Management.Automation // Porting note: if not otherwise defined, map Windows environment // variables to their corresponding Linux counterparts - if (Platform.IsLinux() && String.IsNullOrEmpty(value)) + if (!Platform.IsWindows && String.IsNullOrEmpty(value)) { switch (variable) { @@ -1023,7 +1023,7 @@ namespace System.Management.Automation { get { - if (Platform.IsWindows()) + if (Platform.IsWindows) { return WinGetUserDomainName(); } @@ -1059,7 +1059,7 @@ namespace System.Management.Automation { get { - if (Platform.IsWindows()) + if (Platform.IsWindows) { return WinGetUserName(); } @@ -1088,7 +1088,7 @@ namespace System.Management.Automation { get { - if (Platform.IsWindows()) + if (Platform.IsWindows) { return WinGetMachineName(); } @@ -1108,7 +1108,7 @@ namespace System.Management.Automation { if (m_os == null) { - if (Platform.IsWindows()) + if (Platform.IsWindows) { m_os = WinOSVersion; } @@ -1179,7 +1179,7 @@ namespace System.Management.Automation /// private static string InternalGetFolderPath(SpecialFolder folder) { - if (!Platform.IsWindows()) + if (!Platform.IsWindows) { return Platform.NonWindowsGetFolderPath(folder); } diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index 51fc33d68..50c651425 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -30,39 +30,54 @@ namespace System.Management.Automation /// internal static class Platform { - // this queries specifically for Linux or OS X because they are very similar - internal static bool IsLinux() + + // Platform variables used to defined corresponding PowerShell built-in variables + public static bool IsLinux { -#if CORECLR - return RuntimeInformation.IsOSPlatform(OSPlatform.Linux) - || RuntimeInformation.IsOSPlatform(OSPlatform.OSX); -#else - // FullCLR doesn't have a Linux version - return false; -#endif + get + { + #if CORECLR + return RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + #else + return false; + #endif + } } - // this is specifically to query for Windows, everything else is considered to be non-Windows - internal static bool IsWindows() + public static bool IsOSX { -#if CORECLR - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); -#else - // FullCLR has only Windows version - return true; -#endif + get + { + #if CORECLR + return RuntimeInformation.IsOSPlatform(OSPlatform.OSX); + #else + return false; + #endif + } } - // true if cross-platform (always for this project) - internal static bool IsX() + public static bool IsWindows { -#if CORECLR - // CoreCLR version is cross-platform - return true; -#else - // FullCLR is not cross-platform - return false; -#endif + get + { + #if CORECLR + return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + #else + return true; + #endif + } + } + + public static bool IsCore + { + get + { + #if CORECLR + return true; + #else + return false; + #endif + } } // format files @@ -89,43 +104,43 @@ namespace System.Management.Automation internal static bool HasCom() { // TODO: catch exception from Type.IsComObject - return IsWindows(); + return IsWindows; } // The Antimalware Scan Interface is not supported on Linux internal static bool HasAmsi() { - return IsWindows(); + return IsWindows; } internal static bool UsesCodeSignedAssemblies() { - return !Platform.IsX(); + return !Platform.IsCore; } // This is mainly with respect to the auto-mounting of // disconnected network drives on Windows internal static bool HasDriveAutoMounting() { - return IsWindows(); + return IsWindows; } // Linux does not have a registry internal static bool HasRegistrySupport() { - return IsWindows(); + return IsWindows; } // Linux does not have PowerShell execution policies internal static bool HasExecutionPolicy() { - return IsWindows(); + return IsWindows; } // Linux has a single rooted file system internal static bool HasSingleRootFilesystem() { - return !IsWindows(); + return !IsWindows; } // Linux has no notion of file shares. It has mount points @@ -140,7 +155,7 @@ namespace System.Management.Automation // the UNC equivalent of a "network drive" aka mount would be the mount itself internal static bool HasUNCSupport() { - return IsWindows(); + return IsWindows; } // Linux uses .net to query file attributes @@ -152,25 +167,25 @@ namespace System.Management.Automation // Linux does not have group policy support internal static bool HasGroupPolicySupport() { - return IsWindows(); + return IsWindows; } // non-windows does not have network drive support internal static bool HasNetworkDriveSupport() { - return IsWindows(); + return IsWindows; } // non-windows does not have reparse points internal static bool SupportsReparsePoints() { - return IsWindows(); + return IsWindows; } // non-windows does not support removing drives internal static bool SupportsRemoveDrive() { - return IsWindows(); + return IsWindows; } // Platform methods prefixed NonWindows are: @@ -190,7 +205,7 @@ namespace System.Management.Automation internal static bool NonWindowsIsHardLink(FileSystemInfo fileInfo) { - if (IsLinux()) + if (!IsWindows) { return LinuxPlatform.IsHardLink(fileInfo); } @@ -200,7 +215,7 @@ namespace System.Management.Automation internal static bool NonWindowsIsSymLink(FileSystemInfo fileInfo) { - if (IsLinux()) + if (!IsWindows) { return LinuxPlatform.IsSymLink(fileInfo); } @@ -216,7 +231,7 @@ namespace System.Management.Automation internal static string NonWindowsInternalGetTarget(string path) { - if (IsLinux()) + if (!IsWindows) { return LinuxPlatform.FollowSymLink(path); } @@ -229,7 +244,7 @@ namespace System.Management.Automation #if CORECLR internal static string NonWindowsGetFolderPath(SpecialFolder folder) { - if (IsLinux()) + if (!IsWindows) { return LinuxPlatform.GetFolderPath(folder); } @@ -242,7 +257,7 @@ namespace System.Management.Automation internal static string NonWindowsInternalGetLinkType(FileSystemInfo fileInfo) { - if (IsLinux()) + if (!IsWindows) { if (NonWindowsIsSymLink(fileInfo)) { @@ -263,7 +278,7 @@ namespace System.Management.Automation internal static bool NonWindowsCreateSymbolicLink(string path, string strTargetPath, bool isDirectory) { - if (IsLinux()) + if (!IsWindows) { // Linux doesn't care if target is a directory or not return LinuxPlatform.CreateSymbolicLink(path, strTargetPath); @@ -276,7 +291,7 @@ namespace System.Management.Automation internal static bool NonWindowsCreateHardLink(string path, string strTargetPath) { - if (IsLinux()) + if (!IsWindows) { return LinuxPlatform.CreateHardLink(path, strTargetPath); } @@ -288,7 +303,7 @@ namespace System.Management.Automation internal static void NonWindowsSetDate(DateTime dateToUse) { - if (IsLinux()) + if (!IsWindows) { LinuxPlatform.SetDateInfoInternal date = new LinuxPlatform.SetDateInfoInternal(dateToUse); LinuxPlatform.SetDate(date); @@ -301,7 +316,7 @@ namespace System.Management.Automation internal static string NonWindowsGetDomainName() { - if (IsLinux()) + if (!IsWindows) { string fullyQualifiedName = LinuxPlatform.Native.GetFullyQualifiedName(); if (string.IsNullOrEmpty(fullyQualifiedName)) @@ -326,7 +341,7 @@ namespace System.Management.Automation internal static string NonWindowsGetUserName() { - if (IsLinux()) + if (!IsWindows) { return LinuxPlatform.UserName; } @@ -338,7 +353,7 @@ namespace System.Management.Automation internal static string NonWindowsGetMachineName() { - if (IsLinux()) + if (!IsWindows) { return LinuxPlatform.HostName; } @@ -351,7 +366,7 @@ namespace System.Management.Automation // Hostname in this context seems to be the FQDN internal static string NonWindowsGetHostName() { - if (IsLinux()) + if (!IsWindows) { string hostName = LinuxPlatform.Native.GetFullyQualifiedName(); if (string.IsNullOrEmpty(hostName)) @@ -370,7 +385,7 @@ namespace System.Management.Automation internal static bool NonWindowsIsExecutable(string path) { - if (IsLinux()) + if (!IsWindows) { return LinuxPlatform.IsExecutable(path); } diff --git a/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs b/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs index f37434522..1127b1e1d 100644 --- a/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs +++ b/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs @@ -1810,7 +1810,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format // optional pre-load and binding verification // Porting Note: DLL.resources are not currently available on Linux - if (!Platform.IsX() && this.VerifyStringResources) + if (!Platform.IsCore && this.VerifyStringResources) { DisplayResourceManagerCache.LoadingResult result; DisplayResourceManagerCache.AssemblyBindingStatus bindingStatus; diff --git a/src/System.Management.Automation/engine/CommandDiscovery.cs b/src/System.Management.Automation/engine/CommandDiscovery.cs index 2d57504b8..2d0e2e917 100644 --- a/src/System.Management.Automation/engine/CommandDiscovery.cs +++ b/src/System.Management.Automation/engine/CommandDiscovery.cs @@ -1323,7 +1323,7 @@ namespace System.Management.Automation // Close the progress pane that may have popped up from analyzing UNC paths. // Porting note: we don't like this message - if (!Platform.IsX() && context.CurrentCommandProcessor != null) + if (!Platform.IsCore && context.CurrentCommandProcessor != null) { ProgressRecord analysisProgress = new ProgressRecord(0, Modules.ScriptAnalysisPreparing, " "); analysisProgress.RecordType = ProgressRecordType.Completed; diff --git a/src/System.Management.Automation/engine/CommandPathSearch.cs b/src/System.Management.Automation/engine/CommandPathSearch.cs index 0c4edaf8f..bdb64e59d 100644 --- a/src/System.Management.Automation/engine/CommandPathSearch.cs +++ b/src/System.Management.Automation/engine/CommandPathSearch.cs @@ -56,7 +56,7 @@ namespace System.Management.Automation // turn the command name into a pattern and then match against extensions in PATHEXT. // The old code would enumerate the file system many more times, once per possible extension. // Porting note: this is wrong on Linux, where we don't depend on extensions - if (Platform.IsWindows()) + if (Platform.IsWindows) { commandName = commandName + ".*"; } @@ -478,7 +478,7 @@ namespace System.Management.Automation for (int i = 0; i < baseNames.Length; i++) { if (name.Equals(baseNames[i], StringComparison.OrdinalIgnoreCase) - || (!Platform.IsWindows() && Platform.NonWindowsIsExecutable(name))) + || (!Platform.IsWindows && Platform.NonWindowsIsExecutable(name))) { if (result == null) result = new Collection(); @@ -505,7 +505,7 @@ namespace System.Management.Automation foreach (var fileName in fileNames) { if (fileName.EndsWith(allowedExt, StringComparison.OrdinalIgnoreCase) - || (!Platform.IsWindows() && Platform.NonWindowsIsExecutable(fileName))) + || (!Platform.IsWindows && Platform.NonWindowsIsExecutable(fileName))) { if (result == null) result = new Collection(); diff --git a/src/System.Management.Automation/engine/DataStoreAdapterProvider.cs b/src/System.Management.Automation/engine/DataStoreAdapterProvider.cs index 12d63374d..a8f5c30ab 100644 --- a/src/System.Management.Automation/engine/DataStoreAdapterProvider.cs +++ b/src/System.Management.Automation/engine/DataStoreAdapterProvider.cs @@ -499,7 +499,7 @@ namespace System.Management.Automation // TODO:PSL // this is probably not right here - if (implementingType == typeof(Microsoft.PowerShell.Commands.FileSystemProvider) && !Platform.IsWindows()) + if (implementingType == typeof(Microsoft.PowerShell.Commands.FileSystemProvider) && !Platform.IsWindows) { volumeSeparatedByColon = false; } diff --git a/src/System.Management.Automation/engine/InformationRecord.cs b/src/System.Management.Automation/engine/InformationRecord.cs index b698501db..34546787e 100644 --- a/src/System.Management.Automation/engine/InformationRecord.cs +++ b/src/System.Management.Automation/engine/InformationRecord.cs @@ -38,7 +38,7 @@ namespace System.Management.Automation this.TimeGenerated = DateTime.Now; this.Tags = new List(); - if (Platform.IsWindows()) + if (Platform.IsWindows) { this.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name; } diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index caff5c2ea..1eb0e80bb 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -4724,7 +4724,7 @@ End /// internal static string GetClearHostFunctionText() { - if (Platform.IsWindows()) + if (Platform.IsWindows) { return @" $RawUI = $Host.UI.RawUI @@ -5101,6 +5101,32 @@ end RemotingErrorIdStrings.PSSessionAppName, ScopedItemOptions.None), // End: Variables which control remoting behavior + + #region Platform + new SessionStateVariableEntry( + SpecialVariables.IsLinux, + Platform.IsLinux, + String.Empty, + ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope), + + new SessionStateVariableEntry( + SpecialVariables.IsOSX, + Platform.IsOSX, + String.Empty, + ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope), + + new SessionStateVariableEntry( + SpecialVariables.IsWindows, + Platform.IsWindows, + String.Empty, + ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope), + + new SessionStateVariableEntry( + SpecialVariables.IsCore, + Platform.IsCore, + String.Empty, + ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope), + #endregion }; /// @@ -5662,7 +5688,7 @@ if($paths) { // Porting note: the snapins still require 'ProcessorArchitecture=MSIL' in // the strong name, which is not in the strong name of assemblies created // by dotnet-cli - if (!Platform.IsX() && + if (!Platform.IsCore && !string.Equals(assemblyName.FullName, psSnapInInfo.AssemblyName, StringComparison.OrdinalIgnoreCase)) { string message = StringUtil.Format(ConsoleInfoErrorStrings.PSSnapInAssemblyNameMismatch, psSnapInInfo.AbsoluteModulePath, psSnapInInfo.AssemblyName); diff --git a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs index ca0afb099..6fe29f7e8 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs @@ -582,7 +582,7 @@ namespace System.Management.Automation // module using ""System32" in the key. // Porting note: psHome cannot be lower-cased on case sensitive file systems - if (Platform.IsWindows()) + if (Platform.IsWindows) { psHome = psHome.ToLowerInvariant().Replace("\\syswow64\\", "\\system32\\"); } @@ -600,7 +600,7 @@ namespace System.Management.Automation /// internal static string GetDscModulePath() { - if (!Platform.IsWindows()) + if (!Platform.IsWindows) { return string.Empty; } @@ -763,7 +763,7 @@ namespace System.Management.Automation // Porting note: Open PowerShell has a Modules folder in the the application base path which contains the built-in modules // It must be in the front of the path no matter what. - if (Platform.IsX()) + if (Platform.IsCore) { currentProcessModulePath = AddToPath(currentProcessModulePath, GetSystemwideModulePath(), 0); } diff --git a/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs b/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs index b9636c6a0..50a4675cc 100644 --- a/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs +++ b/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs @@ -131,7 +131,7 @@ namespace System.Management.Automation { var rawArgs = arguments.ToString(); - if (tracer.IsEnabled && Platform.IsWindows()) + if (tracer.IsEnabled && Platform.IsWindows) { // This tracing really shouldn't be in the property getter... tracer.WriteLine("Raw argument string: {0}", rawArgs); diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index 9fc15982d..fb025bce9 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1026,8 +1026,8 @@ namespace System.Management.Automation // On Windows, check the extension list and see if we should try to execute this directly. // Otherwise, use the platform library to check executability - if ((Platform.IsWindows() && ValidateExtension(this.Path)) - || (!Platform.IsWindows() && Platform.NonWindowsIsExecutable(this.Path))) + if ((Platform.IsWindows && ValidateExtension(this.Path)) + || (!Platform.IsWindows && Platform.NonWindowsIsExecutable(this.Path))) { startInfo.UseShellExecute = false; if (redirectInput) diff --git a/src/System.Management.Automation/engine/SecurityManagerBase.cs b/src/System.Management.Automation/engine/SecurityManagerBase.cs index 8d89b859b..a9120d27d 100644 --- a/src/System.Management.Automation/engine/SecurityManagerBase.cs +++ b/src/System.Management.Automation/engine/SecurityManagerBase.cs @@ -90,7 +90,7 @@ namespace System.Management.Automation // TODO:PSL this is a workaround since the exception below // hides the internal issue of what's going on in terms of // execution policy. - if (Platform.IsX()) + if (Platform.IsCore) { return; } diff --git a/src/System.Management.Automation/engine/SpecialVariables.cs b/src/System.Management.Automation/engine/SpecialVariables.cs index 2f6dd0a01..a6a83d883 100644 --- a/src/System.Management.Automation/engine/SpecialVariables.cs +++ b/src/System.Management.Automation/engine/SpecialVariables.cs @@ -156,6 +156,20 @@ namespace System.Management.Automation internal const string PSModuleAutoLoading = "PSModuleAutoLoadingPreference"; internal static VariablePath PSModuleAutoLoadingPreferenceVarPath = new VariablePath("global:" + PSModuleAutoLoading); + #region Platform Variables + internal const string IsLinux = "IsLinux"; + internal static VariablePath IsLinuxPath = new VariablePath("IsLinux"); + + internal const string IsOSX = "IsOSX"; + internal static VariablePath IsOSXPath = new VariablePath("IsOSX"); + + internal const string IsWindows = "IsWindows"; + internal static VariablePath IsWindowsPath = new VariablePath("IsWindows"); + + internal const string IsCore = "IsCore"; + internal static VariablePath IsCorePath = new VariablePath("IsCore"); + + #endregion #region Preference Variables internal const string DebugPreference = "DebugPreference"; diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index d32c79f4d..2c960f93e 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -240,7 +240,7 @@ namespace System.Management.Automation /// internal static string GetApplicationBase(string shellId) { - if (!Platform.IsX()) + if (!Platform.IsCore) { // try to get the path from the registry first string result = GetApplicationBaseFromRegistry(shellId); @@ -305,7 +305,7 @@ namespace System.Management.Automation // And built-in modules string progFileDir; - if (Platform.IsX()) + if (Platform.IsCore) { progFileDir = Path.Combine(appBase, "Modules"); } @@ -607,7 +607,7 @@ namespace System.Management.Automation /// Profile uses this to control profile loading. /// internal static string ProductNameForDirectory = - Platform.IsWindows() ? "WindowsPowerShell" : Platform.ProductNameForDirectory; + Platform.IsWindows ? "WindowsPowerShell" : Platform.ProductNameForDirectory; /// /// The name of the subdirectory that contains packages. @@ -964,7 +964,7 @@ namespace System.Management.Automation // SecurityPrincipal API of .net for now assume Linux // users have no concept of "Administrator" for now and // specifically do not map to a check for root user - if (Platform.IsWindows()) + if (Platform.IsWindows) { System.Security.Principal.WindowsIdentity currentIdentity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(currentIdentity); diff --git a/src/System.Management.Automation/engine/remoting/common/RemoteSessionNamedPipe.cs b/src/System.Management.Automation/engine/remoting/common/RemoteSessionNamedPipe.cs index 0c6aaa43e..89bfdf878 100644 --- a/src/System.Management.Automation/engine/remoting/common/RemoteSessionNamedPipe.cs +++ b/src/System.Management.Automation/engine/remoting/common/RemoteSessionNamedPipe.cs @@ -534,7 +534,7 @@ namespace System.Management.Automation.Remoting // All PowerShell instances will start with the named pipe // and listner created and running. - if (Platform.IsWindows()) + if (Platform.IsWindows) { IPCNamedPipeServerEnabled = true; } diff --git a/src/System.Management.Automation/engine/remoting/fanin/WSManPlugin.cs b/src/System.Management.Automation/engine/remoting/fanin/WSManPlugin.cs index c9fdc61a2..a5d9e3ed2 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/WSManPlugin.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/WSManPlugin.cs @@ -283,7 +283,7 @@ namespace System.Management.Automation.Remoting // to verify here. WSManPluginServerTransportManager serverTransportMgr; - if (Platform.IsWindows()) + if (Platform.IsWindows) { serverTransportMgr = new WSManPluginServerTransportManager(BaseTransportManager.DefaultFragmentSize, new PSRemotingCryptoHelperServer()); } @@ -390,7 +390,7 @@ namespace System.Management.Automation.Remoting // Wrap the provided handle so it can be passed to the registration function EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset); - if (Platform.IsWindows()) + if (Platform.IsWindows) { SafeWaitHandle safeWaitHandle = new SafeWaitHandle(requestDetails.shutdownNotificationHandle, false); // Owned by WinRM ClrFacade.SetSafeWaitHandle(eventWaitHandle, safeWaitHandle); diff --git a/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs b/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs index cf55c288c..ff39a87d0 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs @@ -326,7 +326,7 @@ namespace System.Management.Automation.Remoting this.shutdownPluginGCHandle = GCHandle.Alloc(shutdownPlugin); unmanagedStruct.wsManPluginShutdownPluginCallbackNative = Marshal.GetFunctionPointerForDelegate(shutdownPlugin); } - if(!Platform.IsWindows()) + if(!Platform.IsWindows) { WSMPluginOperationShutdownDelegate pluginShutDownDelegate = new WSMPluginOperationShutdownDelegate(WSManPluginManagedEntryWrapper.WSManPSShutdown); this.WSMPluginOperationShutdownGCHandle = GCHandle.Alloc(pluginShutDownDelegate); @@ -351,7 +351,7 @@ namespace System.Management.Automation.Remoting this.pluginSignalGCHandle.Free(); this.pluginConnectGCHandle.Free(); this.shutdownPluginGCHandle.Free(); - if(!Platform.IsWindows()) + if(!Platform.IsWindows) { this.WSMPluginOperationShutdownGCHandle.Free(); } diff --git a/src/System.Management.Automation/engine/remoting/fanin/WSManPluginShellSession.cs b/src/System.Management.Automation/engine/remoting/fanin/WSManPluginShellSession.cs index b311c2f39..41913739b 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/WSManPluginShellSession.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/WSManPluginShellSession.cs @@ -265,7 +265,7 @@ namespace System.Management.Automation.Remoting //RACE TO BE FIXED - As soon as this API is called, WinRM service will send CommandResponse back and Signal is expected anytime // If Signal comes and executes before registering the notification handle, cleanup will be messed result = WSManNativeApi.WSManPluginReportContext(creationRequestDetails.unmanagedHandle, 0, creationRequestDetails.unmanagedHandle); - if (Platform.IsWindows() && (WSManPluginConstants.ExitCodeSuccess == result)) + if (Platform.IsWindows && (WSManPluginConstants.ExitCodeSuccess == result)) { registeredShutdownNotification = 1; diff --git a/src/System.Management.Automation/engine/remoting/fanin/WSManPluginTransportManager.cs b/src/System.Management.Automation/engine/remoting/fanin/WSManPluginTransportManager.cs index 9db15cbf3..7c49a68bd 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/WSManPluginTransportManager.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/WSManPluginTransportManager.cs @@ -332,7 +332,7 @@ namespace System.Management.Automation.Remoting isRequestPending = true; this.requestDetails = requestDetails; - if (Platform.IsWindows()) + if (Platform.IsWindows) { // Wrap the provided handle so it can be passed to the registration function SafeWaitHandle safeWaitHandle = new SafeWaitHandle(requestDetails.shutdownNotificationHandle, false); // Owned by WinRM diff --git a/src/System.Management.Automation/engine/remoting/server/ServerPowerShellDriver.cs b/src/System.Management.Automation/engine/remoting/server/ServerPowerShellDriver.cs index 692aeaf80..0f1e57ede 100644 --- a/src/System.Management.Automation/engine/remoting/server/ServerPowerShellDriver.cs +++ b/src/System.Management.Automation/engine/remoting/server/ServerPowerShellDriver.cs @@ -336,7 +336,7 @@ namespace System.Management.Automation // Flow the impersonation policy to pipeline execution thread // only if the current thread is impersonated (Delegation is // also a kind of impersonation). - if (Platform.IsWindows()) + if (Platform.IsWindows) { WindowsIdentity currentThreadIdentity = WindowsIdentity.GetCurrent(); switch (currentThreadIdentity.ImpersonationLevel) diff --git a/src/System.Management.Automation/engine/remoting/server/serverremotesession.cs b/src/System.Management.Automation/engine/remoting/server/serverremotesession.cs index d1e7d7c41..d2e5b8955 100644 --- a/src/System.Management.Automation/engine/remoting/server/serverremotesession.cs +++ b/src/System.Management.Automation/engine/remoting/server/serverremotesession.cs @@ -170,7 +170,7 @@ namespace System.Management.Automation.Remoting _senderInfo = senderInfo; _configProviderId = configurationProviderId; _initParameters = initializationParameters; - if (Platform.IsWindows()) + if (Platform.IsWindows) { _cryptoHelper = (PSRemotingCryptoHelperServer)transportManager.CryptoHelper; _cryptoHelper.Session = this; diff --git a/src/System.Management.Automation/namespaces/FileSystemContentStream.cs b/src/System.Management.Automation/namespaces/FileSystemContentStream.cs index 15df54f65..8539cb77e 100644 --- a/src/System.Management.Automation/namespaces/FileSystemContentStream.cs +++ b/src/System.Management.Automation/namespaces/FileSystemContentStream.cs @@ -1195,7 +1195,7 @@ namespace Microsoft.PowerShell.Commands // Porting note: only UTF-8 is supported on Linux, which is not an SBCS if ((_currentEncoding.Equals(_oemEncoding) || _currentEncoding.Equals(_defaultAnsiEncoding)) - && !Platform.IsLinux()) + && Platform.IsWindows) { NativeMethods.CPINFO cpInfo; if (NativeMethods.GetCPInfo((uint)_currentEncoding.CodePage, out cpInfo) && diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 50bd189a0..ab14d7cf7 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -490,7 +490,7 @@ namespace Microsoft.PowerShell.Commands private void MapNetworkDrive(PSDriveInfo drive) { // Porting note: mapped network drives are only supported on Windows - if (Platform.IsWindows()) + if (Platform.IsWindows) { WinMapNetworkDrive(drive); } @@ -992,7 +992,7 @@ namespace Microsoft.PowerShell.Commands // Porting notes: on the non-Windows platforms, the drive never // uses : as a separator between drive and path - if (!Platform.IsWindows()) + if (!Platform.IsWindows) { newPSDriveInfo.VolumeSeparatedByColon = false; } @@ -1505,7 +1505,7 @@ namespace Microsoft.PowerShell.Commands { DirectoryInfo directory = new DirectoryInfo(path); - if (!Platform.IsWindows() && Platform.NonWindowsIsSymLink(directory)) + if (!Platform.IsWindows && Platform.NonWindowsIsSymLink(directory)) { // For Linux, treat symlink to directories like a file WriteItemObject(directory, path, false); @@ -2210,7 +2210,7 @@ namespace Microsoft.PowerShell.Commands if(itemType == ItemType.SymbolicLink) { - if (Platform.IsWindows()) + if (Platform.IsWindows) { success = WinCreateSymbolicLink(path,strTargetPath,isDirectory); } @@ -2221,7 +2221,7 @@ namespace Microsoft.PowerShell.Commands } else if(itemType == ItemType.HardLink) { - if (Platform.IsWindows()) + if (Platform.IsWindows) { success = WinCreateHardLink(path,strTargetPath); } @@ -7981,7 +7981,7 @@ namespace Microsoft.PowerShell.Commands if (fileSysInfo != null) { - if (Platform.IsWindows()) + if (Platform.IsWindows) { using (SafeFileHandle handle = OpenReparsePoint(fileSysInfo.FullName, FileDesiredAccess.GenericRead)) { @@ -8018,7 +8018,7 @@ namespace Microsoft.PowerShell.Commands private static List InternalGetTarget(string filePath) { var links = new List(); - if (!Platform.IsWindows()) + if (!Platform.IsWindows) { string link = Platform.NonWindowsInternalGetTarget(filePath); if (!String.IsNullOrEmpty(link)) @@ -8092,7 +8092,7 @@ namespace Microsoft.PowerShell.Commands private static string InternalGetLinkType(FileSystemInfo fileInfo) { - if (Platform.IsWindows()) + if (Platform.IsWindows) { return WinInternalGetLinkType(fileInfo.FullName); } @@ -8105,7 +8105,7 @@ namespace Microsoft.PowerShell.Commands [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods")] private static string WinInternalGetLinkType(string filePath) { - if (Platform.IsLinux()) + if (!Platform.IsWindows) { throw new Platform.PlatformNotSupportedException(); } @@ -8169,7 +8169,7 @@ namespace Microsoft.PowerShell.Commands internal static bool IsHardLink(FileSystemInfo fileInfo) { - if (Platform.IsWindows()) + if (Platform.IsWindows) return WinIsHardLink(fileInfo); else return Platform.NonWindowsIsHardLink(fileInfo); @@ -8177,7 +8177,7 @@ namespace Microsoft.PowerShell.Commands internal static bool IsReparsePoint(FileSystemInfo fileInfo) { - if (Platform.IsWindows()) + if (Platform.IsWindows) { // Note that this class also has a enum called FileAttributes, so use fully qualified name return (fileInfo.Attributes & System.IO.FileAttributes.ReparsePoint) @@ -8228,7 +8228,7 @@ namespace Microsoft.PowerShell.Commands internal static bool IsHardLink(ref IntPtr handle) { - if (Platform.IsWindows()) + if (Platform.IsWindows) return WinIsHardLink(ref handle); else return Platform.NonWindowsIsHardLink(ref handle); @@ -8256,7 +8256,7 @@ namespace Microsoft.PowerShell.Commands private static string InternalGetTarget(SafeFileHandle handle) { - if (Platform.IsWindows()) + if (Platform.IsWindows) { return WinInternalGetTarget(handle); } @@ -8336,7 +8336,7 @@ namespace Microsoft.PowerShell.Commands { // this is a purely Windows specific feature, no feature flag // used for that reason - if (Platform.IsWindows()) + if (Platform.IsWindows) { return WinCreateJunction(path,target); } @@ -8420,7 +8420,7 @@ namespace Microsoft.PowerShell.Commands if (!String.IsNullOrEmpty(junctionPath)) { - if (!Platform.IsWindows()) + if (!Platform.IsWindows) { // For non-Windows platform, treat it as a file. Just delete it. try diff --git a/src/System.Management.Automation/security/SecuritySupport.cs b/src/System.Management.Automation/security/SecuritySupport.cs index 1a6b0ff4f..755b1df4c 100644 --- a/src/System.Management.Automation/security/SecuritySupport.cs +++ b/src/System.Management.Automation/security/SecuritySupport.cs @@ -433,7 +433,7 @@ namespace System.Management.Automation // There is no signature support on non-Windows platforms (yet), when // execution reaches here, we are sure the file is under product folder - if (Platform.IsX()) + if (Platform.IsCore) { return true; } diff --git a/src/System.Management.Automation/utils/PsUtils.cs b/src/System.Management.Automation/utils/PsUtils.cs index deb01a119..78b0b54c1 100644 --- a/src/System.Management.Automation/utils/PsUtils.cs +++ b/src/System.Management.Automation/utils/PsUtils.cs @@ -586,7 +586,7 @@ namespace System.Management.Automation internal static string GetHostName() { // Note: non-windows CoreCLR does not support System.Net yet - if (Platform.IsWindows()) + if (Platform.IsWindows) { return WinGetHostName(); } @@ -612,7 +612,7 @@ namespace System.Management.Automation internal static uint GetNativeThreadId() { - if (Platform.IsWindows()) + if (Platform.IsWindows) { return WinGetNativeThreadId(); } diff --git a/test/csharp/test_CorePsPlatform.cs b/test/csharp/test_CorePsPlatform.cs index e36f5aa4f..a99c3b0f9 100644 --- a/test/csharp/test_CorePsPlatform.cs +++ b/test/csharp/test_CorePsPlatform.cs @@ -12,7 +12,19 @@ namespace PSTests [Fact] public static void TestIsLinux() { - Assert.True(Platform.IsLinux()); + Assert.True(Platform.IsLinux); + } + + [Fact] + public static void TestIsWindows() + { + Assert.False(Platform.IsWindows); + } + + [Fact] + public static void TestIsCore() + { + Assert.True(Platform.IsCore); } [Fact] diff --git a/test/powershell/Import-Module.Tests.ps1 b/test/powershell/Import-Module.Tests.ps1 index cfbe45879..52e8dd1b3 100644 --- a/test/powershell/Import-Module.Tests.ps1 +++ b/test/powershell/Import-Module.Tests.ps1 @@ -1,5 +1,5 @@ Describe "Import-Module" { - $moduleName = "Microsoft.PowerShell.Platform" + $moduleName = "Microsoft.PowerShell.Security" BeforeEach { Remove-Module -Name $moduleName -Force @@ -21,4 +21,4 @@ { Import-Module -ModuleInfo $a } | Should Not Throw (Get-Module -Name $moduleName).Name | Should Be $moduleName } -} \ No newline at end of file +} diff --git a/test/powershell/Remove-Module.Tests.ps1 b/test/powershell/Remove-Module.Tests.ps1 index f5d65658f..c1d8eae88 100644 --- a/test/powershell/Remove-Module.Tests.ps1 +++ b/test/powershell/Remove-Module.Tests.ps1 @@ -1,5 +1,5 @@ Describe "Remove-Module" { - $moduleName = "Microsoft.PowerShell.Platform" + $moduleName = "Microsoft.PowerShell.Security" BeforeEach { Import-Module -Name $moduleName -Force diff --git a/test/powershell/Select-String.Tests.ps1 b/test/powershell/Select-String.Tests.ps1 index dcac4471a..7a6b2456c 100644 --- a/test/powershell/Select-String.Tests.ps1 +++ b/test/powershell/Select-String.Tests.ps1 @@ -87,8 +87,6 @@ Describe "Select-String" { } Context "Filesytem actions" { - $isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows) - $testDirectory = $TestDrive $testInputFile = Join-Path -Path $testDirectory -ChildPath testfile1.txt