From c928cef07925a5833bd81e78cbee7cc2b42bb9b1 Mon Sep 17 00:00:00 2001 From: reksarka Date: Wed, 20 Sep 2023 05:20:40 +0300 Subject: [PATCH] FIX: Extracting SDK Components In some cases, it is needed to temporary change the workdir. After which some relative paths became inacessible. So, the main fix is to use abs paths for the mentioned cases. --- get-sdk.bat | 2 +- sdk/extract-components.bat | 47 +++++++++++++++++++++----------------- sdk/extract-installers.bat | 22 ++++++++++-------- utils/destination.bat | 3 ++- utils/ensure-archiver.bat | 2 +- utils/ensure-lessmsi.bat | 5 ++-- 6 files changed, 44 insertions(+), 37 deletions(-) diff --git a/get-sdk.bat b/get-sdk.bat index 256ea62..55f284c 100644 --- a/get-sdk.bat +++ b/get-sdk.bat @@ -23,7 +23,7 @@ call check-components "%destination%" && ( ) echo Getting Windows SDK -set "tmp=%destination%\tmp" +for %%i in ("%destination%") do set "tmp=%%~fi\tmp" set "archiver=%tmp%\7-zip" call ensure-archiver "%archiver%" diff --git a/sdk/extract-components.bat b/sdk/extract-components.bat index 49ddf6b..9a89bde 100644 --- a/sdk/extract-components.bat +++ b/sdk/extract-components.bat @@ -17,48 +17,53 @@ setlocal set installers=%~1 set components=%~2 -call which lessmsi >NUL 2>&1 || ( - echo [ERR][%~n0] Requires `lessmsi`! +rem The `lessmsi` may have errors when using some complicated paths. +rem To prevent them, we need to change the workdir to %components% and then +rem use relative paths. Before doing this, we need to save the %origin_path% +rem and ensure that %installers% is an abs path. +set "origin_path=%CD%" +for %%i in ("%installers%") do set "installers=%%~fi" +if not exist "%components%" md "%components%" +cd /d "%components%" + +rem Check this after cd %components%! +call check-installers "%installers%" || ( + echo [ERR][%~n0] No installers found! exit /b 1 ) -rem Only used to suppress output from `lessmsi` to stdout. -set "lessmsi_log=%components%\lessmsi.log" +call which lessmsi >NUL 2>&1 || ( + echo [ERR][%~n0] Executable `lessmsi` not found! + exit /b 2 +) -if exist "%lessmsi_log%" ( - del /q /s "%lessmsi_log%" >NUL 2>&1 && ( - echo [WARN][%~n0] The old "%lessmsi_log%" has been deleted! +rem Used only to suppress output from `lessmsi` to stdout. +if exist lessmsi.log ( + del /q /s lessmsi.log >NUL 2>&1 && ( + echo [WARN][%~n0] The old lessmsi.log has been deleted! ) || ( - echo [WARN][%~n0] Failed to delete "%lessmsi_log%"! + echo [WARN][%~n0] Failed to delete lessmsi.log! ) ) -rem Prevent path errors for `lessmsi`. -set "origin_path=%CD%" -if not exist "%components%" md "%components%" -cd /d "%components%" - echo - Extracting SDK Components for %%i in ("%installers%\*.msi") do ( - if not exist "%components%\%%~ni" ( + if not exist "%%~ni" ( echo - %%~ni - lessmsi x "%%i" >> "%lessmsi_log%" || ( + lessmsi x "%%i" >> lessmsi.log || ( echo [ERR][%~n0] Unable to extract SDK Component! cd /d "%origin_path%" - exit /b 2 + exit /b 3 ) ) else echo - [WARN] Already exist: %%~ni ) echo. -rem Duplicates may be created during MSI extraction. del /q /s *.duplicate* >NUL 2>&1 - -cd /d "%origin_path%" - -del /q /s "%lessmsi_log%" >NUL 2>&1 +del /q /s lessmsi.log >NUL 2>&1 rd /q /s "%installers%" >NUL 2>&1 +cd /d "%origin_path%" endlocal diff --git a/sdk/extract-installers.bat b/sdk/extract-installers.bat index fb400f5..5bcbd60 100644 --- a/sdk/extract-installers.bat +++ b/sdk/extract-installers.bat @@ -1,33 +1,35 @@ @echo off rem -------------------------------------------------------------------------- -rem Extracts the Windows SDK Installers from the [ISO] to the [INSTALLERS] dir. +rem Extracts the Windows SDK Installers from the [iso] to the [installers] dir. rem rem Using: rem -rem extract-installers [ISO] [INSTALLERS] +rem extract-installers [iso] [installers] rem -------------------------------------------------------------------------- setlocal set iso=%~1 set installers=%~2 +set files_to_extract=Installers\*.msi Installers\*.cab call which 7z >NUL 2>&1 && ( echo|set/p=- Extracting SDK Installers ... - set files=Installers\*.msi Installers\*.cab - 7z e -y -o"%installers%" "%iso%" -r %files% >NUL || ( + 7z e -y -o"%installers%" "%iso%" -r %files_to_extract% >NUL || ( + echo FAIL + exit /b 1 + ) + call check-installers "%installers%" || ( echo FAIL exit /b 1 ) echo OK - goto :END + exit /b 0 ) -call extract-installers-from-drive "%iso%" "%installers%" || exit /b 1 - -:END -del /q /s "%iso%" >NUL 2>&1 || echo [WARN][%~n0] Unable to delete "%iso%"! -exit /b 0 +call extract-installers-from-drive "%iso%" "%installers%" ^ + && exit /b 0 ^ + || exit /b 1 endlocal diff --git a/utils/destination.bat b/utils/destination.bat index 4e26cba..f9bf8eb 100644 --- a/utils/destination.bat +++ b/utils/destination.bat @@ -1,7 +1,8 @@ @echo off rem -------------------------------------------------------------------------- -rem Sets the %destination% var and creates the associated dir if needed. +rem Sets the %destination% var without trailing backslash and creates the +rem associated dir if needed. rem -------------------------------------------------------------------------- set destination=%~1 diff --git a/utils/ensure-archiver.bat b/utils/ensure-archiver.bat index 79b4dd2..50524e6 100644 --- a/utils/ensure-archiver.bat +++ b/utils/ensure-archiver.bat @@ -76,5 +76,5 @@ echo. endlocal :SET_PATH -set "PATH=%~1;%PATH%" +set "PATH=%~f1;%PATH%" exit /b 0 diff --git a/utils/ensure-lessmsi.bat b/utils/ensure-lessmsi.bat index 502d8c2..c6adcb5 100644 --- a/utils/ensure-lessmsi.bat +++ b/utils/ensure-lessmsi.bat @@ -5,8 +5,7 @@ rem Third-party util for extracting *.MSI content without its installation. rem rem Using: rem -rem ensure-lessmsi [DESTINATION] -rem +rem ensure-lessmsi [destination] rem -------------------------------------------------------------------------- call which lessmsi >NUL 2>&1 && exit /b 0 @@ -49,5 +48,5 @@ echo. endlocal :SET_PATH -set "PATH=%~1;%PATH%" +set "PATH=%~f1;%PATH%" exit /b 0