From 54784cb77a97d4fdd27b0fd796c2f1db3a0d22da Mon Sep 17 00:00:00 2001 From: quoctruong91 Date: Tue, 5 Jul 2016 14:16:35 -0700 Subject: [PATCH 1/2] Expose Platform API from SMA and uses that in NuGetProvider --- .../NugetLightRequest.cs | 17 ++++++++++++++ .../CoreCLR/CorePsPlatform.cs | 22 +++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PackageManagement.NuGetProvider/NugetLightRequest.cs b/src/Microsoft.PackageManagement.NuGetProvider/NugetLightRequest.cs index 4cc643090a..901591aa28 100644 --- a/src/Microsoft.PackageManagement.NuGetProvider/NugetLightRequest.cs +++ b/src/Microsoft.PackageManagement.NuGetProvider/NugetLightRequest.cs @@ -171,6 +171,10 @@ // or $env:programfiles\NuGet\Packages\ if you are an admin. try { +#if LINUX + // there is only 1 installation location by default for linux ("HOME/.local/share/powershell/PackageManagement/NuGet/Packages") + string basePath = CurrentUserDefaultInstallLocation; +#else var scope = (Scope == null) ? null : Scope.Value; scope = string.IsNullOrWhiteSpace(scope) ? Constants.AllUsers : scope; string basePath; @@ -192,6 +196,7 @@ Constants.Messages.InstallRequiresCurrentUserScopeParameterForNonAdminUser, AllUserDefaultInstallLocation, CurrentUserDefaultInstallLocation); return string.Empty; } +#endif if (!Directory.Exists(basePath)) { @@ -215,7 +220,11 @@ get { #if CORECLR +#if LINUX + return Path.Combine(Platform.SelectProductNameForDirectory(Platform.XDG_Type.POWERSHELL), "PackageManagement", "NuGet", "Packages"); +#else return Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "PackageManagement", "NuGet", "Packages"); +#endif #else return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PackageManagement", "NuGet", "Packages"); #endif @@ -228,7 +237,11 @@ get { #if CORECLR +#if LINUX + return Path.Combine(Platform.SelectProductNameForDirectory(Platform.XDG_Type.POWERSHELL), "PackageManagement", "NuGet", "Packages"); +#else return Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"), "NuGet", "Packages"); +#endif #else return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "NuGet", "Packages"); #endif @@ -1289,7 +1302,11 @@ } } else { +#if LINUX + var appdataFolder = Platform.SelectProductNameForDirectory(Platform.XDG_Type.PROFILE); +#else var appdataFolder = Environment.GetEnvironmentVariable("appdata"); +#endif _configurationFileLocation = Path.Combine(appdataFolder, "NuGet", NuGetConstant.SettingsFileName); //create directory if does not exist diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index c9cd032176..03cccd0985 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -28,7 +28,7 @@ namespace System.Management.Automation /// All these properties are calling into platform specific static classes, to make /// sure the platform implementations are switched at runtime (including pinvokes). /// - internal static class Platform + public static class Platform { // Platform variables used to defined corresponding PowerShell built-in variables @@ -50,6 +50,7 @@ namespace System.Management.Automation PROFILE, MODULES, CACHE, + POWERSHELL, DEFAULT } @@ -115,7 +116,8 @@ namespace System.Management.Automation string xdgdatahome = System.Environment.GetEnvironmentVariable("XDG_DATA_HOME"); string xdgcachehome = System.Environment.GetEnvironmentVariable("XDG_CACHE_HOME"); string xdgConfigHomeDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".config", "powershell"); - string xdgModuleDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".local", "share", "powershell", "Modules"); + string xdgPowerShellDefault = Path.Combine( System.Environment.GetEnvironmentVariable("HOME"), ".local", "share", "powershell"); + string xdgModuleDefault = Path.Combine ( xdgPowerShellDefault, "Modules"); string xdgCacheDefault = Path.Combine (System.Environment.GetEnvironmentVariable("HOME"), ".cache", "powershell"); switch (dirpath){ @@ -132,6 +134,22 @@ namespace System.Management.Automation return Path.Combine(xdgconfighome, "powershell"); } + case Platform.XDG_Type.POWERSHELL: + //the user has set XDG_DATA_HOME corresponding to module path + if (String.IsNullOrEmpty(xdgdatahome)){ + + // create the xdg folder if needed + if (!Directory.Exists(xdgPowerShellDefault)) + { + Directory.CreateDirectory(xdgPowerShellDefault); + } + return xdgPowerShellDefault; + } + else + { + return Path.Combine(xdgdatahome, "powershell"); + } + case Platform.XDG_Type.MODULES: //the user has set XDG_DATA_HOME corresponding to module path if (String.IsNullOrEmpty(xdgdatahome)){ From 35ac5999ac1f2fb3b147726e5df7ecb634a7e985 Mon Sep 17 00:00:00 2001 From: quoctruong91 Date: Wed, 6 Jul 2016 10:28:31 -0700 Subject: [PATCH 2/2] Rename enum interface; Change location used in NuGetProvider to go up 1 level --- .../NugetLightRequest.cs | 6 ++--- .../CoreCLR/CorePsPlatform.cs | 23 +++++++++++-------- .../engine/Utils.cs | 2 +- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.PackageManagement.NuGetProvider/NugetLightRequest.cs b/src/Microsoft.PackageManagement.NuGetProvider/NugetLightRequest.cs index 901591aa28..0c0721b46e 100644 --- a/src/Microsoft.PackageManagement.NuGetProvider/NugetLightRequest.cs +++ b/src/Microsoft.PackageManagement.NuGetProvider/NugetLightRequest.cs @@ -221,7 +221,7 @@ { #if CORECLR #if LINUX - return Path.Combine(Platform.SelectProductNameForDirectory(Platform.XDG_Type.POWERSHELL), "PackageManagement", "NuGet", "Packages"); + return Path.Combine(Path.GetDirectoryName(Platform.SelectProductNameForDirectory(Platform.XDG_Type.DATA)), "PackageManagement", "NuGet", "Packages"); #else return Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "PackageManagement", "NuGet", "Packages"); #endif @@ -238,7 +238,7 @@ { #if CORECLR #if LINUX - return Path.Combine(Platform.SelectProductNameForDirectory(Platform.XDG_Type.POWERSHELL), "PackageManagement", "NuGet", "Packages"); + return Path.Combine(Path.GetDirectoryName(Platform.SelectProductNameForDirectory(Platform.XDG_Type.DATA)), "PackageManagement", "NuGet", "Packages"); #else return Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"), "NuGet", "Packages"); #endif @@ -1303,7 +1303,7 @@ } else { #if LINUX - var appdataFolder = Platform.SelectProductNameForDirectory(Platform.XDG_Type.PROFILE); + var appdataFolder = Path.GetDirectoryName(Platform.SelectProductNameForDirectory(Platform.XDG_Type.CONFIG)); #else var appdataFolder = Environment.GetEnvironmentVariable("appdata"); #endif diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index 03cccd0985..063ba49dda 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -47,10 +47,15 @@ namespace System.Management.Automation //enum for selecting the xdgpaths public enum XDG_Type { - PROFILE, + // location to store configuration file + CONFIG, + // location for powershell modules MODULES, + // location to store temporary files CACHE, - POWERSHELL, + // location to store data that application needs + DATA, + // default location DEFAULT } @@ -116,12 +121,12 @@ namespace System.Management.Automation string xdgdatahome = System.Environment.GetEnvironmentVariable("XDG_DATA_HOME"); string xdgcachehome = System.Environment.GetEnvironmentVariable("XDG_CACHE_HOME"); string xdgConfigHomeDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".config", "powershell"); - string xdgPowerShellDefault = Path.Combine( System.Environment.GetEnvironmentVariable("HOME"), ".local", "share", "powershell"); - string xdgModuleDefault = Path.Combine ( xdgPowerShellDefault, "Modules"); + string xdgDataHomeDefault = Path.Combine( System.Environment.GetEnvironmentVariable("HOME"), ".local", "share", "powershell"); + string xdgModuleDefault = Path.Combine ( xdgDataHomeDefault, "Modules"); string xdgCacheDefault = Path.Combine (System.Environment.GetEnvironmentVariable("HOME"), ".cache", "powershell"); switch (dirpath){ - case Platform.XDG_Type.PROFILE: + case Platform.XDG_Type.CONFIG: //the user has set XDG_CONFIG_HOME corrresponding to profile path if (String.IsNullOrEmpty(xdgconfighome)) { @@ -134,16 +139,16 @@ namespace System.Management.Automation return Path.Combine(xdgconfighome, "powershell"); } - case Platform.XDG_Type.POWERSHELL: + case Platform.XDG_Type.DATA: //the user has set XDG_DATA_HOME corresponding to module path if (String.IsNullOrEmpty(xdgdatahome)){ // create the xdg folder if needed - if (!Directory.Exists(xdgPowerShellDefault)) + if (!Directory.Exists(xdgDataHomeDefault)) { - Directory.CreateDirectory(xdgPowerShellDefault); + Directory.CreateDirectory(xdgDataHomeDefault); } - return xdgPowerShellDefault; + return xdgDataHomeDefault; } else { diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index d9fa665494..a9d4f2aafa 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -607,7 +607,7 @@ namespace System.Management.Automation /// Profile uses this to control profile loading. /// internal static string ProductNameForDirectory = - Platform.IsWindows ? "WindowsPowerShell" : Platform.SelectProductNameForDirectory(Platform.XDG_Type.PROFILE); + Platform.IsWindows ? "WindowsPowerShell" : Platform.SelectProductNameForDirectory(Platform.XDG_Type.CONFIG); /// /// The name of the subdirectory that contains packages.