Skip to content

Commit

Permalink
.build/build.ps1: return to working dir even after an error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
generalmimon committed Apr 29, 2024
1 parent 01fafdd commit 25ea924
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions .build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 25ea924

Please sign in to comment.