-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #746 from aaronparker/development
Development
- Loading branch information
Showing
24 changed files
with
459 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Function Get-Npcap { | ||
function Get-Npcap { | ||
<# | ||
.SYNOPSIS | ||
Returns the latest Npcap version number and download. | ||
|
@@ -8,34 +8,26 @@ Function Get-Npcap { | |
E-mail: [email protected] | ||
#> | ||
[OutputType([System.Management.Automation.PSObject])] | ||
[CmdletBinding(SupportsShouldProcess = $False)] | ||
[CmdletBinding(SupportsShouldProcess = $false)] | ||
param ( | ||
[Parameter(Mandatory = $False, Position = 0)] | ||
[Parameter(Mandatory = $false, Position = 0)] | ||
[ValidateNotNull()] | ||
[System.Management.Automation.PSObject] | ||
$res = (Get-FunctionResource -AppName ("$($MyInvocation.MyCommand)".Split("-"))[1]) | ||
) | ||
|
||
# Get the latest version via the latest tag on the repository | ||
$Tags = Get-GitHubRepoTag -Uri $res.Get.Update.Uri | ||
|
||
# Get latest version and download latest release via GitHub API | ||
$params = @{ | ||
Uri = $res.Get.Update.Uri | ||
ContentType = $res.Get.Update.ContentType | ||
ReturnObject = "Content" | ||
} | ||
|
||
# Get only latest version tag from GitHub API | ||
$Content = ((Invoke-EvergreenWebRequest @params | ConvertFrom-Json).name -replace "v",""| ForEach-Object { New-Object -TypeName "System.Version" ($_) } | Sort-Object -Descending | Select-Object -First 1 | ForEach-Object {("{0}.{1}" -f $_.Major,$_.Minor)}) | ||
# Select the latest version | ||
$Version = $Tags | Sort-Object -Property @{ Expression = { [System.Version]$_.Tag }; Descending = $true } | Select-Object -First 1 | ||
|
||
if ($null -ne $Content) { | ||
$Content | ForEach-Object { | ||
$PSObject = [PSCustomObject] @{ | ||
Version = $_ | ||
Type = "exe" | ||
URI = $res.Get.Download.Uri -replace $res.Get.Download.ReplaceText, $_ | ||
} | ||
Write-Output -InputObject $PSObject | ||
} | ||
# Output the version and download URL | ||
$PSObject = [PSCustomObject] @{ | ||
Version = $Version.Tag | ||
Type = Get-FileType -File $res.Get.Download.Uri | ||
URI = $res.Get.Download.Uri -replace $res.Get.Download.ReplaceText, $Version.Tag | ||
} | ||
} | ||
Write-Output -InputObject $PSObject | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function Convert-Segment { | ||
param ( | ||
[System.String] $Segment | ||
) | ||
|
||
if ($Segment -match '^\d+$') { | ||
return [System.Int32]$Segment | ||
} | ||
else { | ||
$Normalized = 0 | ||
foreach ($Char in $Segment.ToCharArray()) { | ||
$Normalized = $Normalized * 100 + [System.Int32][System.Char]$Char | ||
} | ||
return $Normalized | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
function ConvertTo-DotNetVersionClass { | ||
<# | ||
.EXTERNALHELP Evergreen-help.xml | ||
#> | ||
param ( | ||
[Parameter( | ||
Mandatory = $true, | ||
Position = 0, | ||
ValueFromPipeline, | ||
ValueFromPipelineByPropertyName, | ||
HelpMessage = "A version string to convert to a standard .NET compliant version class.")] | ||
[System.String] $Version | ||
) | ||
|
||
process { | ||
# Split the version string into segments and initialise an array | ||
$Segments = $Version -split '[.\-_+]' | ||
$NormalizedSegments = @() | ||
|
||
# Normalize each segment | ||
foreach ($Segment in $Segments) { | ||
$NormalizedSegments += @(Convert-Segment -Segment $Segment) | ||
} | ||
|
||
# If the number of segments is greater than 4, sum the last segments | ||
if ($NormalizedSegments.Count -gt 4) { | ||
$NormalizedSegments = $NormalizedSegments[0..2] + ($NormalizedSegments[3..($NormalizedSegments.Count - 1)] | Measure-Object -Sum).Sum | ||
} | ||
|
||
# If the number of segments is less than 4, pad with zeros | ||
while ($NormalizedSegments.Count -lt 4) { | ||
$NormalizedSegments += 0 | ||
} | ||
|
||
# Return the version as a .NET Version class | ||
try { | ||
return [System.Version]($NormalizedSegments -join ".") | ||
} | ||
catch { | ||
Write-Warning -Message "Failed to convert version string '$Version' to a .NET Version class." | ||
return ($NormalizedSegments -join ".") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
function Get-GitHubRepoTag { | ||
<# | ||
.SYNOPSIS | ||
Calls the GitHub Tags API passed via $Uri and returns the tags for the repository | ||
Example: https://api.github.com/repos/PowerShell/PowerShell/tags | ||
#> | ||
[OutputType([System.Management.Automation.PSObject])] | ||
[CmdletBinding(SupportsShouldProcess = $false)] | ||
param ( | ||
[Parameter(Mandatory = $true, Position = 0)] | ||
[ValidateScript( { | ||
if ($_ -match "^(https://api\.github\.com/repos/)([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)(/tags)") { | ||
$true | ||
} | ||
else { | ||
throw "'$_' must be in the format 'https://api.github.com/repos/user/tags'. Replace 'user' with the user or organisation and 'repository' with the target repository name." | ||
} | ||
})] | ||
[System.String] $Uri, | ||
|
||
[Parameter(Mandatory = $false, Position = 1)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.String] $MatchVersion = "(\d+(\.\d+){1,4}).*" | ||
) | ||
|
||
begin { | ||
$RateLimit = Get-GitHubRateLimit | ||
} | ||
|
||
process { | ||
if ($RateLimit.remaining -eq 0) { | ||
# We're rate limited, so output a special object | ||
[PSCustomObject] @{ | ||
Version = "RateLimited" | ||
URI = "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting" | ||
} | Write-Output | ||
} | ||
else { | ||
try { | ||
# Retrieve the releases from the GitHub API | ||
# Use TLS for connections | ||
Write-Verbose -Message "$($MyInvocation.MyCommand): Set TLS to 1.2." | ||
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072 | ||
|
||
# Invoke the GitHub releases REST API | ||
# Note that the API performs rate limiting. | ||
# https://docs.github.com/en/free-pro-team@latest/rest/reference/repos#get-the-latest-release | ||
$params = @{ | ||
ContentType = "application/vnd.github.v3+json" | ||
ErrorAction = "Stop" | ||
MaximumRedirection = 0 | ||
DisableKeepAlive = $true | ||
UseBasicParsing = $true | ||
UserAgent = "github-aaronparker-evergreen" | ||
Uri = $Uri | ||
} | ||
if (Test-ProxyEnv) { | ||
$params.Proxy = $script:EvergreenProxy | ||
} | ||
if (Test-ProxyEnv -Creds) { | ||
$params.ProxyCredential = $script:EvergreenProxyCreds | ||
} | ||
# If GITHUB_TOKEN or GH_TOKEN exists, let's add that to the API request | ||
if (Test-Path -Path "env:GITHUB_TOKEN") { | ||
$params.Headers = @{ Authorization = "token $env:GITHUB_TOKEN" } | ||
} | ||
elseif (Test-Path -Path "env:GH_TOKEN") { | ||
$params.Headers = @{ Authorization = "token $env:GH_TOKEN" } | ||
} | ||
|
||
# Output the parameters when using -Verbose | ||
foreach ($tag in $params.GetEnumerator()) { | ||
Write-Verbose -Message "$($MyInvocation.MyCommand): Invoke-WebRequest parameter: $($tag.name): $($tag.value)." | ||
} | ||
|
||
Write-Verbose -Message "$($MyInvocation.MyCommand): Get GitHub release from: $Uri." | ||
$Tags = Invoke-RestMethod @params | ||
} | ||
catch { | ||
throw $_ | ||
} | ||
|
||
if ($null -eq $script:resourceStrings.Properties.GitHubTags) { | ||
Write-Warning -Message "$($MyInvocation.MyCommand): Unable to validate tag against GitHub tags property object because we can't find the module resource." | ||
} | ||
else { | ||
# Validate that $tags has the expected properties | ||
Write-Verbose -Message "$($MyInvocation.MyCommand): Validating GitHub tag object." | ||
foreach ($tag in $Tags) { | ||
|
||
# Compare the GitHub release object with properties that we expect | ||
$params = @{ | ||
ReferenceObject = $script:resourceStrings.Properties.GitHubTags | ||
DifferenceObject = (Get-Member -InputObject $tag -MemberType NoteProperty) | ||
PassThru = $true | ||
ErrorAction = "Continue" | ||
} | ||
$missingProperties = Compare-Object @params | ||
|
||
# Throw an error for missing properties | ||
if ($null -ne $missingProperties) { | ||
Write-Verbose -Message "$($MyInvocation.MyCommand): Validated tag object successfully." | ||
} | ||
else { | ||
Write-Verbose -Message "$($MyInvocation.MyCommand): Validation failed." | ||
$missingProperties | ForEach-Object { | ||
throw [System.Management.Automation.ValidationMetadataException]::New("$($MyInvocation.MyCommand): Property: '$_' missing") | ||
} | ||
} | ||
} | ||
} | ||
|
||
# Build and array of the latest release and download URLs | ||
Write-Verbose -Message "$($MyInvocation.MyCommand): Found $($tags.count) tags." | ||
foreach ($Tag in $Tags) { | ||
|
||
try { | ||
# Uri matches tags for the repo; find the latest tag | ||
$Version = [RegEx]::Match($Tag.name, $MatchVersion).Captures.Groups[1].Value | ||
} | ||
catch { | ||
Write-Verbose -Message "$($MyInvocation.MyCommand): Failed to match version number, returning as-is: $($Tag.name)." | ||
$Version = $Tag.name | ||
} | ||
|
||
# Output the tags object | ||
[PSCustomObject]@{ | ||
Tag = $Version | ||
} | Write-Output | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.