From d879fc338c12defb4b8d2edb84b10cb4762ea250 Mon Sep 17 00:00:00 2001 From: jaimergp Date: Tue, 29 Oct 2024 09:00:38 +0100 Subject: [PATCH 1/5] export INSTALLER_UNATTENDED --- constructor/construct.py | 3 +++ constructor/header.sh | 3 +++ constructor/nsis/main.nsi.tmpl | 10 ++++++++++ constructor/osx/run_user_script.sh | 1 + examples/scripts/post_install.bat | 1 + examples/scripts/post_install.sh | 1 + examples/scripts/pre_install.bat | 1 + examples/scripts/pre_install.sh | 1 + 8 files changed, 21 insertions(+) diff --git a/constructor/construct.py b/constructor/construct.py index 1a3a2b9e7..f556f566f 100644 --- a/constructor/construct.py +++ b/constructor/construct.py @@ -350,13 +350,16 @@ installation path is available as `${PREFIX}`. Installer metadata is available in the `${INSTALLER_NAME}`, `${INSTALLER_VER}`, `${INSTALLER_PLAT}` environment variables. `${INSTALLER_TYPE}` is set to `SH`. + `${INSTALLER_UNATTENDED}` will be `"1"` in batch mode (`-b`), `"0"` otherwise. - For PKG installers, the shebang line is respected if present; otherwise, `bash` is used. The same variables mentioned for `sh` installers are available here. `${INSTALLER_TYPE}` is set to `PKG`. + `${INSTALLER_UNATTENDED}` is not supported and always set to `"?"`. - For Windows `.exe` installers, the script must be a `.bat` file. Installation path is available as `%PREFIX%`. Metadata about the installer can be found in the `%INSTALLER_NAME%`, `%INSTALLER_VER%`, `%INSTALLER_PLAT%` environment variables. `%INSTALLER_TYPE%` is set to `EXE`. + `%INSTALLER_UNATTENDED%` will be `"1"` in silent mode (`/S`), `"0"` otherwise. If necessary, you can activate the installed `base` environment like this: diff --git a/constructor/header.sh b/constructor/header.sh index f91e49ae0..b1d38be9d 100644 --- a/constructor/header.sh +++ b/constructor/header.sh @@ -179,6 +179,9 @@ while getopts "bifhkp:sut" x; do esac done +# For pre- and post-install scripts +export INSTALLER_UNATTENDED="$BATCH" + # For testing, keep the package cache around longer CLEAR_AFTER_TEST=0 if [ "$TEST" = "1" ] && [ "$KEEP_PKGS" = "0" ]; then diff --git a/constructor/nsis/main.nsi.tmpl b/constructor/nsis/main.nsi.tmpl index 6ff4ecdc0..29dfe1036 100644 --- a/constructor/nsis/main.nsi.tmpl +++ b/constructor/nsis/main.nsi.tmpl @@ -1250,6 +1250,11 @@ Section "Install" System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_VER", "${VERSION}").r0' System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_PLAT", "${PLATFORM}").r0' System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_TYPE", "EXE").r0' + ${If} ${Silent} + System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "1").r0' + ${Else} + System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "0").r0' + ${EndIf} ${If} '@VIRTUAL_SPECS@' != '' # We need to specify CONDA_SOLVER=classic for conda-standalone @@ -1452,6 +1457,11 @@ Section "Uninstall" System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_VER", "$0").r0' System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_PLAT", "${PLATFORM}").r0' System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_TYPE", "EXE").r0' + ${If} ${Silent} + System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "1").r0' + ${Else} + System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "0").r0' + ${EndIf} !insertmacro AbortRetryNSExecWaitLibNsisCmd "pre_uninstall" !insertmacro AbortRetryNSExecWaitLibNsisCmd "rmpath" diff --git a/constructor/osx/run_user_script.sh b/constructor/osx/run_user_script.sh index 61816160f..ca325f9d5 100644 --- a/constructor/osx/run_user_script.sh +++ b/constructor/osx/run_user_script.sh @@ -30,6 +30,7 @@ export INSTALLER_NAME="__NAME__" export INSTALLER_VER="__VERSION__" export INSTALLER_PLAT="__PLAT__" export INSTALLER_TYPE="PKG" +export INSTALLER_UNATTENDED="?" export PRE_OR_POST="__PRE_OR_POST__" _SCRIPT_ENV_VARIABLES_='' # Templated extra environment variable(s) diff --git a/examples/scripts/post_install.bat b/examples/scripts/post_install.bat index 7a435cafc..a3b406011 100644 --- a/examples/scripts/post_install.bat +++ b/examples/scripts/post_install.bat @@ -3,6 +3,7 @@ if not "%INSTALLER_NAME%" == "Scripts" exit 1 if not "%INSTALLER_VER%" == "X" exit 1 if not "%INSTALLER_PLAT%" == "win-64" exit 1 if not "%INSTALLER_TYPE%" == "EXE" exit 1 +if "%INSTALLER_UNATTENDED%" == "" exit 1 if "%PREFIX%" == "" exit 1 if not "%CUSTOM_VARIABLE_1%" == "FIR$T-CUSTOM_STRING WITH SPACES AND @*! CHARACTERS" exit 1 if not "%CUSTOM_VARIABLE_2%" == "$ECOND-CUSTOM_STRING WITH SPACES AND @*! CHARACTERS" exit 1 diff --git a/examples/scripts/post_install.sh b/examples/scripts/post_install.sh index 1db67d136..e11b25bf6 100644 --- a/examples/scripts/post_install.sh +++ b/examples/scripts/post_install.sh @@ -9,6 +9,7 @@ echo "INSTALLER_NAME=${INSTALLER_NAME}" echo "INSTALLER_VER=${INSTALLER_VER}" echo "INSTALLER_PLAT=${INSTALLER_PLAT}" echo "INSTALLER_TYPE=${INSTALLER_TYPE}" +echo "INSTALLER_UNATTENDED=${INSTALLER_UNATTENDED}" echo "CUSTOM_VARIABLE_1=${CUSTOM_VARIABLE_1}" echo "CUSTOM_VARIABLE_2=${CUSTOM_VARIABLE_2}" echo "PREFIX=${PREFIX}" diff --git a/examples/scripts/pre_install.bat b/examples/scripts/pre_install.bat index 5ece67c31..b8af99da3 100644 --- a/examples/scripts/pre_install.bat +++ b/examples/scripts/pre_install.bat @@ -2,6 +2,7 @@ if not "%INSTALLER_NAME%" == "Scripts" exit 1 if not "%INSTALLER_VER%" == "X" exit 1 if not "%INSTALLER_PLAT%" == "win-64" exit 1 if not "%INSTALLER_TYPE%" == "EXE" exit 1 +if "%INSTALLER_UNATTENDED%" == "" exit 1 if "%PREFIX%" == "" exit 1 if not "%CUSTOM_VARIABLE_1%" == "FIR$T-CUSTOM_STRING WITH SPACES AND @*! CHARACTERS" exit 1 if not "%CUSTOM_VARIABLE_2%" == "$ECOND-CUSTOM_STRING WITH SPACES AND @*! CHARACTERS" exit 1 diff --git a/examples/scripts/pre_install.sh b/examples/scripts/pre_install.sh index df0806980..fcf11dcf7 100644 --- a/examples/scripts/pre_install.sh +++ b/examples/scripts/pre_install.sh @@ -6,6 +6,7 @@ echo "INSTALLER_NAME=${INSTALLER_NAME}" echo "INSTALLER_VER=${INSTALLER_VER}" echo "INSTALLER_PLAT=${INSTALLER_PLAT}" echo "INSTALLER_TYPE=${INSTALLER_TYPE}" +echo "INSTALLER_UNATTENDED=${INSTALLER_UNATTENDED}" echo "CUSTOM_VARIABLE_1=${CUSTOM_VARIABLE_1}" echo "CUSTOM_VARIABLE_2=${CUSTOM_VARIABLE_2}" echo "PREFIX=${PREFIX}" From 158881a7d2cfd004ddb7eff7a2c494d99f4e281e Mon Sep 17 00:00:00 2001 From: jaimergp Date: Tue, 29 Oct 2024 09:02:36 +0100 Subject: [PATCH 2/5] add news --- news/885-installer-unattended | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 news/885-installer-unattended diff --git a/news/885-installer-unattended b/news/885-installer-unattended new file mode 100644 index 000000000..b4ee1d139 --- /dev/null +++ b/news/885-installer-unattended @@ -0,0 +1,19 @@ +### Enhancements + +* Export `INSTALLER_UNATTENDED` environment variable so pre- and post-install scripts can detect if the installer is running in batch/silent mode or not. (#882 via #885) + +### Bug fixes + +* + +### Deprecations + +* + +### Docs + +* + +### Other + +* From 20986266f7a7bdb6aef921d69b344bf701c74272 Mon Sep 17 00:00:00 2001 From: jaimergp Date: Tue, 29 Oct 2024 11:02:30 +0100 Subject: [PATCH 3/5] rerender docs --- CONSTRUCT.md | 3 +++ docs/source/construct-yaml.md | 3 +++ 2 files changed, 6 insertions(+) diff --git a/CONSTRUCT.md b/CONSTRUCT.md index 92d93b572..831af3d06 100644 --- a/CONSTRUCT.md +++ b/CONSTRUCT.md @@ -476,13 +476,16 @@ Path to a post-install script. Some notes: installation path is available as `${PREFIX}`. Installer metadata is available in the `${INSTALLER_NAME}`, `${INSTALLER_VER}`, `${INSTALLER_PLAT}` environment variables. `${INSTALLER_TYPE}` is set to `SH`. + `${INSTALLER_UNATTENDED}` will be `"1"` in batch mode (`-b`), `"0"` otherwise. - For PKG installers, the shebang line is respected if present; otherwise, `bash` is used. The same variables mentioned for `sh` installers are available here. `${INSTALLER_TYPE}` is set to `PKG`. + `${INSTALLER_UNATTENDED}` is not supported and always set to `"?"`. - For Windows `.exe` installers, the script must be a `.bat` file. Installation path is available as `%PREFIX%`. Metadata about the installer can be found in the `%INSTALLER_NAME%`, `%INSTALLER_VER%`, `%INSTALLER_PLAT%` environment variables. `%INSTALLER_TYPE%` is set to `EXE`. + `%INSTALLER_UNATTENDED%` will be `"1"` in silent mode (`/S`), `"0"` otherwise. If necessary, you can activate the installed `base` environment like this: diff --git a/docs/source/construct-yaml.md b/docs/source/construct-yaml.md index 92d93b572..831af3d06 100644 --- a/docs/source/construct-yaml.md +++ b/docs/source/construct-yaml.md @@ -476,13 +476,16 @@ Path to a post-install script. Some notes: installation path is available as `${PREFIX}`. Installer metadata is available in the `${INSTALLER_NAME}`, `${INSTALLER_VER}`, `${INSTALLER_PLAT}` environment variables. `${INSTALLER_TYPE}` is set to `SH`. + `${INSTALLER_UNATTENDED}` will be `"1"` in batch mode (`-b`), `"0"` otherwise. - For PKG installers, the shebang line is respected if present; otherwise, `bash` is used. The same variables mentioned for `sh` installers are available here. `${INSTALLER_TYPE}` is set to `PKG`. + `${INSTALLER_UNATTENDED}` is not supported and always set to `"?"`. - For Windows `.exe` installers, the script must be a `.bat` file. Installation path is available as `%PREFIX%`. Metadata about the installer can be found in the `%INSTALLER_NAME%`, `%INSTALLER_VER%`, `%INSTALLER_PLAT%` environment variables. `%INSTALLER_TYPE%` is set to `EXE`. + `%INSTALLER_UNATTENDED%` will be `"1"` in silent mode (`/S`), `"0"` otherwise. If necessary, you can activate the installed `base` environment like this: From 352eb11a52f69b72e2409e4421dd132a6f68a5e1 Mon Sep 17 00:00:00 2001 From: jaimergp Date: Wed, 30 Oct 2024 18:28:21 +0100 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Marco Esters --- constructor/nsis/main.nsi.tmpl | 8 ++++---- examples/scripts/post_install.bat | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/constructor/nsis/main.nsi.tmpl b/constructor/nsis/main.nsi.tmpl index 29dfe1036..125901856 100644 --- a/constructor/nsis/main.nsi.tmpl +++ b/constructor/nsis/main.nsi.tmpl @@ -1251,9 +1251,9 @@ Section "Install" System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_PLAT", "${PLATFORM}").r0' System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_TYPE", "EXE").r0' ${If} ${Silent} - System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "1").r0' + System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "1").r0' ${Else} - System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "0").r0' + System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "0").r0' ${EndIf} ${If} '@VIRTUAL_SPECS@' != '' @@ -1458,9 +1458,9 @@ Section "Uninstall" System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_PLAT", "${PLATFORM}").r0' System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_TYPE", "EXE").r0' ${If} ${Silent} - System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "1").r0' + System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "1").r0' ${Else} - System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "0").r0' + System::Call 'kernel32::SetEnvironmentVariable(t,t)i("INSTALLER_UNATTENDED", "0").r0' ${EndIf} !insertmacro AbortRetryNSExecWaitLibNsisCmd "pre_uninstall" diff --git a/examples/scripts/post_install.bat b/examples/scripts/post_install.bat index a3b406011..7916add93 100644 --- a/examples/scripts/post_install.bat +++ b/examples/scripts/post_install.bat @@ -3,7 +3,7 @@ if not "%INSTALLER_NAME%" == "Scripts" exit 1 if not "%INSTALLER_VER%" == "X" exit 1 if not "%INSTALLER_PLAT%" == "win-64" exit 1 if not "%INSTALLER_TYPE%" == "EXE" exit 1 -if "%INSTALLER_UNATTENDED%" == "" exit 1 +if not "%INSTALLER_UNATTENDED%" == "1" exit 1 if "%PREFIX%" == "" exit 1 if not "%CUSTOM_VARIABLE_1%" == "FIR$T-CUSTOM_STRING WITH SPACES AND @*! CHARACTERS" exit 1 if not "%CUSTOM_VARIABLE_2%" == "$ECOND-CUSTOM_STRING WITH SPACES AND @*! CHARACTERS" exit 1 From b58577c8faa5f5990d052e6bde0f8d0a16b2b78c Mon Sep 17 00:00:00 2001 From: jaimergp Date: Wed, 30 Oct 2024 22:27:40 +0100 Subject: [PATCH 5/5] more precise tests --- examples/scripts/post_install.sh | 4 ++++ examples/scripts/pre_install.bat | 2 +- examples/scripts/pre_install.sh | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/scripts/post_install.sh b/examples/scripts/post_install.sh index e11b25bf6..db0509f26 100644 --- a/examples/scripts/post_install.sh +++ b/examples/scripts/post_install.sh @@ -21,6 +21,10 @@ test "${CUSTOM_VARIABLE_1}" = 'FIR$T-CUSTOM_'\''STRING'\'' WITH SPACES AND @*! " # shellcheck disable=SC2016 # String interpolation disabling is deliberate test "${CUSTOM_VARIABLE_2}" = '$ECOND-CUSTOM_'\''STRING'\'' WITH SPACES AND @*! "CHARACTERS"' +if [[ "${INSTALLER_TYPE}" == "SH" ]]; then + test "${INSTALLER_UNATTENDED}" = "1" +fi + if [[ $(uname -s) == Linux ]]; then if [[ ${INSTALLER_PLAT} != linux-* ]]; then exit 1 diff --git a/examples/scripts/pre_install.bat b/examples/scripts/pre_install.bat index b8af99da3..ec4fce07c 100644 --- a/examples/scripts/pre_install.bat +++ b/examples/scripts/pre_install.bat @@ -2,7 +2,7 @@ if not "%INSTALLER_NAME%" == "Scripts" exit 1 if not "%INSTALLER_VER%" == "X" exit 1 if not "%INSTALLER_PLAT%" == "win-64" exit 1 if not "%INSTALLER_TYPE%" == "EXE" exit 1 -if "%INSTALLER_UNATTENDED%" == "" exit 1 +if not "%INSTALLER_UNATTENDED%" == "1" exit 1 if "%PREFIX%" == "" exit 1 if not "%CUSTOM_VARIABLE_1%" == "FIR$T-CUSTOM_STRING WITH SPACES AND @*! CHARACTERS" exit 1 if not "%CUSTOM_VARIABLE_2%" == "$ECOND-CUSTOM_STRING WITH SPACES AND @*! CHARACTERS" exit 1 diff --git a/examples/scripts/pre_install.sh b/examples/scripts/pre_install.sh index fcf11dcf7..753db8121 100644 --- a/examples/scripts/pre_install.sh +++ b/examples/scripts/pre_install.sh @@ -18,6 +18,10 @@ test "${CUSTOM_VARIABLE_1}" = 'FIR$T-CUSTOM_'\''STRING'\'' WITH SPACES AND @*! " # shellcheck disable=SC2016 # String interpolation disabling is deliberate test "${CUSTOM_VARIABLE_2}" = '$ECOND-CUSTOM_'\''STRING'\'' WITH SPACES AND @*! "CHARACTERS"' +if [[ "${INSTALLER_TYPE}" == "SH" ]]; then + test "${INSTALLER_UNATTENDED}" = "1" +fi + if [[ $(uname -s) == Linux ]]; then if [[ ${INSTALLER_PLAT} != linux-* ]]; then exit 1