From e9e79b3ce4da7aeeb8da463547ea373374db1d5d Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Mon, 9 May 2022 09:15:24 -0500 Subject: [PATCH] Force Git version >= 2.35.2 (#57741) --- Tools/YamlCreate.ps1 | 154 ++++++++++++++++++++++++++----------------- 1 file changed, 95 insertions(+), 59 deletions(-) diff --git a/Tools/YamlCreate.ps1 b/Tools/YamlCreate.ps1 index fae2f78183f..17a10903982 100644 --- a/Tools/YamlCreate.ps1 +++ b/Tools/YamlCreate.ps1 @@ -23,12 +23,101 @@ if ($help) { exit } -# Check whether the script is present inside a fork/clone of microsoft/winget-pkgs repository -try { - $script:gitTopLevel = (Resolve-Path $(git rev-parse --show-toplevel)).Path -} catch { - # If there was an exception, the user isn't in a git repo. Throw a custom exception and pass the original exception as an InternalException - throw [UnmetDependencyException]::new('This script must be run from inside a clone of the winget-pkgs repository', $_.Exception) +# Custom menu prompt that listens for keypresses. Requires a prompt and array of entries at minimum. Entries preceeded with `*` are shown in green +# Returns a console key value +Function Invoke-KeypressMenu { + Param + ( + [Parameter(Mandatory = $true, Position = 0)] + [string] $Prompt, + [Parameter(Mandatory = $true, Position = 1)] + [string[]] $Entries, + [Parameter(Mandatory = $false)] + [string] $HelpText, + [Parameter(Mandatory = $false)] + [string] $HelpTextColor, + [Parameter(Mandatory = $false)] + [string] $DefaultString + ) + + Write-Host "`n" + Write-Host -ForegroundColor 'Yellow' "$Prompt" + if ($PSBoundParameters.ContainsKey('HelpText') -and (![string]::IsNullOrWhiteSpace($HelpText))) { + if ($PSBoundParameters.ContainsKey('HelpTextColor') -and (![string]::IsNullOrWhiteSpace($HelpTextColor))) { + Write-Host -ForegroundColor $HelpTextColor $HelpText + } else { + Write-Host -ForegroundColor 'Blue' $HelpText + } + } + foreach ($entry in $Entries) { + $_isDefault = $entry.StartsWith('*') + if ($_isDefault) { + $_entry = ' ' + $entry.Substring(1) + $_color = 'Green' + } else { + $_entry = ' ' + $entry + $_color = 'White' + } + Write-Host -ForegroundColor $_color $_entry + } + Write-Host + if ($PSBoundParameters.ContainsKey('DefaultString') -and (![string]::IsNullOrWhiteSpace($DefaultString))) { + Write-Host -NoNewline "Enter Choice (default is '$DefaultString'): " + } else { + Write-Host -NoNewline 'Enter Choice (' + Write-Host -NoNewline -ForegroundColor 'Green' 'Green' + Write-Host -NoNewline ' is default): ' + } + + do { + $keyInfo = [Console]::ReadKey($false) + } until ($keyInfo.Key) + + return $keyInfo.Key +} + +#If the user has git installed, make sure it is a patched version +if (Get-Command 'git.exe' -ErrorAction SilentlyContinue) { + $GitMinimumVersion = [System.Version]::Parse('2.35.2') + $gitVersionString = ((git version) | Select-String '([0-9]{1,}\.){3,4}').Matches.Value.Trim(' ', '.') + $gitVersion = [System.Version]::Parse($gitVersionString) + if ($gitVersion -lt $GitMinimumVersion) { + # Prompt user to install git + if (Get-Command 'winget.exe' -ErrorAction SilentlyContinue) { + $_menu = @{ + entries = @('[Y] Upgrade Git'; '[N] Do not upgrade') + Prompt = 'The version of git installed on your machine does not satisfy the requirement of version >= 2.35.2; Would you like to upgrade?' + HelpText = "Upgrading will attempt to upgrade git using winget`n" + DefaultString = '' + } + switch (Invoke-KeypressMenu -Prompt $_menu['Prompt'] -Entries $_menu['Entries'] -DefaultString $_menu['DefaultString'] -HelpText $_menu['HelpText']) { + 'Y' { + Write-Host + try { + winget upgrade --id Git.Git --exact + } catch { + throw [UnmetDependencyException]::new('Git could not be upgraded sucessfully', $_) + } finally { + $gitVersionString = ((git version) | Select-String '([0-9]{1,}\.){3,4}').Matches.Value.Trim(' ', '.') + $gitVersion = [System.Version]::Parse($gitVersionString) + if ($gitVersion -lt $GitMinimumVersion) { + throw [UnmetDependencyException]::new('Git could not be upgraded sucessfully') + } + } + } + default { Write-Host; throw [UnmetDependencyException]::new('The version of git installed on your machine does not satisfy the requirement of version >= 2.35.2') } + } + } else { + throw [UnmetDependencyException]::new('The version of git installed on your machine does not satisfy the requirement of version >= 2.35.2') + } + } + # Check whether the script is present inside a fork/clone of microsoft/winget-pkgs repository + try { + $script:gitTopLevel = (Resolve-Path $(git rev-parse --show-toplevel)).Path + } catch { + # If there was an exception, the user isn't in a git repo. Throw a custom exception and pass the original exception as an InternalException + throw [UnmetDependencyException]::new('This script must be run from inside a clone of the winget-pkgs repository', $_.Exception) + } } # Installs `powershell-yaml` as a dependency for parsing yaml content @@ -244,59 +333,6 @@ Function Write-MulticolorLine { } } -# Custom menu prompt that listens for keypresses. Requires a prompt and array of entries at minimum. Entries preceeded with `*` are shown in green -# Returns a console key value -Function Invoke-KeypressMenu { - Param - ( - [Parameter(Mandatory = $true, Position = 0)] - [string] $Prompt, - [Parameter(Mandatory = $true, Position = 1)] - [string[]] $Entries, - [Parameter(Mandatory = $false)] - [string] $HelpText, - [Parameter(Mandatory = $false)] - [string] $HelpTextColor, - [Parameter(Mandatory = $false)] - [string] $DefaultString - ) - - Write-Host "`n" - Write-Host -ForegroundColor 'Yellow' "$Prompt" - if ($PSBoundParameters.ContainsKey('HelpText') -and (![string]::IsNullOrWhiteSpace($HelpText))) { - if ($PSBoundParameters.ContainsKey('HelpTextColor') -and (![string]::IsNullOrWhiteSpace($HelpTextColor))) { - Write-Host -ForegroundColor $HelpTextColor $HelpText - } else { - Write-Host -ForegroundColor 'Blue' $HelpText - } - } - foreach ($entry in $Entries) { - $_isDefault = $entry.StartsWith('*') - if ($_isDefault) { - $_entry = ' ' + $entry.Substring(1) - $_color = 'Green' - } else { - $_entry = ' ' + $entry - $_color = 'White' - } - Write-Host -ForegroundColor $_color $_entry - } - Write-Host - if ($PSBoundParameters.ContainsKey('DefaultString') -and (![string]::IsNullOrWhiteSpace($DefaultString))) { - Write-Host -NoNewline "Enter Choice (default is '$DefaultString'): " - } else { - Write-Host -NoNewline 'Enter Choice (' - Write-Host -NoNewline -ForegroundColor 'Green' 'Green' - Write-Host -NoNewline ' is default): ' - } - - do { - $keyInfo = [Console]::ReadKey($false) - } until ($keyInfo.Key) - - return $keyInfo.Key -} - # Checks a URL and returns the status code received from the URL Function Test-Url { Param