-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli_changer.bat
481 lines (369 loc) · 13.1 KB
/
cli_changer.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
@echo off
setlocal EnableDelayedExpansion EnableExtensions
rem +------------------------------------------------+
rem | User Defined Variable(s) |
rem +------------------------------------------------+
rem WampServer custom install path.
rem Note: Trailing slash is not required.
set $customInstallPath=
rem +------------------------------------------------+
rem | DO NOT EDIT BELOW THIS LINE |
rem +------------------------------------------------+
rem -------------------
rem Default Variables
rem -------------------
set $scriptVersion=1.3.1
set $defaultInstallPath[0]=C:\wamp
set $defaultInstallPath[1]=C:\wamp64
set $pathToPhpFolders=bin\php
set $cliMode=0
set $cliSessionMode=0
set $colorNormal=08
set $colorSuccess=0A
set $colorWarning=0E
set $colorFailure=0C
rem -------------------------
rem Check Mode of Operation
rem -------------------------
rem Check mode of operation.
if "%1" neq "" (
rem CLI Mode in use.
set $cliMode=1
rem Check if CLI session mode is in use.
if "%2"=="-t" (
set $cliSessionMode=1
)
if "%2"=="--temp" (
set $cliSessionMode=1
)
) else (
rem TUI mode in use.
cls
title WampServer PHP CLI Version Changer v%$scriptVersion%
color %$colorNormal%
)
rem --------------------------
rem Check Installation Paths
rem --------------------------
rem Test for a custom install path.
if defined $customInstallPath (
rem Check if the folder exists.
if not exist "%$customInstallPath%" goto invalidCustomInstallPathGiven
set $installPath=%$customInstallPath%
)
rem Test for the first default install path.
if not defined $installPath (
rem Check if the first default install path exists.
if exist %$defaultInstallPath[0]% (
set $installPath=%$defaultInstallPath[0]%
)
)
rem Test for the second default install path.
if not defined $installPath (
rem Check if the second default install path exists.
if exist %$defaultInstallPath[1]% (
set $installPath=%$defaultInstallPath[1]%
)
)
rem Exit if unable to find installation path.
if not defined $installPath goto defaultInstallPathsMissing
rem -----------------------
rem Check PHP Folder Path
rem -----------------------
rem Set the absolute path to the PHP folders.
if %$installPath:~-1% neq \ (
set $pathToPhpFolders=%$installPath%\%$pathToPhpFolders%
) else (
set $pathToPhpFolders=%$installPath%%$pathToPhpFolders%
)
rem Check the path to the PHP folders exists.
if not exist "%$pathToPhpFolders%" goto invalidPathToPhpFoldersGiven
rem ---------------------
rem Get Available PHP's
rem ---------------------
rem Get a list of available PHP's.
set counter=0
for /F "delims=" %%a in ('dir %$pathToPhpFolders% /AD /B') do (
set /A counter=counter+1
set $availablePhpArray[!counter!]=%%a
)
rem Get the total number of elements in the available PHP array.
set $availablePhpCount=!counter!
rem ----------------------------
rem Explode Environmental Path
rem ----------------------------
rem Get the correctly referenced environmental path.
if %$cliSessionMode% equ 0 (
rem Get the 'users' environmental path.
for /F "usebackq tokens=2,*" %%a in (`reg.exe query HKCU\Environment /v PATH`) do (
set $pathString=%%b
)) else (
rem Get the command window 'session' environmental path.
rem Note: This path is a combination of the system environmental
rem path and the user environmental path.
set $pathString=%Path:)=^)%
)
rem Explode the path string into an array.
set counter=0
:explode
for /F "tokens=1* delims=;" %%a in ("%$pathString%") do (
set /A counter=counter+1
set $pathArray[!counter!]=%%a
set $pathString=%%b
)
if defined $pathString goto explode
rem Get the total number of elements in the path array.
set $pathArrayCount=!counter!
rem ----------------------------
rem Find Active PHP Version(s)
rem ----------------------------
rem As the operating system only uses the first found PHP reference in the environmental path, then we will as well.
rem Note: If a PHP version other than an installed version is found, it will not be shown as an option,
rem though it will be removed from the environmental path when the newly selected version is added.
set $currentPhpVersionId=0
rem Iterate through the path array.
for /L %%a in (1,1,%$pathArrayCount%) do (
rem Iterate through the available PHP's array.
for /L %%b in (1,1,%$availablePhpCount%) do (
rem Check if the path string matches the (combined) full path of the available PHP version string.
if "!$pathArray[%%a]!"=="%$pathToPhpFolders%\!$availablePhpArray[%%b]!" (
rem Force the 'for' command parameters into type 'integer'.
set /A $currentPhpVersionId=currentPhpVersionId+%%b
goto break
)
)
)
:break
rem ------------------
rem Operation by CLI
rem ------------------
rem Check if the CLI is being used.
if %$cliMode% equ 1 (
rem Set the newly selected id.
set $newSelectionId=0
rem Iterate through the available PHP versions array.
for /L %%a in (1,1,%$availablePhpCount%) do (
rem If a matching installed PHP folder name is found, set the new selection id.
if "%1"=="!$availablePhpArray[%%a]!" (
set $newSelectionId=%%a
)
)
rem Bypass displaying the TUI.
goto checkUserInput
)
rem ------------------
rem Operation by TUI
rem ------------------
rem Hack to define a backspace so the 'set /p' command can be offset from the windows edge.
for /F %%a in ('"prompt $H &echo on &for %%b in (1) do rem"') do set backspace=%%a
rem Show the header.
echo:
echo Available PHP CLI Versions
echo --------------------------
echo:
rem Iterate though the available PHP versions array.
for /L %%a in (1,1,%$availablePhpCount%) do (
rem Check if the listed version matches the current version.
if %%a equ %$currentPhpVersionId% (
echo %%a - !$availablePhpArray[%%a]! - Current
) else (
echo %%a - !$availablePhpArray[%%a]!
)
)
rem Prompt the user to make a selection.
echo:
set /p $newSelectionId=%backspace% Selection (1-%$availablePhpCount%):
echo:
rem ------------------
rem Check User Input
rem ------------------
:checkUserInput
rem Check if the new selection comprises of digits.
echo %$newSelectionId%| findstr /R "^[1-9][0-9]*$" >nul
if %errorlevel% neq 0 goto invalidSelectionGiven
rem Check if the new selection is a valid selection.
if %$newSelectionId% gtr %$availablePhpCount% goto invalidSelectionGiven
rem Check if the new selection is the same as the current selection.
if %$newSelectionId% equ %$currentPhpVersionId% goto currentSelectionGiven
rem --------------------------
rem Implode Environment Path
rem --------------------------
rem Rebuild the path string while excluding any and all found PHP paths.
set "$pathString="
rem Iterate through the path array.
for /L %%a in (1,1,%$pathArrayCount%) do (
rem Remove any trailing slash.
if !$pathArray[%%a]:~-1! equ \ (
set $path=!$pathArray[%%a]:~0,-1!
) else (
set $path=!$pathArray[%%a]!
)
rem Get the last segment of the path.
for %%b in (!$path!) do (
set $lastSegment=%%~nxb
)
rem Check the last segment for a matching regex expression. IE: Any PHP folder.
echo !$lastSegment! | findstr /R /C:"^php[1-9][0-9]*\.[0-9][0-9]*\.*[0-9]*[0-9]*" >nul
rem If a match is not found, append the path to the path string and include a trailing semicolon.
if !errorlevel! neq 0 (
set $pathString=!$pathString!!$pathArray[%%a]!;
)
)
rem ---------------------
rem Add Chosen PHP Path
rem ---------------------
rem Add the selected PHP folder path to the end of the path string.
rem Note: Final path in environmental path not to include a trailing semicolon.
rem Adding selected PHP folder path to front of environmental path would
rem speed-up discoverability but unnecessarily complicate implosion.
set $pathString=%$pathString%%$pathToPhpFolders%\!$availablePhpArray[%$newSelectionId%]!
rem ----------------------------
rem Set The Environmental Path
rem ----------------------------
rem Check if the CLI 'session' mode is being used.
if %$cliSessionMode% equ 1 (
rem Show the success message.
rem Note: Message must come first else we will loose
rem reference to newly selected PHP array value.
call :sessionUpdateSuccessful
rem Set the 'session' environmental path variable.
endlocal && set "Path=%$pathString%" >nul
exit /B 0
) else (
rem Set the user environmental path variable.
setx Path "%$pathString%" >nul
rem Show the successful message.
goto updateSuccessful
)
rem ====================================================================================================================
rem Success Messages
rem ====================================================================================================================
rem -------------------
rem Update successful
rem -------------------
:updateSuccessful
if %$cliMode% equ 0 (
color %$colorSuccess%
echo Update Successful - The PHP CLI version is now !$availablePhpArray[%$newSelectionId%]!
echo:
echo Press any key to exit.
pause >nul
exit 0
) else (
echo:
echo Success - The PHP CLI version is now !$availablePhpArray[%$newSelectionId%]!
exit /B 0
)
rem ---------------------------
rem Session update successful
rem ---------------------------
:sessionUpdateSuccessful
echo:
echo Success: This sessions PHP CLI version is now !$availablePhpArray[%$newSelectionId%]!
exit /B
rem ====================================================================================================================
rem Notice Message
rem ====================================================================================================================
rem -------------------------
rem Current selection given
rem -------------------------
:currentSelectionGiven
if %$cliMode% equ 0 (
color %$colorSuccess%
echo Current selection was given - The PHP CLI version remains unchanged.
echo:
echo Press any key to exit.
pause >nul
exit 0
) else (
echo:
echo Notice: Current selection was given - The PHP CLI version remains unchanged.
exit /B 0
)
rem ====================================================================================================================
rem Failure Message
rem ====================================================================================================================
rem -------------------------
rem Invalid selection given
rem -------------------------
:invalidSelectionGiven
if %$cliMode% equ 0 (
color %$colorWarning%
echo An invalid selection was given - The PHP CLI version remains unchanged.
echo:
echo Press any key to exit.
pause >nul
exit 1
) else (
echo:
echo Failure: An invalid php version was given - The PHP CLI version remains unchanged.
exit /B 1
)
rem ====================================================================================================================
rem Error Messages
rem ====================================================================================================================
rem -----------------------------------
rem Invalid custom install path given
rem -----------------------------------
:invalidCustomInstallPathGiven
if %$cliMode% equ 0 (
color %$colorFailure%
echo:
echo The $customInstallPath path "%$customInstallPath%" does not exist.
echo:
echo Press any key to exit.
pause >nul
exit 1
) else (
echo:
echo Error: The $customInstallPath path "%$customInstallPath%" does not exist.
exit /B 1
)
rem -------------------------------
rem Default install paths missing
rem -------------------------------
:defaultInstallPathsMissing
if %$cliMode% equ 0 (
color %$colorFailure%
echo:
echo Neither of the default installation paths exists.
echo:
echo 1. %$defaultInstallPath[0]%
echo 2. %$defaultInstallPath[1]%
echo:
echo WampServer does not appear to be installed.
echo:
echo Press any key to exit.
pause >nul
exit 1
) else (
echo:
echo Error: Neither of the default installation paths exists.
echo:
echo 1. %$defaultInstallPath[0]%
echo 2. %$defaultInstallPath[1]%
echo:
echo WampServer does not appear to be installed.
exit /B 1
)
rem ----------------------------------
rem Invalid path to PHP folder given
rem ----------------------------------
:invalidPathToPhpFoldersGiven
if %$cliMode% equ 0 (
color %$colorFailure%
echo:
echo The $pathToPhpFolders path "%$pathToPhpFolders%" does not exist.
echo:
echo See the WampServer website for help.
echo:
echo Press any key to exit.
pause >nul
exit 1
) else (
echo:
echo Error: The $pathToPhpFolders path "%$pathToPhpFolders%" does not exist.
echo:
echo See the WampServer website for help.
exit /B 1
)