Skip to content

Add PSModulePath paths when retrieving resource paths #700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 23 additions & 29 deletions src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -602,16 +602,17 @@ public static string GetInstalledPackageName(string pkgPath)
// expecting the full version module path
// ex: ./PowerShell/Modules/TestModule/1.0.0
return new DirectoryInfo(pkgPath).Parent.Name;
}

public static List<string> GetAllResourcePaths(
}

// Find all potential resource paths
public static List<string> GetPathsFromEnvVarAndScope(
PSCmdlet psCmdlet,
ScopeType? scope = null)
{
GetStandardPlatformPaths(
psCmdlet,
out string myDocumentsPath,
out string programFilesPath);
ScopeType? scope)
{
GetStandardPlatformPaths(
psCmdlet,
out string myDocumentsPath,
out string programFilesPath);

List<string> resourcePaths = new List<string>();

Expand All @@ -634,6 +635,15 @@ public static List<string> GetAllResourcePaths(
resourcePaths.Add(Path.Combine(programFilesPath, "Scripts"));
}

return resourcePaths;
}

public static List<string> GetAllResourcePaths(
PSCmdlet psCmdlet,
ScopeType? scope = null)
{
List<String> resourcePaths = GetPathsFromEnvVarAndScope(psCmdlet, scope);

// resourcePaths should now contain, eg:
// ./PowerShell/Scripts
// ./PowerShell/Modules
Expand Down Expand Up @@ -680,26 +690,10 @@ public static List<string> GetAllResourcePaths(
// Find all potential installation paths given a scope
public static List<string> GetAllInstallationPaths(
PSCmdlet psCmdlet,
ScopeType scope)
{
GetStandardPlatformPaths(
psCmdlet,
out string myDocumentsPath,
out string programFilesPath);

// The default user scope is CurrentUser
var installationPaths = new List<string>();
if (scope == ScopeType.AllUsers)
{
installationPaths.Add(Path.Combine(programFilesPath, "Modules"));
installationPaths.Add(Path.Combine(programFilesPath, "Scripts"));
}
else
{
installationPaths.Add(Path.Combine(myDocumentsPath, "Modules"));
installationPaths.Add(Path.Combine(myDocumentsPath, "Scripts"));
}

ScopeType? scope)
{
List<String> installationPaths = GetPathsFromEnvVarAndScope(psCmdlet, scope);

installationPaths = installationPaths.Distinct(StringComparer.InvariantCultureIgnoreCase).ToList();
installationPaths.ForEach(dir => psCmdlet.WriteVerbose(string.Format("All paths to search: '{0}'", dir)));

Expand Down
7 changes: 7 additions & 0 deletions test/InstallPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ Describe 'Test Install-PSResource for Module' {
$pkg.Version | Should -Be "5.0.0.0"
}

It "Install resource under specified in PSModulePath" {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository
$pkg = Get-PSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
($env:PSModulePath).Contains($pkg.InstalledLocation)
}

# Windows only
It "Install resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Scope CurrentUser
Expand Down