Skip to content

Commit c6edbc0

Browse files
authored
Merge pull request #2311 from daxgames/fix_path_enh_recurs
Fix and enhance enhance_path_recursive
2 parents c0eeec6 + 8781779 commit c6edbc0

File tree

6 files changed

+97
-37
lines changed

6 files changed

+97
-37
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
bin/*
55
!bin/Readme.md
6+
opt/*
7+
!opt/Readme.md
68

79
vendor/*/*
810
!vendor/bin/*

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,29 @@ The Cmder's user interface is also designed to be more eye pleasing, and you can
2323
### Shared Cmder install with Non-Portable Individual User Config
2424
1. Download the [latest release](https://github.com/cmderdev/cmder/releases/)
2525
2. Extract the archive to a shared location.
26-
3. (optional) Place your own executable files into the `%cmder_root%\bin` folder to be injected into your PATH.
27-
4. (optional) Create `%userprofile%\cmder_config\bin` folder to be injected into individual users PATH. Default is to auto create this on first run.
28-
5. (optional) Place your own executable files into the `%userprofile%\cmder_config\bin` folder to be injected into your PATH.
29-
6. Run `Cmder.exe` with `/C` command line argument. Example: `cmder.exe /C %userprofile%\cmder_config`
26+
3. (optional) Place your own executable files and custom app folders into the `%cmder_root%\bin`. See: [opt/README.md](./bin/README)
27+
- This folder to be injected into your PATH by default.
28+
- See `/max_depth [1-5]` in 'Command Line Arguments for `init.bat`' table to add subdirectories recursively.
29+
4. (optional) Place your own custom app folders into the `%cmder_root%\opt`. See: [opt/README.md](./opt/README)
30+
- This folder will NOT be injected into your PATH so you have total control of what gets added.
31+
5. Run `Cmder.exe` with `/C` command line argument. Example: `cmder.exe /C %userprofile%\cmder_config`
3032
* This will create the following directory structure if it is missing.
3133

3234
```
3335
c:\users\[username]\cmder_config
3436
├───bin
35-
└───config
36-
└───profile.d
37+
├───config
38+
│ └───profile.d
39+
└───opt
3740
```
3841
42+
- (optional) Place your own executable files and custom app folders into `%userprofile%\cmder_config\bin`.
43+
- This folder to be injected into your PATH by default.
44+
- See `/max_depth [1-5]` in 'Command Line Arguments for `init.bat`' table to add subdirectories recursively.
45+
- (optional) Place your own custom app folders into the `%user_profile%\cmder_config\opt`.
46+
- This folder will NOT be injected into your PATH so you have total control of what gets added.
47+
48+
3949
* Both the shared install and the individual user config locations can contain a full set of init and profile.d scripts enabling shared config with user overrides. See below.
4050
4151
## Cmder.exe Command Line Arguments

opt/Readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Bin
2+
3+
This folder is for optional user packages and will not be automatically injected into the PATH.
4+
5+
Use `%lib_path% enhance_path "%cmder_root%\[path to folder]"` in `%cmder_root%\config\user_profile.cmd` or `%cmder_root%\config\profile.d\*.cmd` to add to the path.

vendor/bin/cexec.cmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ set "currenArgu=%~1"
1919
if /i "%currenArgu%" equ "/setPath" (
2020
:: set %flag_exists% shortcut
2121
endlocal
22-
set "ccall=call %~dp0cexec.cmd"
23-
set "cexec=%~dp0cexec.cmd"
22+
set "ccall=call ^"%~dp0cexec.cmd^""
23+
set "cexec=^"%~dp0cexec.cmd^""
2424
) else if /i "%currenArgu%" == "/?" (
2525
goto :help
2626
) else if /i "%currenArgu%" equ "/help" (

vendor/init.bat

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@ set CMDER_INIT_START=%time%
99
:: !!! Use "%CMDER_ROOT%\config\user_profile.cmd" to add your own startup commands
1010

1111
:: Use /v command line arg or set to > 0 for verbose output to aid in debugging.
12-
set verbose_output=0
13-
set debug_output=0
14-
set time_init=0
15-
set fast_init=0
16-
set max_depth=1
17-
:: Add *nix tools to end of path. 0 turns off *nix tools.
18-
set nix_tools=1
12+
if not defined verbose_output set verbose_output=0
13+
14+
:: Use /d command line arg or set to 1 for debug output to aid in debugging.
15+
if not defined debug_output set debug_output=0
16+
17+
:: Use /t command line arg or set to 1 to display init time.
18+
if not defined time_init set time_init=0
19+
20+
:: Use /f command line arg to speed up init at the expense of some functionality.
21+
if not defined fast_init set fast_init=0
22+
23+
:: Use /max_depth 1-5 to set max recurse depth for calls to `enhance_path_recursive`
24+
if not defined max_depth set max_depth=1
25+
26+
:: Add *nix tools to end of path. 0 turns off *nix tools, 2 adds *nix tools to the front of thr path.
27+
if not defined nix_tools set nix_tools=1
28+
1929
set "CMDER_USER_FLAGS= "
2030

2131
:: Find root dir
@@ -120,6 +130,8 @@ goto var_loop
120130

121131
if defined CMDER_USER_CONFIG (
122132
%lib_console% debug_output init.bat "CMDER IS ALSO USING INDIVIDUAL USER CONFIG FROM '%CMDER_USER_CONFIG%'!"
133+
134+
if not exist "%CMDER_USER_CONFIG%\opt" md "%CMDER_USER_CONFIG%\opt"
123135
)
124136

125137
:: Pick right version of clink
@@ -154,6 +166,12 @@ if "%CMDER_CLINK%" == "1" (
154166
%lib_console% verbose_output "WARNING: Incompatible 'ComSpec/Shell' Detetected Skipping Clink Injection!"
155167
)
156168

169+
if "%CMDER_CONFIGURED%" == "1" (
170+
echo Cmder is already configured, skipping Cmder Init!
171+
172+
goto CMDER_CONFIGURED
173+
)
174+
157175
:: Prepare for git-for-windows
158176

159177
:: I do not even know, copypasted from their .bat
@@ -261,9 +279,11 @@ endlocal
261279

262280
:PATH_ENHANCE
263281
%lib_path% enhance_path "%CMDER_ROOT%\vendor\bin"
264-
%lib_path% enhance_path_recursive "%CMDER_ROOT%\bin" %max_depth%
282+
283+
:USER_CONFIG_START
284+
%lib_path% enhance_path_recursive "%CMDER_ROOT%\bin" 0 %max_depth%
265285
if defined CMDER_USER_BIN (
266-
%lib_path% enhance_path_recursive "%CMDER_USER_BIN%" %max_depth%
286+
%lib_path% enhance_path_recursive "%CMDER_USER_BIN%" 0 %max_depth%
267287
)
268288
%lib_path% enhance_path "%CMDER_ROOT%" append
269289

@@ -367,6 +387,8 @@ if "%CMDER_ALIASES%" == "1" if exist "%CMDER_ROOT%\bin\alias.bat" if exist "%CMD
367387
)
368388

369389
set initialConfig=
390+
391+
:CMDER_CONFIGURED
370392
set CMDER_CONFIGURED=1
371393

372394
set CMDER_INIT_END=%time%

vendor/lib/lib_path.cmd

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,26 @@ exit /b
5151
set "position="
5252
)
5353

54+
dir "%add_path%" | findstr -i "\.COM \.EXE \.BAT \.CMD \.PS1 \.VBS" >NUL
55+
if "%ERRORLEVEL%" == "0" (
56+
set "add_to_path=%add_path%"
57+
) else (
58+
set "add_to_path="
59+
)
60+
5461
if "%fast_init%" == "1" (
5562
if "%position%" == "append" (
56-
set "PATH=%PATH%;%add_path%"
63+
set "PATH=%PATH%;%add_to_path%"
5764
) else (
58-
set "PATH=%add_path%;%PATH%"
65+
set "PATH=%add_to_path%;%PATH%"
5966
)
6067
goto :end_enhance_path
68+
) else if "add_to_path" equ "" (
69+
goto :end_enhance_path
6170
)
6271

6372
set found=0
64-
set "find_query=%add_path%"
73+
set "find_query=%add_to_path%"
6574
set "find_query=%find_query:\=\\%"
6675
set "find_query=%find_query: =\ %"
6776
set OLD_PATH=%PATH%
@@ -90,11 +99,11 @@ exit /b
9099

91100
if "%found%" == "0" (
92101
if /i "%position%" == "append" (
93-
%lib_console% debug_output :enhance_path "Appending '%add_path%'"
94-
set "PATH=%PATH%;%add_path%"
102+
%lib_console% debug_output :enhance_path "Appending '%add_to_path%'"
103+
set "PATH=%PATH%;%add_to_path%"
95104
) else (
96-
%lib_console% debug_output :enhance_path "Prepending '%add_path%'"
97-
set "PATH=%add_path%;%PATH%"
105+
%lib_console% debug_output :enhance_path "Prepending '%add_to_path%'"
106+
set "PATH=%add_to_path%;%PATH%"
98107
)
99108

100109
set found=1
@@ -150,52 +159,64 @@ exit /b
150159
exit 1
151160
)
152161

153-
if "%~2" gtr "1" (
154-
set "max_depth=%~2"
162+
set "depth=%~2"
163+
set "max_depth=%~3"
164+
165+
if "%~4" neq "" if /i "%~4" == "append" (
166+
set "position=%~4"
155167
) else (
156-
set "max_depth=1"
168+
set "position="
157169
)
158170

159-
if "%~3" neq "" if /i "%~3" == "append" (
160-
set "position=%~3"
171+
dir "%add_path%" | findstr -i "\.COM \.EXE \.BAT \.CMD \.PS1 \.VBS" >NUL
172+
if "%ERRORLEVEL%" == "0" (
173+
set "add_to_path=%add_path%"
161174
) else (
162-
set "position="
175+
set "add_to_path="
163176
)
164177

165178
if "%fast_init%" == "1" (
166-
call :enhance_path "%add_path%" %position%
179+
if "%add_to_path%" neq "" (
180+
call :enhance_path "%add_to_path%" %position%
181+
)
167182
)
168183

169184
set "PATH=%PATH:;;=;%"
170185
if "%fast_init%" == "1" (
171186
exit /b
172187
)
173188

174-
if "%depth%" == "" set depth=0
175-
176-
%lib_console% debug_output :enhance_path_recursive "Env Var - add_path=%add_path%"
189+
%lib_console% debug_output :enhance_path_recursive "Env Var - add_path=%add_to_path%"
177190
%lib_console% debug_output :enhance_path_recursive "Env Var - position=%position%"
191+
%lib_console% debug_output :enhance_path_recursive "Env Var - depth=%depth%"
178192
%lib_console% debug_output :enhance_path_recursive "Env Var - max_depth=%max_depth%"
179193

180194
if %max_depth% gtr %depth% (
181-
%lib_console% debug_output :enhance_path_recursive "Adding parent directory - '%add_path%'"
182-
call :enhance_path "%add_path%" %position%
195+
if "%add_to_path%" neq "" (
196+
%lib_console% debug_output :enhance_path_recursive "Adding parent directory - '%add_to_path%'"
197+
call :enhance_path "%add_to_path%" %position%
198+
)
183199
call :set_depth
184200
call :loop_depth
185201
)
186202

187203
set "PATH=%PATH%"
204+
188205
exit /b
189206

190207
: set_depth
191208
set /a "depth=%depth%+1"
192209
exit /b
193210

194211
:loop_depth
212+
if %depth% == %max_depth% (
213+
exit /b
214+
)
215+
195216
for /d %%i in ("%add_path%\*") do (
196217
%lib_console% debug_output :enhance_path_recursive "Env Var BEFORE - depth=%depth%"
197218
%lib_console% debug_output :enhance_path_recursive "Found Subdirectory - '%%~fi'"
198-
call :enhance_path_recursive "%%~fi" %max_depth% %position%
219+
call :enhance_path_recursive "%%~fi" %depth% %max_depth% %position%
199220
%lib_console% debug_output :enhance_path_recursive "Env Var AFTER- depth=%depth%"
200221
)
201222
exit /b

0 commit comments

Comments
 (0)