forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 11
/
psmodules.ps1
71 lines (54 loc) · 2.69 KB
/
psmodules.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@(
@('core', 'Test-ScoopDebugEnabled'),
@('Helpers', 'New-IssuePrompt')
) | ForEach-Object {
if (!([bool] (Get-Command $_[1] -ErrorAction 'Ignore'))) {
Write-Verbose "Import of lib '$($_[0])' initiated from '$PSCommandPath'"
. (Join-Path $PSScriptRoot "$($_[0]).ps1")
}
}
function install_psmodule($manifest, $dir, $global) {
$psmodule = $manifest.psmodule
if (!$psmodule) { return }
$moduleName = $psmodule.name
if (!$moduleName) { throw [ScoopException]::new("Invalid manifest|-The 'name' property is missing from 'psmodule'") } # TerminatingError thrown
$modules = if ($global) { $SCOOP_GLOBAL_MODULE_DIRECTORY } else { $SCOOP_MODULE_DIRECTORY }
$modules = Confirm-DirectoryExistence -LiteralPath $modules
ensure_in_psmodulepath $SCOOP_MODULE_DIRECTORY $false
if ($global) { ensure_in_psmodulepath $SCOOP_GLOBAL_MODULE_DIRECTORY $true }
$linkFrom = Join-Path $modules $moduleName
Write-UserMessage -Message "Installing PowerShell module '$moduleName'", "Linking $(friendly_path $linkFrom) => $(friendly_path $dir)"
if (Test-Path $linkFrom) {
Write-UserMessage -Message "$(friendly_path $linkFrom) already exists. It will be replaced." -Warning
$linkFrom = Resolve-Path $linkFrom # TODOOOO: Why????
Remove-DirectoryJunctionLink -LinkName $linkFrom.FullName
}
New-DirectoryJunctionLink -LinkName $linkFrom -Target $dir | Out-Null
}
function uninstall_psmodule($manifest, $dir, $global) {
$psmodule = $manifest.psmodule
if (!$psmodule) { return }
$moduleName = $psmodule.name
Write-UserMessage -Message "Uninstalling PowerShell module '$moduleName'."
$from = if ($global) { $SCOOP_GLOBAL_MODULE_DIRECTORY } else { $SCOOP_MODULE_DIRECTORY }
$linkFrom = Join-Path $from $moduleName
if (Test-Path $linkFrom) {
Write-UserMessage -Message "Removing $(friendly_path $linkFrom)"
$linkfrom = Resolve-Path $linkFrom # TODOOOO: Why????
Remove-DirectoryJunctionLink -LinkName $linkFrom.FullName
}
}
function ensure_in_psmodulepath($dir, $global) {
# TODO: Properly support unix
if ($SHOVEL_IS_UNIX) { return }
$path = env 'psmodulepath' $global
if (!$global -and ($null -eq $path)) {
# Add "default" psmodule path
$path = Join-Path $env:USERPROFILE 'Documents\WindowsPowerShell\Modules'
}
if ($path -notmatch [System.Text.RegularExpressions.Regex]::Escape($dir)) {
Write-UserMessage -Message "Adding $(friendly_path $dir) to $(if($global){'global'}else{'your'}) PowerShell module path."
env 'psmodulepath' $global "$dir;$path" # for future sessions...
$env:psmodulepath = "$dir;$env:psmodulepath" # for this session
}
}