Skip to content

Commit 4d4559f

Browse files
authored
better install path (#41)
* Test-PathWritable * update logic
1 parent d991f3b commit 4d4559f

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Private/Get-InstallPath.ps1

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
function Get-InstallPath {
22

33
# returns OS specific path for module installation, it support only -Scope CurrentUser
4-
if ($IsLinux -or $IsOSX) {
4+
5+
$defaultPath = if ($IsLinux -or $IsOSX) {
56
#"$HOME/.local/share/powershell/Modules"
67
# https://github.com/PowerShell/PowerShellGet/blob/d4dfebbbec4dfbe73392719a8a331541ed75d508/src/PowerShellGet/private/modulefile/PartOne.ps1#L71
78
Join-Path (Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('USER_MODULES')) -Parent) 'Modules'
@@ -18,4 +19,22 @@ function Get-InstallPath {
1819
}
1920
}
2021

22+
$ModulePaths = $Env:PSModulePath -split (';:'[[int]($IsLinux -or $IsMacOS)])
23+
if ($defaultPath -in $ModulePaths) {
24+
$defaultPath
25+
} else {
26+
# default path is not in findable by get-module, try to avoid it
27+
$writablePath = ''
28+
foreach ($P1 in $ModulePaths) {
29+
if (([string]::IsNullOrEmpty($writablePath)) -and (Test-PathWritable $P1)) {
30+
$writablePath = $P1
31+
}
32+
}
33+
if ([string]::IsNullOrEmpty($writablePath)) {
34+
# we found no writable paths, return default one
35+
$defaultPath
36+
} else {
37+
$writablePath
38+
}
39+
}
2140
}

Private/Test-PathWritable.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function Test-PathWritable {
2+
param (
3+
[string]$Path
4+
)
5+
# returns true if given directory is writable, false otherwise
6+
if (!(Test-Path $Path -PathType Container)) {
7+
#throw "Path $Path is not a directory"
8+
$false
9+
}
10+
11+
$FileName = Join-Path $Path ([io.path]::GetRandomFileName())
12+
13+
try {
14+
[io.file]::OpenWrite($FileName).close()
15+
[io.file]::Delete($FileName)
16+
$true
17+
} catch {
18+
$false
19+
}
20+
21+
}

Tests/module/InstallModuleFromGit.Tests.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
$ModuleName = 'InstallModuleFromGit'
1212
$here = Split-Path -Parent $MyInvocation.MyCommand.Path # Tests/Module folder
1313
$root = (get-item $here).Parent.Parent.FullName # module root folder
14-
Import-Module (Join-Path $root "$ModuleName.psm1") -Force
14+
Import-Module (Join-Path $root "$ModuleName.psd1") -Force
1515

1616

1717
#
@@ -67,6 +67,8 @@ Describe 'Proper Functions Declaration' -Tag 'Other' {
6767

6868
Describe 'Proper Documentation' -Tag 'Documentation' {
6969

70+
Push-Location $root
71+
7072
It 'Updates documentation and does git diff' {
7173

7274
# install PlatyPS
@@ -92,6 +94,7 @@ Describe 'Proper Documentation' -Tag 'Documentation' {
9294
}
9395
}
9496

97+
Pop-Location
9598
}
9699

97100

0 commit comments

Comments
 (0)