From 5a51d070d17361e5844782bfbb79747612f2d8a8 Mon Sep 17 00:00:00 2001 From: Bryan Oltman Date: Fri, 11 Aug 2023 11:44:40 -0400 Subject: [PATCH] fix(shorebird_cli): only redirect powershell stderr output if it won't cause the script to terminate (#1089) --- bin/shorebird.ps1 | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/bin/shorebird.ps1 b/bin/shorebird.ps1 index 7885d900..67688e09 100644 --- a/bin/shorebird.ps1 +++ b/bin/shorebird.ps1 @@ -16,6 +16,28 @@ $flutter = [IO.Path]::Combine($shorebirdCacheDir, "flutter", $flutterVersion, "b $shorebirdScript = [IO.Path]::Combine($shorebirdCliDir, "bin", "shorebird.dart") $dart = [IO.Path]::Combine($flutterPath, "bin", "cache", "dart-sdk", "bin", "dart.exe") +# Executes $command and redirects as much output to $null as possible. +# +# This is a workaround for old versions of Powershell treating any write to +# the Error stream with $ErrorActionPreference = "Stop" as a terminating error. +# This is fixed in Powershell 7.1. +# +# See https://github.com/PowerShell/PowerShell/issues/4002 for more info. +function Invoke-SilentlyIfPossible($command) { + $psVersion = $PSVersionTable.PSVersion + $shouldRedirectStdErr = $psVersion.Major -ge 7 -and $psVersion.Minor -ge 1 + if ($shouldRedirectStdErr) { + # Redirect everything to $null. + & $command *> $null + } + else { + # Otherwise redirect everything _but_ the Error stream to $null. See + # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_output_streams?view=powershell-7.3#long-description + # for a description of these streams. + & $command 1> $null 3> $null 4> $null 5> $null 6> $null + } +} + function Test-GitInstalled { if (Get-Command git -ErrorAction SilentlyContinue) { Write-Debug "Git is installed." @@ -78,15 +100,20 @@ function Update-Flutter { Write-Output "Updating Flutter..." if (!(Test-Path $flutterPath)) { - Write-Output "Cloning flutter, this may take a bit..." - git clone --filter=tree:0 https://github.com/shorebirdtech/flutter.git --no-checkout "$flutterPath" *> $null + Invoke-SilentlyIfPossible { + git clone --filter=tree:0 https://github.com/shorebirdtech/flutter.git --no-checkout "$flutterPath" + } } else { - git -C "$flutterPath" fetch *> $null + Invoke-SilentlyIfPossible { + git -C "$flutterPath" fetch + } } - # -c to avoid printing a warning about being in a detached head state. - git -C "$flutterPath" -c advice.detachedHead=false checkout "$flutterVersion" *> $null + Invoke-SilentlyIfPossible { + # -c to avoid printing a warning about being in a detached head state. + git -C "$flutterPath" -c advice.detachedHead=false checkout "$flutterVersion" + } # Set FLUTTER_STORAGE_BASE_URL=https://download.shorebird.dev and execute # a `flutter` command to trigger a download of Dart, etc.