Skip to content

Commit 259fd1e

Browse files
Final touches
1 parent fdf71ae commit 259fd1e

File tree

1 file changed

+50
-32
lines changed

1 file changed

+50
-32
lines changed

cli_changer.bat

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ rem | User Defined Variable(s) |
77
rem +------------------------------------------------+
88

99
rem WampServer custom install path.
10-
set $customInstallPath=
10+
rem Note: Trailing slash is not required.
11+
set $customInstallPath=E:\WampServer64
1112

1213

1314

@@ -52,9 +53,6 @@ if "%1" neq "" (
5253
set $cliSessionMode=1
5354
)
5455
) else (
55-
rem Hack to define a backspace so the 'set /p' command can be offset from the windows edge.
56-
for /F %%a in ('"prompt $H &echo on &for %%b in (1) do rem"') do set backspace=%%a
57-
5856
rem TUI mode in use.
5957
cls
6058
title WampServer PHP CLI Version Changer v%$scriptVersion%
@@ -101,7 +99,7 @@ rem -----------------------
10199
rem Check PHP Folder Path
102100
rem -----------------------
103101

104-
rem Set the path to the PHP folders.
102+
rem Set the absolute path to the PHP folders.
105103
if %$installPath:~-1% neq \ (
106104
set $pathToPhpFolders=%$installPath%\%$pathToPhpFolders%
107105
) else (
@@ -132,14 +130,15 @@ rem ----------------------------
132130
rem Explode Environmental Path
133131
rem ----------------------------
134132

135-
rem Get the correctly referenced path string.
133+
rem Get the correctly referenced environmental path.
136134
if %$cliSessionMode% equ 0 (
137135
rem Get the 'users' environmental path.
138136
for /F "usebackq tokens=2,*" %%a in (`reg.exe query HKCU\Environment /v PATH`) do (
139137
set $pathString=%%b
140138
)) else (
141-
rem Get the command windows 'session' environmental path.
142-
rem This path is a combination of the system environmental path and the user environmental path.
139+
rem Get the command window 'session' environmental path.
140+
rem Note: This path is a combination of the system environmental
141+
rem path and the user environmental path.
143142
set $pathString=%Path%
144143
)
145144

@@ -163,8 +162,9 @@ rem ----------------------------
163162
rem Find Active PHP Version(s)
164163
rem ----------------------------
165164

166-
rem If there is more than one PHP reference in the path, the operating system will
167-
rem only use the first one. Therefore, we only need to match the first one.
165+
rem As the operating system only uses the first found PHP reference in the environmental path, then we will as well.
166+
rem Note: If a PHP version other than an installed version is found, it will not be shown as an option,
167+
rem though it will be removed from the environmental path when the newly selected version is added.
168168
set $currentPhpVersionId=0
169169

170170
rem Iterate through the path array.
@@ -204,14 +204,17 @@ if %$cliMode% equ 1 (
204204
)
205205
)
206206

207-
rem Bypass displaying anything.
207+
rem Bypass displaying the TUI.
208208
goto checkUserInput
209209
)
210210

211211

212-
rem ----------------------
213-
rem Display PHP Versions
214-
rem ----------------------
212+
rem ------------------
213+
rem Operation by TUI
214+
rem ------------------
215+
216+
rem Hack to define a backspace so the 'set /p' command can be offset from the windows edge.
217+
for /F %%a in ('"prompt $H &echo on &for %%b in (1) do rem"') do set backspace=%%a
215218

216219
rem Show the header.
217220
echo:
@@ -230,7 +233,7 @@ for /L %%a in (1,1,%$availablePhpCount%) do (
230233
)
231234
)
232235

233-
rem Prompt the user to make a new selection.
236+
rem Prompt the user to make a selection.
234237
echo:
235238
set /p $newSelectionId=%backspace% Selection (1-%$availablePhpCount%):
236239
echo:
@@ -256,7 +259,7 @@ rem --------------------------
256259
rem Implode Environment Path
257260
rem --------------------------
258261

259-
rem Rebuild the path string while excluding any and all found PHP path.
262+
rem Rebuild the path string while excluding any and all found PHP paths.
260263
set "$pathString="
261264

262265
rem Iterate through the path array.
@@ -277,7 +280,7 @@ for /L %%a in (1,1,%$pathArrayCount%) do (
277280
rem Check the last segment for a matching regex expression. IE: Any PHP folder.
278281
echo !$lastSegment! | findstr /R /C:"^php[1-9][0-9]*\.[0-9][0-9]*\.*[0-9]*[0-9]*" >nul
279282

280-
rem If a match is not found, append the path to the path string.
283+
rem If a match is not found, append the path to the path string and include a trailing semicolon.
281284
if !errorlevel! neq 0 (
282285
set $pathString=!$pathString!!$pathArray[%%a]!;
283286
)
@@ -289,6 +292,9 @@ rem Add Chosen PHP Path
289292
rem ---------------------
290293

291294
rem Add the selected PHP folder path to the end of the path string.
295+
rem Note: Final path in environmental path not to include a trailing semicolon.
296+
rem Adding selected PHP folder path to front of environmental path would
297+
rem speed-up discoverability but unnecessarily complicate implosion.
292298
set $pathString=%$pathString%%$pathToPhpFolders%\!$availablePhpArray[%$newSelectionId%]!
293299

294300

@@ -299,25 +305,27 @@ rem ----------------------------
299305
rem Check if the CLI 'session' mode is being used.
300306
if %$cliSessionMode% equ 1 (
301307

302-
rem Message must come first as we loose reference to all variables.
308+
rem Show the success message.
309+
rem Note: Message must come first else we will loose
310+
rem reference to newly selected PHP array value.
303311
call :sessionUpdateSuccessful
304312

305-
rem Set the sessions environmental path variable.
313+
rem Set the 'session' environmental path variable.
306314
endlocal && set "Path=%$pathString%" >nul
307315

308316
exit /B 0
309317
) else (
310318

311319
rem Set the user environmental path variable.
312-
setx path "%$pathString%" >nul
320+
setx Path "%$pathString%" >nul
313321

314322
rem Show the successful message.
315323
goto updateSuccessful
316324
)
317325

318326

319327
rem ====================================================================================================================
320-
rem Success Message
328+
rem Success Messages
321329
rem ====================================================================================================================
322330

323331
rem -------------------
@@ -333,7 +341,8 @@ if %$cliMode% equ 0 (
333341
pause >nul
334342
exit 0
335343
) else (
336-
echo Success - The PHP CLI version is now !$availablePhpArray[%$newSelectionId%]!
344+
echo:
345+
echo Success - The PHP CLI version is now !$availablePhpArray[%$newSelectionId%]!
337346
exit /B 0
338347
)
339348

@@ -343,7 +352,8 @@ rem Session update successful
343352
rem ---------------------------
344353
:sessionUpdateSuccessful
345354

346-
echo Success: This sessions PHP CLI version is now !$availablePhpArray[%$newSelectionId%]!
355+
echo:
356+
echo Success: This sessions PHP CLI version is now !$availablePhpArray[%$newSelectionId%]!
347357

348358
exit /B
349359

@@ -365,7 +375,8 @@ if %$cliMode% equ 0 (
365375
pause >nul
366376
exit 0
367377
) else (
368-
echo Notice: The PHP CLI version remains unchanged.
378+
echo:
379+
echo Notice: Current selection was given - The PHP CLI version remains unchanged.
369380
exit /B 0
370381
)
371382

@@ -387,7 +398,8 @@ if %$cliMode% equ 0 (
387398
pause >nul
388399
exit 1
389400
) else (
390-
echo Failure: An invalid php version was given - The PHP CLI version remains unchanged.
401+
echo:
402+
echo Failure: An invalid php version was given - The PHP CLI version remains unchanged.
391403
exit /B 1
392404
)
393405

@@ -410,7 +422,8 @@ if %$cliMode% equ 0 (
410422
pause >nul
411423
exit 1
412424
) else (
413-
echo Error: The $customInstallPath path "%$customInstallPath%" does not exist.
425+
echo:
426+
echo Error: The $customInstallPath path "%$customInstallPath%" does not exist.
414427
exit /B 1
415428
)
416429

@@ -433,10 +446,13 @@ if %$cliMode% equ 0 (
433446
pause >nul
434447
exit 1
435448
) else (
436-
echo Error: Neither of the default installation paths exists.
437-
echo 1. %$defaultInstallPath[0]%
438-
echo 2. %$defaultInstallPath[1]%
439-
echo WampServer does not appear to be installed.
449+
echo:
450+
echo Error: Neither of the default installation paths exists.
451+
echo:
452+
echo 1. %$defaultInstallPath[0]%
453+
echo 2. %$defaultInstallPath[1]%
454+
echo:
455+
echo WampServer does not appear to be installed.
440456
exit /B 1
441457
)
442458

@@ -457,7 +473,9 @@ if %$cliMode% equ 0 (
457473
pause >nul
458474
exit 1
459475
) else (
460-
echo Error: The $pathToPhpFolders path "%$pathToPhpFolders%" does not exist.
461-
echo See the WampServer website for help.
476+
echo:
477+
echo Error: The $pathToPhpFolders path "%$pathToPhpFolders%" does not exist.
478+
echo:
479+
echo See the WampServer website for help.
462480
exit /B 1
463481
)

0 commit comments

Comments
 (0)