-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ps1
152 lines (152 loc) · 6.76 KB
/
main.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
146
147
148
149
150
151
152
#Requires -PSEdition Core -Version 7.2
$Script:ErrorActionPreference = 'Stop'
[Boolean]$IsDebugMode = $Env:RUNNER_DEBUG -ieq '1'
[RegEx]$RegExSemVerModifier = '^(?:[<>]=?|=|\^|~) *'
Function Resolve-TargetVersion {
[CmdletBinding()]
[OutputType([SemVer])]
Param (
[Parameter(Mandatory = $True, Position = 0)][AllowEmptyString()][String]$VersionModifier,
[Parameter(Mandatory = $True, Position = 1)][SemVer]$VersionNumber,
[Switch]$AllowPrerelease
)
[SemVer[]]$VersionsAvailable = Find-Module -Name 'hugoalh.GitHubActionsToolkit' -AllVersions -Repository 'PSGallery' -AllowPrerelease:($AllowPrerelease.IsPresent) -Verbose:$IsDebugMode |
ForEach-Object -Process { [SemVer]::Parse($_.Version) } |
Sort-Object -Descending
[String]$VersionsAvailableStringify = $VersionsAvailable |
ForEach-Object -Process { $_.ToString() } |
Join-String -Separator ', '
Write-Host -Object "::debug::Versions Available [$($VersionsAvailable.Count)]: $VersionsAvailableStringify"
$VersionResolve = $VersionsAvailable |
Where-Object -FilterScript {
[SemVer]$VersionAvailable = $_ # This reassign is necessary, as $_ in Switch is value of the switch!
Switch ($VersionModifier) {
'<' {
$VersionAvailable -ilt $VersionNumber |
Write-Output
Break
}
'<=' {
$VersionAvailable -ile $VersionNumber |
Write-Output
Break
}
'>=' {
$VersionAvailable -ige $VersionNumber |
Write-Output
Break
}
'>' {
$VersionAvailable -igt $VersionNumber |
Write-Output
Break
}
'^' {
$VersionAvailable -ige $VersionNumber -and $VersionAvailable -ilt [SemVer]::Parse("$($VersionNumber.Major + 1).0.0") |
Write-Output
Break
}
'~' {
$VersionAvailable -ige $VersionNumber -and $VersionAvailable -ilt [SemVer]::Parse("$($VersionNumber.Major).$($VersionNumber.Minor + 1).0") |
Write-Output
Break
}
Default {
$VersionAvailable -ieq $VersionNumber |
Write-Output
Break
}
}
} |
Sort-Object -Descending |
Select-Object -First 1
If ($Null -ieq $VersionResolve) {
throw "No available versions that fulfill the target version ``$($VersionModifier)$($VersionNumber.ToString())``! Only these versions are available: $VersionsAvailableStringify"
}
Write-Output -InputObject $VersionResolve
}
Write-Host -Object 'Initialize.'
[Boolean]$InputAllowPreRelease = [Boolean]::Parse($Env:INPUT_ALLOWPRERELEASE)
[Boolean]$InputForce = [Boolean]::Parse($Env:INPUT_FORCE)
[Boolean]$InputKeepSetting = [Boolean]::Parse($Env:INPUT_KEEPSETTING)
[String]$InputScope = $Env:INPUT_SCOPE
[String]$InputVersionRaw = $Env:INPUT_VERSION
[Boolean]$InputUninstall = $InputVersionRaw -iin @('-', 'False', 'None', 'Uninstall')
[Boolean]$InputVersionLatest = $InputVersionRaw -iin @('*', 'Latest')
If (!$InputVersionLatest -and !$InputUninstall) {
Try {
[String]$InputVersionModifier = ($InputVersionRaw -imatch $RegExSemVerModifier) ? $Matches[0].Trim() : ''
[SemVer]$InputVersionNumber = [SemVer]::Parse((($InputVersionRaw -imatch $RegExSemVerModifier) ? $InputVersionRaw -ireplace "^$([RegEx]::Escape($Matches[0]))", '' : $InputVersionRaw))
}
Catch {
Write-Host -Object "::error::``$InputVersionRaw`` is not a valid version!"
Exit 1
}
}
Write-Host -Object 'Check PowerShell package provider.'
$PSPackageProviderPowerShellGetMeta = Get-PackageProvider -Name 'PowerShellGet'
If (!($PSPackageProviderPowerShellGetMeta.Version -ge [Microsoft.PackageManagement.Internal.Utility.Versions.FourPartVersion]::Parse('2.2.5') -and $PSPackageProviderPowerShellGetMeta.Version -lt [Microsoft.PackageManagement.Internal.Utility.Versions.FourPartVersion]::Parse('3.0.0'))) {
Write-Host -Object "::error::PowerShell package provider ``PowerShellGet`` is not compatible! Expect ``^2.2.5.0``; Current ``$($PSPackageProviderPowerShellGetMeta.Version.ToString())``."
Exit 1
}
Write-Host -Object 'Check PowerShell repository.'
$PSRepositoryPSGalleryMeta = Get-PSRepository -Name 'PSGallery'
If ($Null -eq $PSRepositoryPSGalleryMeta) {
Write-Host -Object '::error::PowerShell repository `PSGallery` is missing!'
Exit 1
}
If ($PSRepositoryPSGalleryMeta.SourceLocation -ine 'https://www.powershellgallery.com/api/v2') {
Write-Host -Object '::error::PowerShell repository `PSGallery` was modified!'
Exit 1
}
If ($PSRepositoryPSGalleryMeta.InstallationPolicy -ine 'Trusted') {
Write-Host -Object 'Tweak PowerShell repository configuration.'
Set-PSRepository -Name 'PSGallery' -InstallationPolicy 'Trusted' -Verbose:$IsDebugMode
}
Write-Host -Object 'Setup toolkit.'
Try {
$ToolkitInstalledPrevious = Get-InstalledModule -Name 'hugoalh.GitHubActionsToolkit' -AllVersions -AllowPrerelease -ErrorAction 'SilentlyContinue'
If ($InputUninstall) {
$ToolkitInstalledPrevious |
ForEach-Object -Process {
Uninstall-Module -Name 'hugoalh.GitHubActionsToolkit' -RequiredVersion $_.Version -AllowPrerelease -Confirm:$False -Verbose:$IsDebugMode
}
}
Else {
[SemVer]$VersionResolve = Resolve-TargetVersion -VersionModifier ($InputVersionLatest ? '>=' : $InputVersionModifier) -VersionNumber ($InputVersionLatest ? [SemVer]::Parse('2.1.0') : $InputVersionNumber) -AllowPrerelease:$InputAllowPreRelease
$ToolkitInstalledPrevious |
Where-Object -FilterScript { $InputForce -or [SemVer]::Parse($_.Version) -ine $VersionResolve } |
ForEach-Object -Process {
Uninstall-Module -Name 'hugoalh.GitHubActionsToolkit' -RequiredVersion $_.Version -AllowPrerelease -Confirm:$False -Verbose:$IsDebugMode
}
If (
$InputForce -or
(
$ToolkitInstalledPrevious |
Where-Object -FilterScript { [SemVer]::Parse($_.Version) -ieq $VersionResolve } |
Measure-Object
).Count -eq 0
) {
Install-Module -Name 'hugoalh.GitHubActionsToolkit' -Repository 'PSGallery' -RequiredVersion $VersionResolve -Scope $InputScope -AllowPrerelease:$InputAllowPreRelease -AcceptLicense -Confirm:$False -Verbose:$IsDebugMode
}
}
}
Catch {
Write-Host -Object "::error::$($_ -ireplace '\r?\n', ' ')"
Exit 1
}
$ToolkitInstalledCurrent = Get-InstalledModule -Name 'hugoalh.GitHubActionsToolkit' -AllVersions -AllowPrerelease -ErrorAction ($InputUninstall ? 'Continue' : 'Stop')
$ToolkitInstalledCurrent |
Format-List -Property @('Version', 'PublishedDate', 'InstalledDate', 'UpdatedDate', 'Dependencies', 'PackageManagementProvider', 'InstalledLocation')
If ($Null -ne $ToolkitInstalledCurrent) {
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value @(
"path=$($ToolkitInstalledCurrent.InstalledLocation)",
"version=$($ToolkitInstalledCurrent.Version.ToString())"
) -Confirm:$False -Encoding 'UTF8NoBOM'
}
If (!$InputKeepSetting) {
If ($PSRepositoryPSGalleryMeta.InstallationPolicy -ine 'Trusted') {
Write-Host -Object 'Restore PowerShell repository configuration.'
Set-PSRepository -Name 'PSGallery' -InstallationPolicy $PSRepositoryPSGalleryMeta.InstallationPolicy -Verbose:$IsDebugMode
}
}