Skip to content
Merged
9 changes: 5 additions & 4 deletions docs/installation/linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ sudo installer -pkg powershell-6.0.0-alpha.8-osx.10.11-x64.pkg -target /
Paths
=====

* User profiles will be read from `~/.config/powershell/profile.ps1`.
* User modules will be read from `~/.local/share/powershell/Modules`
* PSReadLine history will be recorded to `~/.local/share/powershell/PSReadLine/ConsoleHost_history.txt`
* `$PSHOME` is `/opt/microsoft/powershell/6.0.0-alpha.8/`
* Default profiles will be read from `$PSHOME/profile.ps1`.
* User profiles will be read from `~/.config/powershell/profile.ps1`
* Default profiles will be read from `$PSHOME/profile.ps1`
* User modules will be read from `~/.local/share/powershell/Modules`
* Shared modules will be read from `/usr/local/share/powershell/Modules`
* Default modules will be read from `$PSHOME/Modules`
* PSReadLine history will be recorded to `~/.local/share/powershell/PSReadLine/ConsoleHost_history.txt`

The profiles respect PowerShell's per-host configuration,
so the default host-specific profiles exists at `Microsoft.PowerShell_profile.ps1` in the same locations.
Expand Down
20 changes: 8 additions & 12 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,17 @@ internal static int Start(
try
{
string profileDir;
if (Platform.IsWindows)
{
profileDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
@"\Microsoft\Windows\PowerShell";
#if UNIX

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 😄

profileDir = Platform.SelectProductNameForDirectory(Platform.XDG_Type.CACHE);
#else
profileDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
@"\Microsoft\Windows\PowerShell";

if (!Directory.Exists(profileDir))
{
Directory.CreateDirectory(profileDir);
}
}
else
if (!Directory.Exists(profileDir))
{
profileDir = Platform.SelectProductNameForDirectory(Platform.XDG_Type.CACHE);
Directory.CreateDirectory(profileDir);
}

#endif
ClrFacade.SetProfileOptimizationRoot(profileDir);
}
catch
Expand Down
203 changes: 85 additions & 118 deletions src/System.Management.Automation/CoreCLR/CorePsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ internal static TypeCode GetTypeCodeInCoreClr(Type type)
#endregion Reflection and Type related extensions

#region Environment Extensions

// TODO:CORECLR - Environment Extensions need serious work to refine.
internal enum EnvironmentVariableTarget
{
Expand Down Expand Up @@ -824,37 +825,7 @@ public static void FailFast(string message, Exception exception)

public static string GetEnvironmentVariable(string variable)
{
string value = System.Environment.GetEnvironmentVariable(variable);

// Porting note: if not otherwise defined, map Windows environment
// variables to their corresponding Linux counterparts
if (!Platform.IsWindows && String.IsNullOrEmpty(value))
{
switch (variable)
{
case "OS":
return "Linux";

case "COMPUTERNAME":
return System.Environment.GetEnvironmentVariable("HOSTNAME");

case "USERNAME":
return System.Environment.GetEnvironmentVariable("USER");

case "HOMEPATH":
case "USERPROFILE":
return System.Environment.GetEnvironmentVariable("HOME");

case "TMP":
case "TEMP":
return System.Environment.GetEnvironmentVariable("TMPDIR");

default:
break;
}
}

return value;
return System.Environment.GetEnvironmentVariable(variable);
}

public static IDictionary GetEnvironmentVariables()
Expand Down Expand Up @@ -896,7 +867,6 @@ public static IDictionary GetEnvironmentVariables(EnvironmentVariableTarget targ
#if UNIX
return null;
#else

if( target == EnvironmentVariableTarget.Machine)
{
using (RegistryKey environmentKey =
Expand Down Expand Up @@ -957,7 +927,6 @@ public static string GetEnvironmentVariable(string variable, EnvironmentVariable
#if UNIX
return null;
#else

if (target == EnvironmentVariableTarget.Machine)
{
using (RegistryKey environmentKey =
Expand Down Expand Up @@ -986,47 +955,18 @@ public static string GetEnvironmentVariable(string variable, EnvironmentVariable

#region Property_Extensions

internal static string WinGetUserDomainName()
{
StringBuilder domainName = new StringBuilder(1024);
uint domainNameLen = (uint)domainName.Capacity;

byte ret = Win32Native.GetUserNameEx(Win32Native.NameSamCompatible, domainName, ref domainNameLen);
if (ret == 1)
{
string samName = domainName.ToString();
int index = samName.IndexOf('\\');
if (index != -1)
{
return samName.Substring(0, index);
}
}
else
{
int errorCode = Marshal.GetLastWin32Error();
throw new InvalidOperationException(Win32Native.GetMessage(errorCode));
}

// Cannot use LookupAccountNameW to get DomainName because 'GetUserName' is not available in CSS and thus we cannot get the account.
throw new InvalidOperationException(CoreClrStubResources.CannotGetDomainName);
}

/// <summary>
/// UserDomainName
/// </summary>
public static string UserDomainName
{
get
{
if (Platform.IsWindows)
{
return WinGetUserDomainName();
}
else
{
return Platform.NonWindowsGetDomainName();
}

#if UNIX
return Platform.NonWindowsGetDomainName();
#else
return WinGetUserDomainName();
#endif
}
}

Expand All @@ -1040,20 +980,7 @@ public static string UserName
#if UNIX
return Platform.Unix.UserName;
#else
StringBuilder domainName = new StringBuilder(1024);
uint domainNameLen = (uint)domainName.Capacity;

byte ret = Win32Native.GetUserNameEx(Win32Native.NameSamCompatible, domainName, ref domainNameLen);
if (ret == 1)
{
string samName = domainName.ToString();
int index = samName.IndexOf('\\');
if (index != -1)
{
return samName.Substring(index + 1);
}
}
return string.Empty;
return WinGetUserName();
#endif
}
}
Expand All @@ -1078,45 +1005,19 @@ public static OperatingSystem OSVersion
{
if (s_os == null)
{
if (Platform.IsWindows)
{
s_os = WinOSVersion;
}
else
{
// TODO:PSL use P/Invoke to provide proper version

// Porting note: cannot put this in CorePsPlatform since
// System.Management.Automation.Environment only exists in CoreCLR
// builds of monad.
s_os = new Environment.OperatingSystem(new Version(1, 0, 0, 0), "");
}
#if UNIX
// TODO:PSL use P/Invoke to provide proper version
// OSVersion will be back in CoreCLR 1.1
s_os = new Environment.OperatingSystem(new Version(1, 0, 0, 0), "");
#else
s_os = WinGetOSVersion();
#endif
}
return s_os;
}
}
private static volatile OperatingSystem s_os;

/// <summary>
/// Windows OSVersion implementation
/// </summary>
private static OperatingSystem WinOSVersion
{
get
{
Win32Native.OSVERSIONINFOEX osviex = new Win32Native.OSVERSIONINFOEX();
osviex.OSVersionInfoSize = Marshal.SizeOf(osviex);
if (!Win32Native.GetVersionEx(ref osviex))
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode);
}

Version v = new Version(osviex.MajorVersion, osviex.MinorVersion, osviex.BuildNumber, (osviex.ServicePackMajor << 16) | osviex.ServicePackMinor);
return new OperatingSystem(v, osviex.CSDVersion);
}
}

#endregion Property_Extensions

#region SpecialFolder_Extensions
Expand Down Expand Up @@ -1242,13 +1143,79 @@ private static string InternalGetFolderPath(SpecialFolder folder)

#endregion SpecialFolder_Extensions

#region NativeMethods
#region WinPlatform_Specific_Methods
#if !UNIX
/// <summary>
/// Windows UserDomainName implementation
/// </summary>
private static string WinGetUserDomainName()
{
StringBuilder domainName = new StringBuilder(1024);
uint domainNameLen = (uint)domainName.Capacity;

byte ret = Win32Native.GetUserNameEx(Win32Native.NameSamCompatible, domainName, ref domainNameLen);
if (ret == 1)
{
string samName = domainName.ToString();
int index = samName.IndexOf('\\');
if (index != -1)
{
return samName.Substring(0, index);
}
}
else
{
int errorCode = Marshal.GetLastWin32Error();
throw new InvalidOperationException(Win32Native.GetMessage(errorCode));
}

// Cannot use LookupAccountNameW to get DomainName because 'GetUserName' is not available in CSS and thus we cannot get the account.
throw new InvalidOperationException(CoreClrStubResources.CannotGetDomainName);
}

/// <summary>
/// Windows UserName implementation
/// </summary>
private static string WinGetUserName()
{
StringBuilder domainName = new StringBuilder(1024);
uint domainNameLen = (uint)domainName.Capacity;

byte ret = Win32Native.GetUserNameEx(Win32Native.NameSamCompatible, domainName, ref domainNameLen);
if (ret == 1)
{
string samName = domainName.ToString();
int index = samName.IndexOf('\\');
if (index != -1)
{
return samName.Substring(index + 1);
}
}
return string.Empty;
}

/// <summary>
/// Windows OSVersion implementation
/// </summary>
private static OperatingSystem WinGetOSVersion()
{
Win32Native.OSVERSIONINFOEX osviex = new Win32Native.OSVERSIONINFOEX();
osviex.OSVersionInfoSize = Marshal.SizeOf(osviex);
if (!Win32Native.GetVersionEx(ref osviex))
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode);
}

Version v = new Version(osviex.MajorVersion, osviex.MinorVersion, osviex.BuildNumber, (osviex.ServicePackMajor << 16) | osviex.ServicePackMinor);
return new OperatingSystem(v, osviex.CSDVersion);
}

/// <summary>
/// DllImport uses the ApiSet dll that is available on CSS, since this code
/// will only be included when building targeting CoreCLR.
/// </summary>
internal static class Win32Native
private static class Win32Native
{
internal const int NameSamCompatible = 2; // EXTENDED_NAME_FORMAT - NameSamCompatible

Expand Down Expand Up @@ -1307,8 +1274,8 @@ internal static string GetMessage(int errorCode)
}
}
}

#endregion NativeMethods
#endif
#endregion WinPlatform_Specific_Methods

#region NestedTypes

Expand Down
Loading