diff --git a/bin/hab.bat b/bin/hab.bat index 4973efb..1be71cb 100644 --- a/bin/hab.bat +++ b/bin/hab.bat @@ -41,10 +41,12 @@ if exist %temp_launch_file% ( ) ) -:: the config scripts turn echo back on, turn it off again -@ECHO OFF +:: the config scripts turn echo back on, Make sure to prefix future commands +:: with @ to prevent printing them to the console :: Remove the temp directory and its contents -RMDIR /S /Q %temp_directory% +@RMDIR /S /Q %temp_directory% -@ECHO ON +:: Ensure the errorlevel is reported to the calling process. This is needed +:: when calling hab via subprocess/QtCore.QProcess calls to receive errorlevel +@exit /b %ERRORLEVEL% diff --git a/bin/hab.ps1 b/bin/hab.ps1 index 23fc483..8130853 100644 --- a/bin/hab.ps1 +++ b/bin/hab.ps1 @@ -47,3 +47,6 @@ elseif (Test-Path $temp_config -PathType Leaf) # Remove the temp directory if it exists Remove-Item $temp_directory -Force -Recurse -erroraction 'silentlycontinue' + +# Ensure the exit-code is reported to the calling process. +exit $LASTEXITCODE diff --git a/hab/parsers/hab_base.py b/hab/parsers/hab_base.py index aaca19e..f558059 100644 --- a/hab/parsers/hab_base.py +++ b/hab/parsers/hab_base.py @@ -631,11 +631,13 @@ def shell_formats(cls, ext): if ext in (".bat", ".cmd"): ret["launch"] = 'cmd.exe /k "{path}"\n' elif ext == ".ps1": - ret[ - "launch" - ] = 'powershell.exe{launch_args} -ExecutionPolicy Unrestricted . "{path}"\n' + ret["launch"] = ( + 'powershell.exe{launch_args} -ExecutionPolicy Unrestricted -File "{path}"\n' + "exit $LASTEXITCODE\n" + ) elif ext in (".sh", ""): # Assume no ext is a .sh file - ret["launch"] = 'bash --init-file "{path}"\n' + # `set -e` propagates the error code returned by bash to the caller + ret["launch"] = 'set -e\nbash --init-file "{path}"\n' return ret diff --git a/hab/templates/config.ps1 b/hab/templates/config.ps1 index 1f18bae..f9c1555 100644 --- a/hab/templates/config.ps1 +++ b/hab/templates/config.ps1 @@ -55,4 +55,6 @@ function {{ alias }}() { {% if launch_info %} # Run the requested command {{ launch_info.key }}{{ launch_info.args }} +# Ensure the exit-code is reported to the calling process. +exit $LASTEXITCODE {% endif %} diff --git a/hab/templates/config.sh b/hab/templates/config.sh index 6725422..6a4fad1 100644 --- a/hab/templates/config.sh +++ b/hab/templates/config.sh @@ -1,6 +1,11 @@ # Customize the prompt export PS1="[{{ hab_cfg.uri }}] $PS1" +# Exit immediately if a command exits with a non-zero status. +# This ensures that if any exit codes are encountered it gets propagated to +# whatever originally called hab. +set -e + # Setting global environment variables: {% for key, value in hab_cfg.environment.items() %} {% if value %}