@@ -863,67 +863,102 @@ function Test-MikefarahYqAvailable {
863863 #>
864864 [CmdletBinding ()]
865865 [OutputType ([bool ])]
866- param ()
867-
868- if (Get-Command Get-CachedExternalCommand - ErrorAction SilentlyContinue) {
869- $yqCmd = Get-CachedExternalCommand - Name ' yq'
870- if (-not $yqCmd ) {
871- return $false
872- }
873-
874- if (Get-Command Test-IsMikefarahYqExecutable - ErrorAction SilentlyContinue) {
875- $executable = if (Get-Command Resolve-CommandExecutablePath - ErrorAction SilentlyContinue) {
876- Resolve-CommandExecutablePath - CommandInfo $yqCmd
877- }
878- elseif (-not [string ]::IsNullOrWhiteSpace($yqCmd.Name )) {
879- [string ]$yqCmd.Name
880- }
881- else {
882- $null
883- }
884- return Test-IsMikefarahYqExecutable - Executable $executable
885- }
866+ param (
867+ [switch ]$Refresh
868+ )
886869
887- return $true
870+ if (-not $Refresh -and (Get-Variable - Name MikefarahYqAvailabilityCache - Scope Script - ErrorAction SilentlyContinue)) {
871+ return [bool ]$script :MikefarahYqAvailabilityCache
888872 }
889873
890- foreach ($candidate in @ (' go-yq' , ' yq' )) {
891- foreach ($cmd in @ (Get-Command $candidate - CommandType Application - ErrorAction SilentlyContinue)) {
892- $exe = if (Get-Command Resolve-CommandExecutablePath - ErrorAction SilentlyContinue) {
893- Resolve-CommandExecutablePath - CommandInfo $cmd
894- }
895- elseif (-not [string ]::IsNullOrWhiteSpace($cmd.Name )) {
896- [string ]$cmd.Name
897- }
898- else {
899- $null
900- }
901- if ([string ]::IsNullOrWhiteSpace($exe )) {
902- continue
903- }
904-
905- if (Get-Command Test-IsMikefarahYqExecutable - ErrorAction SilentlyContinue) {
906- if (Test-IsMikefarahYqExecutable - Executable $exe ) {
907- return $true
874+ try {
875+ $available = $false
876+
877+ if (Get-Command Get-CachedExternalCommand - ErrorAction SilentlyContinue) {
878+ $yqCmd = Get-CachedExternalCommand - Name ' yq'
879+ if ($yqCmd ) {
880+ if (Get-Command Test-IsMikefarahYqExecutable - ErrorAction SilentlyContinue) {
881+ $executable = if (Get-Command Resolve-CommandExecutablePath - ErrorAction SilentlyContinue) {
882+ Resolve-CommandExecutablePath - CommandInfo $yqCmd
883+ }
884+ elseif (-not [string ]::IsNullOrWhiteSpace($yqCmd.Name )) {
885+ [string ]$yqCmd.Name
886+ }
887+ else {
888+ $null
889+ }
890+ $available = Test-IsMikefarahYqExecutable - Executable $executable
908891 }
909- }
910- else {
911- $versionOutput = (& $exe -- version 2>&1 | Out-String ).Trim()
912- if ($versionOutput -match ' mikefarah|github\.com/mikefarah' ) {
913- return $true
892+ else {
893+ $available = $true
914894 }
895+ }
896+ }
897+ else {
898+ foreach ($candidate in @ (' go-yq' , ' yq' )) {
899+ foreach ($cmd in @ (Get-Command $candidate - CommandType Application - ErrorAction SilentlyContinue)) {
900+ $exe = if (Get-Command Resolve-CommandExecutablePath - ErrorAction SilentlyContinue) {
901+ Resolve-CommandExecutablePath - CommandInfo $cmd
902+ }
903+ elseif (-not [string ]::IsNullOrWhiteSpace($cmd.Name )) {
904+ [string ]$cmd.Name
905+ }
906+ else {
907+ $null
908+ }
909+ if ([string ]::IsNullOrWhiteSpace($exe )) {
910+ continue
911+ }
915912
916- if ($versionOutput -notmatch ' ^yq\s+\d' ) {
917- $evalHelp = (& $exe eval -- help 2>&1 | Out-String )
918- if ($evalHelp -match ' evaluates' -and $evalHelp -notmatch ' jq_filter' ) {
919- return $true
913+ if (Get-Command Test-IsMikefarahYqExecutable - ErrorAction SilentlyContinue) {
914+ if (Test-IsMikefarahYqExecutable - Executable $exe ) {
915+ $available = $true
916+ break
917+ }
918+ }
919+ else {
920+ $versionOutput = (& $exe -- version 2>&1 | Out-String ).Trim()
921+ if ($versionOutput -match ' mikefarah|github\.com/mikefarah' ) {
922+ $available = $true
923+ break
924+ }
925+
926+ if ($versionOutput -notmatch ' ^yq\s+\d' ) {
927+ $evalHelp = (& $exe eval -- help 2>&1 | Out-String )
928+ if ($evalHelp -match ' evaluates' -and $evalHelp -notmatch ' jq_filter' ) {
929+ $available = $true
930+ break
931+ }
932+ }
920933 }
921934 }
935+
936+ if ($available ) {
937+ break
938+ }
922939 }
923940 }
941+
942+ $script :MikefarahYqAvailabilityCache = $available
943+ return $available
924944 }
945+ catch {
946+ $script :MikefarahYqAvailabilityCache = $false
947+ return $false
948+ }
949+ }
925950
926- return $false
951+ function Clear-TestMikefarahYqAvailabilityCache {
952+ <#
953+ . SYNOPSIS
954+ Clears the cached result used by Test-MikefarahYqAvailable.
955+ #>
956+ [CmdletBinding ()]
957+ param ()
958+
959+ if (Get-Variable - Name MikefarahYqAvailabilityCache - Scope Script - ErrorAction SilentlyContinue) {
960+ $script :MikefarahYqAvailabilityCache = $null
961+ }
927962}
928963
929964function Get-TestMikefarahYqSkipMessage {
0 commit comments