Skip to content

Commit aadddd4

Browse files
committed
Rework installation to go to Common Files.
Also added logic to add the custom module location to the PSModulePath. On uninstall, the path entry is removed iff the module location ends up as being empty after we remove our module. This makes us play nice with other packages that want to also install in this Common Files location.
1 parent 4a9156d commit aadddd4

File tree

4 files changed

+90
-25
lines changed

4 files changed

+90
-25
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
param(
2+
[Parameter(Mandatory=$TRUE)]
3+
[String] $PathToModuleFiles,
4+
[Parameter(Mandatory=$TRUE)]
5+
[String] $ModuleName
6+
)
7+
8+
if (!(Test-Path -PathType Container $PathToModuleFiles)) {
9+
Write-Error "$PathToModuleFiles not found or is not a directory. Nothing to install."
10+
exit
11+
}
12+
13+
$moduleRoot = "$($env:CommonProgramW6432)\Modules\"
14+
$dest = "$moduleRoot\$ModuleName\"
15+
16+
if (!(Test-Path $moduleRoot)) {
17+
New-Item -ItemType directory $moduleRoot | out-null
18+
}
19+
20+
Write-Host -NoNewLine "Copying $ModuleName module to $moduleRoot... "
21+
22+
Copy-Item -Force -Recurse $PathToModuleFiles $dest
23+
24+
Write-Host -ForegroundColor Green "done"
25+
26+
#Update PSModulePath if it needs it
27+
$path = [Environment]::GetEnvironmentVariable("PSModulePath")
28+
29+
$pathContainsRoot = 0 -ne ($path.Split(';') | ? { Test-Path $_ } | ? { (Resolve-Path $_).Path -eq (Resolve-Path $moduleRoot).Path}).count
30+
31+
if(-Not $pathContainsRoot) {
32+
33+
Write-Host -NoNewLine "Adding $moduleRoot to `$env:PSModulePath... "
34+
35+
$path += ";$moduleRoot"
36+
[Environment]::SetEnvironmentVariable("PSModulePath", $path, "Machine")
37+
38+
Write-Host -ForegroundColor Green "done"
39+
}
40+
41+
Write-Host "Installation complete."
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
param(
2+
[Parameter(Mandatory=$TRUE)]
3+
[String] $ModuleName
4+
)
5+
6+
$moduleRoot = "$($env:CommonProgramW6432)\Modules\"
7+
$dir = "$moduleRoot\$ModuleName\"
8+
9+
if (Test-Path $dir) {
10+
Write-Host -NoNewLine "Removing $ModuleName module from $moduleRoot... "
11+
12+
Remove-Item -Recurse -Force $dir
13+
14+
Write-Host -ForegroundColor Green "done"
15+
}
16+
17+
if (Test-Path $moduleRoot) {
18+
if (!(Get-ChildItem -Recurse -Force $moduleRoot)) {
19+
Write-Host "Module directory empty."
20+
21+
Write-Host -NoNewLine "Removing module directory from `$env:PSModulePath... "
22+
23+
$path = [Environment]::GetEnvironmentVariable("PSModulePath")
24+
25+
#Remove match paths but be careful about handling paths that don't
26+
#exist. We don't want to accidentally remove a path that we didn't
27+
#add.
28+
$cleanedPath = $path.Split(';') |
29+
Where-Object {
30+
(-Not (Test-Path $_ )) -or
31+
((Resolve-Path $_).Path -ne (Resolve-Path $moduleRoot).Path) } |
32+
Select-Object -Unique
33+
$cleanedPath = $cleanedPath -Join ';'
34+
35+
[Environment]::SetEnvironmentVariable("PSModulePath", $cleanedPath, "Machine")
36+
37+
Write-Host -ForegroundColor Green "done"
38+
39+
40+
Write-Host -NoNewLine "Removing empty module directory... "
41+
Remove-Item $moduleRoot
42+
Write-Host -ForegroundColor Green "done"
43+
44+
}
45+
46+
Write-Host "Removal complete."
47+
}

ChocolateyPackage/tools/chocolateyinstall.ps1

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,9 @@ $packageName = "PSReadLine"
22

33
try {
44
$source = Resolve-Path ($PSScriptRoot + "\..\$packageName\")
5-
$dest64 = "C:\Windows\system32\WindowsPowerShell\v1.0\Modules\"
6-
$dest32 = "C:\Windows\sysWOW64\WindowsPowerShell\v1.0\Modules\"
75

8-
$command = ""
6+
Start-ChocolateyProcessAsAdmin "$PSScriptRoot\Install-Module.ps1 '$source' '$packageName'"
97

10-
if (Test-Path -PathType Container $dest64) {
11-
$command = $command + "Copy-Item -Force -Recurse `'$source`' `'$dest64`';"
12-
}
13-
14-
if (Test-Path -PathType Container $dest32) {
15-
$command = $command + "Copy-Item -Force -Recurse `'$source`' `'$dest32`'"
16-
}
17-
18-
Start-ChocolateyProcessAsAdmin $command
198

209
Write-ChocolateySuccess $packageName
2110
} catch {

ChocolateyPackage/tools/chocolateyuninstall.ps1

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
$packageName = "PSReadLine"
22

33
try {
4-
$dir64 = "C:\Windows\system32\WindowsPowerShell\v1.0\Modules\$packageName\"
5-
$dir32 = "C:\Windows\sysWOW64\WindowsPowerShell\v1.0\Modules\$packageName\"
64

7-
$command = ""
8-
9-
if (Test-Path -PathType Container $dir64) {
10-
$command = $command + "Remove-Item -Recurse -Force `'$dir64`';"
11-
}
12-
13-
if (Test-Path -PathType Container $dir32) {
14-
$command = $command + "Remove-Item -Recurse -Force `'$dir32`'"
15-
}
16-
17-
Start-ChocolateyProcessAsAdmin $command
5+
Start-ChocolateyProcessAsAdmin "$PSScriptRoot\Remove-Module.ps1 '$packageName'"
186

197
Write-ChocolateySuccess $packageName
208
} catch {

0 commit comments

Comments
 (0)