cmder/vendor/lib/lib_path.cmd

243 lines
6.3 KiB
Batchfile
Raw Normal View History

@echo off
call "%~dp0lib_base.cmd"
call "%%~dp0lib_console"
set lib_path=call "%~dp0lib_path.cmd"
if "%~1" == "/h" (
%lib_base% help "%~0"
) else if "%1" neq "" (
call :%*
)
exit /b
:enhance_path
:::===============================================================================
:::enhance_path - Add a directory to the path env variable if required.
:::
:::include:
:::
::: call "lib_path.cmd"
:::
:::usage:
:::
::: %lib_path% enhance_path "[dir_path]" [append]
:::
:::required:
:::
::: [dir_path] <in> Fully qualified directory path. Ex: "c:\bin"
:::
:::options:
:::
::: append <in> Append to the path env variable rather than pre-pend.
2020-07-25 17:11:34 +08:00
::B
:::
:::output:
:::
::: path <out> Sets the path env variable if required.
:::-------------------------------------------------------------------------------
if "%~1" neq "" (
set "add_path=%~1"
) else (
2020-12-08 03:41:11 +08:00
%print_error% "You must specify a directory to add to the path!"
exit 1
)
if "%~2" neq "" if /i "%~2" == "append" (
set "position=%~2"
) else (
set "position="
)
2020-04-30 00:03:24 +08:00
dir "%add_path%" | findstr -i "\.COM \.EXE \.BAT \.CMD \.PS1 \.VBS" >NUL
if "%ERRORLEVEL%" == "0" (
set "add_to_path=%add_path%"
) else (
set "add_to_path="
)
if "%fast_init%" == "1" (
if "%position%" == "append" (
2020-04-30 00:03:24 +08:00
set "PATH=%PATH%;%add_to_path%"
) else (
2020-04-30 00:03:24 +08:00
set "PATH=%add_to_path%;%PATH%"
)
2019-12-05 05:04:16 +08:00
goto :end_enhance_path
2020-04-30 00:03:24 +08:00
) else if "add_to_path" equ "" (
goto :end_enhance_path
)
2018-09-16 23:18:10 +08:00
set found=0
2020-04-30 00:03:24 +08:00
set "find_query=%add_to_path%"
set "find_query=%find_query:\=\\%"
set "find_query=%find_query: =\ %"
2021-11-07 23:54:33 +08:00
set "OLD_PATH=%PATH%"
2019-12-05 05:04:16 +08:00
setlocal enabledelayedexpansion
2020-08-23 20:42:58 +08:00
if "!found!" == "0" (
echo "!path!"|!WINDIR!\System32\findstr >nul /I /R /C:";!find_query!;"
2019-10-14 03:47:25 +08:00
call :set_found
2018-09-16 23:18:10 +08:00
)
2020-12-10 21:35:40 +08:00
%print_debug% :enhance_path "Env Var INSIDE PATH !find_query! - found=!found!"
2018-09-16 23:18:10 +08:00
2020-08-23 20:42:58 +08:00
if /i "!position!" == "append" (
2019-12-05 05:04:16 +08:00
if "!found!" == "0" (
2020-08-23 20:42:58 +08:00
echo "!path!"|!WINDIR!\System32\findstr >nul /I /R /C:";!find_query!\"$"
2019-12-05 05:04:16 +08:00
call :set_found
)
2020-12-10 21:35:40 +08:00
%print_debug% :enhance_path "Env Var END PATH !find_query! - found=!found!"
2019-12-05 05:04:16 +08:00
) else (
if "!found!" == "0" (
2020-08-23 20:42:58 +08:00
echo "!path!"|!WINDIR!\System32\findstr >nul /I /R /C:"^\"!find_query!;"
2019-12-05 05:04:16 +08:00
call :set_found
)
2020-12-10 21:35:40 +08:00
%print_debug% :enhance_path "Env Var BEGIN PATH !find_query! - found=!found!"
)
2019-12-05 05:04:16 +08:00
endlocal & set found=%found%
if "%found%" == "0" (
if /i "%position%" == "append" (
2020-12-08 03:41:11 +08:00
%print_debug% :enhance_path "Appending '%add_to_path%'"
2020-04-30 00:03:24 +08:00
set "PATH=%PATH%;%add_to_path%"
) else (
2020-12-08 03:41:11 +08:00
%print_debug% :enhance_path "Prepending '%add_to_path%'"
2020-04-30 00:03:24 +08:00
set "PATH=%add_to_path%;%PATH%"
)
2019-12-05 05:04:16 +08:00
set found=1
)
2019-12-05 05:04:16 +08:00
:end_enhance_path
2019-10-14 03:47:25 +08:00
set "PATH=%PATH:;;=;%"
REM echo %path%|"C:\Users\dgames\cmder - dev\vendor\git-for-windows\usr\bin\wc" -c
if "%fast_init%" == "1" exit /b
if not "%OLD_PATH:~0,3000%" == "%OLD_PATH:~0,3001%" goto :toolong
if not "%OLD_PATH%" == "%PATH%" goto :changed
exit /b
:toolong
2022-09-04 21:16:39 +08:00
echo "%OLD_PATH%">"%temp%\cmder_lib_pathA"
echo "%PATH%">"%temp%\cmder_lib_pathB"
fc /b "%temp%\cmder_lib_pathA" "%temp%\cmder_lib_pathB" 2>nul 1>nul
if errorlevel 1 ( del "%temp%\cmder_lib_pathA" & del "%temp%\cmder_lib_pathB" & goto :changed )
del "%temp%\cmder_lib_pathA" & del "%temp%\cmder_lib_pathB"
exit /b
:changed
2020-12-08 03:41:11 +08:00
%print_debug% :enhance_path "END Env Var - PATH=%path%"
%print_debug% :enhance_path "Env Var %find_query% - found=%found%"
exit /b
2019-10-14 03:47:25 +08:00
exit /b
2019-10-14 03:47:25 +08:00
:set_found
2019-12-05 05:04:16 +08:00
if "%ERRORLEVEL%" == "0" (
set found=1
)
exit /b
:enhance_path_recursive
:::===============================================================================
:::enhance_path_recursive - Add a directory and subs to the path env variable if
::: required.
:::.
:::include:
:::.
::: call "$0"
:::.
:::usage:
:::.
::: call "%~DP0lib_path" enhance_path_recursive "[dir_path]" [max_depth] [append]
:::.
:::required:
:::.
::: [dir_path] <in> Fully qualified directory path. Ex: "c:\bin"
:::.
:::options:
:::.
2022-01-16 00:00:58 +08:00
::: [max_depth] <in> Max recursion depth. Default: 1
:::.
::: append <in> Append instead to path env variable rather than pre-pend.
:::.
:::output:
:::.
::: path <out> Sets the path env variable if required.
:::-------------------------------------------------------------------------------
if "%~1" neq "" (
set "add_path=%~1"
) else (
2020-12-08 03:41:11 +08:00
%print_error% "You must specify a directory to add to the path!"
exit 1
)
2020-04-30 00:03:24 +08:00
set "depth=%~2"
set "max_depth=%~3"
if "%~4" neq "" if /i "%~4" == "append" (
set "position=%~4"
) else (
2020-04-30 00:03:24 +08:00
set "position="
)
2020-07-25 17:11:34 +08:00
dir "%add_path%" 2>NUL | findstr -i "\.COM \.EXE \.BAT \.CMD \.PS1 \.VBS" >NUL
2020-07-24 01:36:50 +08:00
2020-04-30 00:03:24 +08:00
if "%ERRORLEVEL%" == "0" (
set "add_to_path=%add_path%"
) else (
2020-04-30 00:03:24 +08:00
set "add_to_path="
)
if "%fast_init%" == "1" (
2020-04-30 00:03:24 +08:00
if "%add_to_path%" neq "" (
call :enhance_path "%add_to_path%" %position%
)
)
2019-10-13 22:07:23 +08:00
set "PATH=%PATH:;;=;%"
if "%fast_init%" == "1" (
exit /b
)
2020-12-08 03:41:11 +08:00
%print_debug% :enhance_path_recursive "Env Var - add_path=%add_to_path%"
%print_debug% :enhance_path_recursive "Env Var - position=%position%"
%print_debug% :enhance_path_recursive "Env Var - depth=%depth%"
%print_debug% :enhance_path_recursive "Env Var - max_depth=%max_depth%"
2019-10-14 03:47:25 +08:00
if %max_depth% gtr %depth% (
2020-04-30 00:03:24 +08:00
if "%add_to_path%" neq "" (
2020-12-08 03:41:11 +08:00
%print_debug% :enhance_path_recursive "Adding parent directory - '%add_to_path%'"
2020-04-30 00:03:24 +08:00
call :enhance_path "%add_to_path%" %position%
)
2019-10-14 03:47:25 +08:00
call :set_depth
call :loop_depth
)
2019-10-14 03:47:25 +08:00
set "PATH=%PATH%"
2020-04-30 00:03:24 +08:00
exit /b
2019-10-14 03:47:25 +08:00
: set_depth
set /a "depth=%depth%+1"
exit /b
:loop_depth
2020-04-30 00:03:24 +08:00
if %depth% == %max_depth% (
exit /b
)
2019-10-14 03:47:25 +08:00
for /d %%i in ("%add_path%\*") do (
2020-12-08 03:41:11 +08:00
%print_debug% :enhance_path_recursive "Env Var BEFORE - depth=%depth%"
%print_debug% :enhance_path_recursive "Found Subdirectory - '%%~fi'"
2020-04-30 00:03:24 +08:00
call :enhance_path_recursive "%%~fi" %depth% %max_depth% %position%
2020-12-08 03:41:11 +08:00
%print_debug% :enhance_path_recursive "Env Var AFTER- depth=%depth%"
2019-10-14 03:47:25 +08:00
)
exit /b