-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathInstallModules.ps1
145 lines (132 loc) · 6.67 KB
/
InstallModules.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#Requires -PSEdition Core
#Requires -Version 7.2
Set-StrictMode -Version 3
$script:rootDir = Resolve-Path (Join-Path $PSScriptRoot ..)
$script:profileDir = Join-Path $rootDir Profile
$script:localModulesDirectory = Join-Path $rootDir Modules
$script:localAdditionalModulesDirectory = Join-Path $rootDir AdditionalModules
. "$profileDir/Functions.ps1"
function FixPSModulePath($path, $messageSuffix) {
if ($null -eq $path) { $path = '' }
if ($IsWindows -and (Get-Command scoop -ErrorAction SilentlyContinue)) {
$script:scoopDir = "$env:USERPROFILE\scoop"
if (Test-Path $scoopDir) {
$scoopModulesDir = Join-Path $scoopDir modules
if (!($path.Contains($scoopModulesDir))) {
if ($null -ne $messageSuffix) {
Write-Host "Adding Scoop modules directory '$scoopModulesDir' to PSModulePath for $messageSuffix."
}
$path = "$scoopModulesDir;$path"
}
}
}
if (!($path.Contains($localModulesDirectory))) {
if ($null -ne $messageSuffix) {
Write-Host "Adding modules directory '$localModulesDirectory' to PSModulePath for $messageSuffix."
}
$path = "$localModulesDirectory$([System.IO.Path]::PathSeparator)$path"
}
if (!($path.Contains($localAdditionalModulesDirectory))) {
if ($null -ne $messageSuffix) {
Write-Host "Adding modules directory '$localAdditionalModulesDirectory' to PSModulePath for $messageSuffix."
}
$path = "$localAdditionalModulesDirectory$([System.IO.Path]::PathSeparator)$path"
}
if ($path.IndexOf($localModulesDirectory) -lt ($path.IndexOf($localAdditionalModulesDirectory))) {
if ($null -ne $messageSuffix) {
Write-Host "Fixing the order of PSModulePath for $messageSuffix."
}
$path = $path.Replace("$localModulesDirectory$([System.IO.Path]::PathSeparator)", "")
$path = $path.Replace("$localAdditionalModulesDirectory", "")
$path = $path.Replace("$([System.IO.Path]::PathSeparator)$([System.IO.Path]::PathSeparator)", [System.IO.Path]::PathSeparator)
if ($path.StartsWith($([System.IO.Path]::PathSeparator))) {
$path = $path.Substring(1)
}
$path = "$localAdditionalModulesDirectory$([System.IO.Path]::PathSeparator)$localModulesDirectory$([System.IO.Path]::PathSeparator)$path"
}
return $path
}
function FixJsonConfigFile {
# this is for PowerShell Core which is be done via config file
# see: help about_powershell_config
# https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_powershell_config
return # todo: remove this when the startup bug is fixed: https://github.com/PowerShell/PowerShell/issues/20706
$pwshUserDir = Split-Path $PROFILE.CurrentUserCurrentHost
if (!(Test-Path $pwshUserDir)) { New-Item -ItemType Directory -Path $pwshUserDir }
$pwshConfigFile = Join-Path $pwshUserDir powershell.config.json
if (!(Test-Path $pwshConfigFile)) { Set-Content $pwshConfigFile -Value '{}' }
$pwshConfig = Get-Content -Raw $pwshConfigFile | ConvertFrom-Json -AsHashtable
$oldPsModulePathForConfigFile = $pwshConfig['PSModulePath']
$newPSModulePathForConfigFile = FixPSModulePath $oldPsModulePathForConfigFile "config file '$pwshConfigFile'"
if ($oldPsModulePathForConfigFile -ne $newPSModulePathForConfigFile) {
Write-Host "Setting PSModulePath in config file '$pwshConfigFile'."
$pwshConfig.PSModulePath = $newPSModulePathForConfigFile
$pwshConfigText = ConvertTo-Json $pwshConfig
Set-Content $pwshConfigFile $pwshConfigText
}
}
if ($IsWindows) {
# this is for Windows PowerShell, see PowerShell Core bellow
# also see: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_psmodulepath#powershell-psmodulepath-construction
$oldUserPSModulePathForRegistry = [Environment]::GetEnvironmentVariable('PSModulePath', 'User')
$newUserPSModulePathForRegistry = FixPSModulePath $oldUserPSModulePathForRegistry "USER scope on Registry"
if ($oldUserPSModulePathForRegistry -ne $newUserPSModulePathForRegistry) {
Write-Output "Setting USER scope PSModulePath on Registry."
[Environment]::SetEnvironmentVariable('PSModulePath', $newUserPSModulePathForRegistry, 'User')
}
FixJsonConfigFile
} elseif ($IsLinux) {
FixJsonConfigFile
} elseif ($IsMacOS) {
FixJsonConfigFile
} else {
Write-Warning "PSModule setup is not implemented for this platform '$([System.Environment]::OSVersion.Platform)' (send a PR!)"
$false
}
function ModuleMissing([Parameter(Mandatory = $true)][string]$moduleName, [System.Version]$minimumVersion) {
if ($null -eq $minimumVersion) {
(([array]($env:PSModulePath.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries) | `
ForEach-Object { Join-Path $_ $moduleName } | `
Where-Object { Test-Path $_ }
)) ?? @()).Count -eq 0
} else {
(([array]($env:PSModulePath.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries) | `
ForEach-Object { Join-Path $_ $moduleName } | `
Where-Object { Test-Path $_ } | `
Where-Object { (Get-Module -ListAvailable $_).Version -ge $minimumVersion } `
)) ?? @()).Count -eq 0
}
}
if (!(Test-Path (Join-Path $localModulesDirectory PowerShellGet))) {
Save-Module -Name PowerShellGet -Path $localModulesDirectory -Confirm:$false
}
if (!(Test-Path (Join-Path $localModulesDirectory psake))) {
Save-Module -Name psake -Path $localModulesDirectory -Confirm:$false
}
$psakeTabExpansionFile = Join-Path $localModulesDirectory psake PsakeTabExpansion.ps1
if (!(Test-Path $psakeTabExpansionFile)) {
Invoke-WebRequest -Uri https://github.com/psake/psake/raw/master/tabexpansion/PsakeTabExpansion.ps1 -OutFile $psakeTabExpansionFile
}
if (ModuleMissing VSSetup) {
Save-Module VSSetup $localModulesDirectory -Confirm:$false
}
if (ModuleMissing Terminal-Icons) {
Save-Module Terminal-Icons $localModulesDirectory -Confirm:$false
}
if (ModuleMissing Pester '5.0.0') {
Save-Module Pester $localModulesDirectory -Confirm:$false
}
if (ModuleMissing PSScriptAnalyzer) {
Save-Module PSScriptAnalyzer $localModulesDirectory -Confirm:$false
}
if (ModuleMissing PSReadLine '2.2.6') {
Save-Module PSReadLine $localModulesDirectory -Confirm:$false
}
if (ModuleMissing Microsoft.Graph) {
Save-Module Microsoft.Graph $localModulesDirectory -Confirm:$false
}
Remove-Item -Path Function:\FixPSModulePath
if ($IsWindows) {
powershell.exe -NoProfile -File $PSScriptRoot\InstallModules-Windows.ps1
Test-Error
}