-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.ps1
More file actions
172 lines (159 loc) · 7.49 KB
/
Copy pathinit.ps1
File metadata and controls
172 lines (159 loc) · 7.49 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
[CmdletBinding()]
param()
begin {
$scriptName = $MyInvocation.MyCommand.Name
Write-Debug "[$scriptName] - Start"
}
process {
try {
$env:PSMODULE_GITHUB_SCRIPT = $true
$fenceTitle = $env:PSMODULE_GITHUB_SCRIPT_INPUT_Name
$showInit = $env:PSMODULE_GITHUB_SCRIPT_INPUT_ShowInit -eq 'true'
Write-Debug "[$scriptName] - ShowInit: $env:PSMODULE_GITHUB_SCRIPT_INPUT_ShowInit"
if ($showInit) {
$fenceStart = "┏━━┫ $fenceTitle - Init ┣━━━━━━━━┓"
Write-Output $fenceStart
Write-Output '::group:: - Install GitHub PowerShell module'
}
$Name = 'GitHub'
$Version = [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_Version) ? $null : $env:PSMODULE_GITHUB_SCRIPT_INPUT_Version
$Prerelease = $env:PSMODULE_GITHUB_SCRIPT_INPUT_Prerelease -eq 'true'
$installedParams = @{
Name = $Name
ErrorAction = 'SilentlyContinue'
}
if ($Version) {
# Version accepts an exact version or a NuGet version range. Let PSResourceGet resolve
# range satisfaction instead of comparing the raw value with an exact string match.
Write-Verbose "Filtering by version: $Version"
$installedParams['Version'] = $Version
}
$alreadyInstalled = Get-InstalledPSResource @installedParams
if ($showInit) {
Write-Output 'Already installed:'
$alreadyInstalled | Format-List | Out-String
}
if (-not $alreadyInstalled) {
$params = @{
Name = $Name
Repository = 'PSGallery'
TrustRepository = $true
Prerelease = $Prerelease
Reinstall = $true
WarningAction = 'SilentlyContinue'
}
if ($Version) {
$params['Version'] = $Version
}
$Count = 5
$Delay = 10
for ($i = 1; $i -le $Count; $i++) {
try {
Install-PSResource @params -ErrorAction Stop
break
} catch {
Write-Warning $_.Exception.Message
if ($i -eq $Count) {
throw $_
}
Start-Sleep -Seconds $Delay
}
}
}
# Resolve the exact installed version that satisfies the request (newest match), so the loaded
# module is deterministic instead of whatever PowerShell would auto-load from PSModulePath. When
# prerelease versions are not requested, never resolve to one; when a stable and a prerelease share
# a base version, the stable one wins. PSResourceGet exposes prerelease state as the 'Prerelease'
# string (empty for stable).
$resolveParams = @{
Name = $Name
ErrorAction = 'SilentlyContinue'
}
if ($Version) {
$resolveParams['Version'] = $Version
}
$candidates = @(Get-InstalledPSResource @resolveParams)
if (-not $Prerelease) {
$candidates = @($candidates | Where-Object { [string]::IsNullOrWhiteSpace($_.Prerelease) })
}
$resolved = $candidates | Sort-Object -Property @(
@{ Expression = 'Version'; Descending = $true }
@{ Expression = { -not [string]::IsNullOrWhiteSpace($_.Prerelease) }; Descending = $false }
) | Select-Object -First 1
if (-not $resolved) {
$requested = [string]::IsNullOrWhiteSpace($Version) ? 'latest' : $Version
throw "No installed '$Name' version satisfies the requested version '$requested'."
}
$alreadyImported = Get-Module -Name $Name
if ($showInit) {
Write-Output 'Already imported:'
$alreadyImported | Format-List | Out-String
}
# Remove every already-loaded instance (all versions, including nested) so only the chosen version
# remains loaded, then import that exact version into the global session state so every subsequent
# command (info.ps1, the user script, clean.ps1) uses the selected version.
Get-Module -Name $Name -All | Remove-Module -Force -ErrorAction SilentlyContinue
Write-Verbose "Importing module: $Name $($resolved.Version)"
Import-Module -Name $Name -RequiredVersion $resolved.Version -Force -Global -ErrorAction Stop
$providedToken = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_Token)
$providedClientID = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_ClientID)
$providedPrivateKey = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_PrivateKey)
$providedKeyVaultKeyReference = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_KeyVaultKeyReference)
# Validate mutual exclusion of PrivateKey and KeyVaultKeyReference
if ($providedPrivateKey -and $providedKeyVaultKeyReference) {
throw 'Only one of PrivateKey or KeyVaultKeyReference can be provided.'
}
# Validate that if ClientID is provided, exactly one of PrivateKey or KeyVaultKeyReference is also provided
if ($providedClientID -and -not ($providedPrivateKey -or $providedKeyVaultKeyReference)) {
throw 'When ClientID is provided, either PrivateKey or KeyVaultKeyReference must also be provided.'
}
$moduleStatus = [pscustomobject]@{
Name = $Name
Version = [string]::IsNullOrEmpty($Version) ? 'latest' : $Version
Prerelease = $Prerelease
'Already installed' = $null -ne $alreadyInstalled
'Already imported' = $null -ne $alreadyImported
'Provided Token' = $providedToken
'Provided ClientID' = $providedClientID
'Provided PrivateKey' = $providedPrivateKey
'Provided KeyVaultKeyReference' = $providedKeyVaultKeyReference
}
if ($showInit) {
Write-Output 'Module status:'
$moduleStatus | Format-List | Out-String
Write-Output '::endgroup::'
Write-Output '::group:: - Connect to GitHub'
}
if ($providedClientID -and $providedPrivateKey) {
$params = @{
ClientID = $env:PSMODULE_GITHUB_SCRIPT_INPUT_ClientID
PrivateKey = $env:PSMODULE_GITHUB_SCRIPT_INPUT_PrivateKey
Silent = (-not $showInit)
}
Connect-GitHub @params
} elseif ($providedClientID -and $providedKeyVaultKeyReference) {
$params = @{
ClientID = $env:PSMODULE_GITHUB_SCRIPT_INPUT_ClientID
KeyVaultKeyReference = $env:PSMODULE_GITHUB_SCRIPT_INPUT_KeyVaultKeyReference
Silent = (-not $showInit)
}
Connect-GitHub @params
} elseif ($providedToken) {
$params = @{
Token = $env:PSMODULE_GITHUB_SCRIPT_INPUT_Token
Silent = (-not $showInit)
}
Connect-GitHub @params
}
if ($showInit) {
Write-Output '::endgroup::'
$fenceEnd = '┗' + ('━' * ($fenceStart.Length - 2)) + '┛'
Write-Output $fenceEnd
}
} catch {
throw $_
}
}
end {
Write-Debug "[$scriptName] - End"
}