From 6aa82adc9af4afae86b2834dd61b5e5f933dbeec Mon Sep 17 00:00:00 2001 From: Aaron Parker Date: Thu, 21 Sep 2023 09:18:45 +1000 Subject: [PATCH] Optimise code for Opera apps #487 --- Evergreen/Apps/Get-OperaBrowser.ps1 | 32 +++------------ Evergreen/Apps/Get-OperaCryptoBrowser.ps1 | 32 +++------------ Evergreen/Apps/Get-OperaGXBrowser.ps1 | 32 +++------------ Evergreen/Manifests/OperaBrowser.json | 3 +- Evergreen/Manifests/OperaCryptoBrowser.json | 3 +- Evergreen/Manifests/OperaGXBrowser.json | 3 +- Evergreen/Shared/Get-OperaApp.ps1 | 45 +++++++++++++++++++++ 7 files changed, 63 insertions(+), 87 deletions(-) create mode 100644 Evergreen/Shared/Get-OperaApp.ps1 diff --git a/Evergreen/Apps/Get-OperaBrowser.ps1 b/Evergreen/Apps/Get-OperaBrowser.ps1 index 605b5f3e..ce5e63e7 100644 --- a/Evergreen/Apps/Get-OperaBrowser.ps1 +++ b/Evergreen/Apps/Get-OperaBrowser.ps1 @@ -8,36 +8,14 @@ function Get-OperaBrowser { Twitter: @stealthpuppy #> [OutputType([System.Management.Automation.PSObject])] - [CmdletBinding(SupportsShouldProcess = $False)] + [CmdletBinding(SupportsShouldProcess = $false)] param ( - [Parameter(Mandatory = $False, Position = 0)] - [ValidateNotNull()] + [Parameter(Mandatory = $false, Position = 0)] + [ValidateNotNullOrEmpty()] [System.Management.Automation.PSObject] $res = (Get-FunctionResource -AppName ("$($MyInvocation.MyCommand)".Split("-"))[1]) ) - foreach ($Channel in $res.Get.Update.Channels) { - $Update = Invoke-RestMethodWrapper -Uri $res.Get.Update.Uri[$Channel] - - if ($Null -ne $Update.($res.Get.Update.Property)) { - Write-Verbose -Message "$($MyInvocation.MyCommand): checking property: $($res.Get.Update.Property)." - Write-Verbose -Message "$($MyInvocation.MyCommand): found version: $($Update.($res.Get.Update.Property))" - - # Step through each installer type - foreach ($Architecture in $res.Get.Download.Architectures) { - - # Build the output object; Output object to the pipeline - $Url = $res.Get.Download.Uri[$Channel] -replace $res.Get.Download.ReplaceText, $Update.($res.Get.Update.Property) ` - -replace "#architecture", $res.Get.Download.Architecture[$Architecture] - $PSObject = [PSCustomObject] @{ - Version = $Update.($res.Get.Update.Property) - Channel = $Channel - Architecture = $Architecture - Type = Get-FileType -File $Url - URI = $Url - } - Write-Output -InputObject $PSObject - } - } - } + $Output = Get-OperaApp -res $res + Write-Output -InputObject $Output } diff --git a/Evergreen/Apps/Get-OperaCryptoBrowser.ps1 b/Evergreen/Apps/Get-OperaCryptoBrowser.ps1 index 2671882f..ec7ab90f 100644 --- a/Evergreen/Apps/Get-OperaCryptoBrowser.ps1 +++ b/Evergreen/Apps/Get-OperaCryptoBrowser.ps1 @@ -8,36 +8,14 @@ function Get-OperaCryptoBrowser { Twitter: @stealthpuppy #> [OutputType([System.Management.Automation.PSObject])] - [CmdletBinding(SupportsShouldProcess = $False)] + [CmdletBinding(SupportsShouldProcess = $false)] param ( - [Parameter(Mandatory = $False, Position = 0)] - [ValidateNotNull()] + [Parameter(Mandatory = $false, Position = 0)] + [ValidateNotNullOrEmpty()] [System.Management.Automation.PSObject] $res = (Get-FunctionResource -AppName ("$($MyInvocation.MyCommand)".Split("-"))[1]) ) - foreach ($Channel in $res.Get.Update.Channels) { - $Update = Invoke-RestMethodWrapper -Uri $res.Get.Update.Uri[$Channel] - - if ($Null -ne $Update.($res.Get.Update.Property)) { - Write-Verbose -Message "$($MyInvocation.MyCommand): checking property: $($res.Get.Update.Property)." - Write-Verbose -Message "$($MyInvocation.MyCommand): found version: $($Update.($res.Get.Update.Property))" - - # Step through each installer type - foreach ($Architecture in $res.Get.Download.Architectures) { - - # Build the output object; Output object to the pipeline - $Url = $res.Get.Download.Uri[$Channel] -replace $res.Get.Download.ReplaceText, $Update.($res.Get.Update.Property) ` - -replace "#architecture", $res.Get.Download.Architecture[$Architecture] - $PSObject = [PSCustomObject] @{ - Version = $Update.($res.Get.Update.Property) - Channel = $Channel - Architecture = $Architecture - Type = Get-FileType -File $Url - URI = $Url - } - Write-Output -InputObject $PSObject - } - } - } + $Output = Get-OperaApp -res $res + Write-Output -InputObject $Output } diff --git a/Evergreen/Apps/Get-OperaGXBrowser.ps1 b/Evergreen/Apps/Get-OperaGXBrowser.ps1 index cb6c46df..2cae95ec 100644 --- a/Evergreen/Apps/Get-OperaGXBrowser.ps1 +++ b/Evergreen/Apps/Get-OperaGXBrowser.ps1 @@ -8,36 +8,14 @@ function Get-OperaGXBrowser { Twitter: @stealthpuppy #> [OutputType([System.Management.Automation.PSObject])] - [CmdletBinding(SupportsShouldProcess = $False)] + [CmdletBinding(SupportsShouldProcess = $false)] param ( - [Parameter(Mandatory = $False, Position = 0)] - [ValidateNotNull()] + [Parameter(Mandatory = $false, Position = 0)] + [ValidateNotNullOrEmpty()] [System.Management.Automation.PSObject] $res = (Get-FunctionResource -AppName ("$($MyInvocation.MyCommand)".Split("-"))[1]) ) - foreach ($Channel in $res.Get.Update.Channels) { - $Update = Invoke-RestMethodWrapper -Uri $res.Get.Update.Uri[$Channel] - - if ($Null -ne $Update.($res.Get.Update.Property)) { - Write-Verbose -Message "$($MyInvocation.MyCommand): checking property: $($res.Get.Update.Property)." - Write-Verbose -Message "$($MyInvocation.MyCommand): found version: $($Update.($res.Get.Update.Property))" - - # Step through each installer type - foreach ($Architecture in $res.Get.Download.Architectures) { - - # Build the output object; Output object to the pipeline - $Url = $res.Get.Download.Uri[$Channel] -replace $res.Get.Download.ReplaceText, $Update.($res.Get.Update.Property) ` - -replace "#architecture", $res.Get.Download.Architecture[$Architecture] - $PSObject = [PSCustomObject] @{ - Version = $Update.($res.Get.Update.Property) - Channel = $Channel - Architecture = $Architecture - Type = Get-FileType -File $Url - URI = $Url - } - Write-Output -InputObject $PSObject - } - } - } + $Output = Get-OperaApp -res $res + Write-Output -InputObject $Output } diff --git a/Evergreen/Manifests/OperaBrowser.json b/Evergreen/Manifests/OperaBrowser.json index aa01cb88..8132bfe5 100644 --- a/Evergreen/Manifests/OperaBrowser.json +++ b/Evergreen/Manifests/OperaBrowser.json @@ -28,8 +28,7 @@ "Architectures": [ "x64", "x86" - ], - "ReplaceText": "#version" + ] } }, "Install": { diff --git a/Evergreen/Manifests/OperaCryptoBrowser.json b/Evergreen/Manifests/OperaCryptoBrowser.json index 982bdd99..1456c8be 100644 --- a/Evergreen/Manifests/OperaCryptoBrowser.json +++ b/Evergreen/Manifests/OperaCryptoBrowser.json @@ -25,8 +25,7 @@ }, "Architectures": [ "x64" - ], - "ReplaceText": "#version" + ] } }, "Install": { diff --git a/Evergreen/Manifests/OperaGXBrowser.json b/Evergreen/Manifests/OperaGXBrowser.json index de391d14..2a55d7f0 100644 --- a/Evergreen/Manifests/OperaGXBrowser.json +++ b/Evergreen/Manifests/OperaGXBrowser.json @@ -26,8 +26,7 @@ "Architectures": [ "x64", "x86" - ], - "ReplaceText": "#version" + ] } }, "Install": { diff --git a/Evergreen/Shared/Get-OperaApp.ps1 b/Evergreen/Shared/Get-OperaApp.ps1 new file mode 100644 index 00000000..ebc941ee --- /dev/null +++ b/Evergreen/Shared/Get-OperaApp.ps1 @@ -0,0 +1,45 @@ +function Get-OperaApp { + <# + .SYNOPSIS + Returns the available version and URIs for Opera apps + + .NOTES + Author: Aaron Parker + Twitter: @stealthpuppy + #> + [OutputType([System.Management.Automation.PSObject])] + [CmdletBinding(SupportsShouldProcess = $false)] + param ( + [Parameter(Mandatory = $false, Position = 0)] + [ValidateNotNullOrEmpty()] + [System.Management.Automation.PSObject] + $res = (Get-FunctionResource -AppName ("$($MyInvocation.MyCommand)".Split("-"))[1]) + ) + + foreach ($Channel in $res.Get.Update.Channels) { + $Update = Invoke-RestMethodWrapper -Uri $res.Get.Update.Uri[$Channel] + + if ($null -ne $Update.($res.Get.Update.Property)) { + Write-Verbose -Message "$($MyInvocation.MyCommand): checking property: $($res.Get.Update.Property)." + Write-Verbose -Message "$($MyInvocation.MyCommand): found version: $($Update.($res.Get.Update.Property))" + + # Step through each installer type + foreach ($Architecture in $res.Get.Download.Architectures) { + + # Create the URL + $Url = $res.Get.Download.Uri[$Channel] -replace "#version", $Update.($res.Get.Update.Property) ` + -replace "#architecture", $res.Get.Download.Architecture[$Architecture] + + # Build the output object; Output object to the pipeline + $PSObject = [PSCustomObject] @{ + Version = $Update.($res.Get.Update.Property) + Channel = $Channel + Architecture = $Architecture + Type = Get-FileType -File $Url + URI = $Url + } + Write-Output -InputObject $PSObject + } + } + } +}