Files
Scoop/lib/psmodules.ps1
2024-04-10 14:55:20 +08:00

55 lines
1.8 KiB
PowerShell

function install_psmodule($manifest, $dir, $global) {
$psmodule = $manifest.psmodule
if (!$psmodule) { return }
$targetdir = ensure (modulesdir $global)
ensure_in_psmodulepath $targetdir $global
$module_name = $psmodule.name
if (!$module_name) {
abort "Invalid manifest: The 'name' property is missing from 'psmodule'."
}
$linkfrom = "$targetdir\$module_name"
Write-Host "Installing PowerShell module '$module_name'"
Write-Host "Linking $(friendly_path $linkfrom) => $(friendly_path $dir)"
if (Test-Path $linkfrom) {
warn "$(friendly_path $linkfrom) already exists. It will be replaced."
Remove-Item -Path $linkfrom -Force -Recurse -ErrorAction SilentlyContinue
}
New-DirectoryJunction $linkfrom $dir | Out-Null
}
function uninstall_psmodule($manifest, $dir, $global) {
$psmodule = $manifest.psmodule
if (!$psmodule) { return }
$module_name = $psmodule.name
Write-Host "Uninstalling PowerShell module '$module_name'."
$targetdir = modulesdir $global
$linkfrom = "$targetdir\$module_name"
if (Test-Path $linkfrom) {
Write-Host "Removing $(friendly_path $linkfrom)"
$linkfrom = Convert-Path $linkfrom
Remove-Item -Path $linkfrom -Force -Recurse -ErrorAction SilentlyContinue
}
}
function ensure_in_psmodulepath($dir, $global) {
$path = Get-EnvVar -Name 'PSModulePath' -Global:$global
if (!$global -and $null -eq $path) {
$path = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules"
}
if ($path -notmatch [Regex]::Escape($dir)) {
Write-Output "Adding $(friendly_path $dir) to $(if($global){'global'}else{'your'}) PowerShell module path."
Set-EnvVar -Name 'PSModulePath' -Value "$dir;$path" -Global:$global
}
}