From 25ea9242e6e1cdd5df0f63a142674c9390007e25 Mon Sep 17 00:00:00 2001 From: Petr Pucil Date: Mon, 29 Apr 2024 00:32:01 +0200 Subject: [PATCH] .build/build.ps1: return to working dir even after an error When testing the script locally in PowerShell console, I noticed that if one of the commands fails and causes the termination of the script, the last command `Pop-Location` is not executed and thus the working directory in the PowerShell console remains `$repoRoot\build`. From the user's point of view, this means that the script changed their working directory, which is annoying. For example, here is how the old version of the script behaves in a PowerShell session if the `build/` folder is not writable: ``` PS C:\temp\kaitai_struct\runtime\cpp_stl> .\.build\build.ps1 -GTestPath C:\temp\vcpkg\installed\x64-windows Re-run cmake no build system arguments CMake Error: Unable to (re)create the private pkgRedirects directory: C:/temp/kaitai_struct/runtime/cpp_stl/build/CMakeFiles/pkgRedirects Exception: C:\temp\kaitai_struct\runtime\cpp_stl\.build\build.ps1:35 Line | 35 | throw "'cmake' exited with code $LastExitCode" | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 'cmake' exited with code 1 PS C:\temp\kaitai_struct\runtime\cpp_stl\build> ``` Notice that the original prompt was `PS C:\temp\kaitai_struct\runtime\cpp_stl>`, but the new prompt is `PS C:\temp\kaitai_struct\runtime\cpp_stl\build>`. This commit fixes this issue by ensuring that `Pop-Location` runs regardless of whether any previous command failed. --- .build/build.ps1 | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/.build/build.ps1 b/.build/build.ps1 index 5f884fb..a1bf513 100644 --- a/.build/build.ps1 +++ b/.build/build.ps1 @@ -25,22 +25,24 @@ $PSDefaultParameterValues['*:ErrorAction'] = 'Stop' $repoRoot = (Resolve-Path "$PSScriptRoot\..").Path Push-Location $repoRoot -$null = New-Item -Path build -ItemType Directory -Force -cd build - -$env:VERBOSE = '1' - -cmake -DCMAKE_PREFIX_PATH="$GTestPath" -DSTRING_ENCODING_TYPE="$EncodingType" .. -if ($LastExitCode -ne 0) { - throw "'cmake' exited with code $LastExitCode" -} - -cmake --build . --config Debug -if ($LastExitCode -ne 0) { - throw "'cmake --build' exited with code $LastExitCode" +try { + $null = New-Item -Path build -ItemType Directory -Force + cd build + + $env:VERBOSE = '1' + + cmake -DCMAKE_PREFIX_PATH="$GTestPath" -DSTRING_ENCODING_TYPE="$EncodingType" .. + if ($LastExitCode -ne 0) { + throw "'cmake' exited with code $LastExitCode" + } + + cmake --build . --config Debug + if ($LastExitCode -ne 0) { + throw "'cmake --build' exited with code $LastExitCode" + } + + cp $GTestPath\debug\bin\*.dll tests\Debug + cp Debug\kaitai_struct_cpp_stl_runtime.dll tests\Debug +} finally { + Pop-Location } - -cp $GTestPath\debug\bin\*.dll tests\Debug -cp Debug\kaitai_struct_cpp_stl_runtime.dll tests\Debug - -Pop-Location