-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphp.bat
More file actions
364 lines (313 loc) · 10.5 KB
/
Copy pathphp.bat
File metadata and controls
364 lines (313 loc) · 10.5 KB
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
@ECHO off
rem /**
rem * main script
rem */
:main
SETLOCAL EnableDelayedExpansion
rem load all params as string
rem (this contains also parts like a=b, while access single params would deliver %1=a and %2=b without information which character is between that)
SET params=%*
SET basepath=%~dp0
SET versionsPath=!basepath!php\versions
SET globalVersionFile=!basepath!php\php.version
if exist "!globalVersionFile!" (
SET loadedGlobalVersionFile=!globalVersionFile!
)
if not exist "!globalVersionFile!" (
SET loadedGlobalVersionFile=!basepath!php\default.php.version
)
SET /P phpversion=<!loadedGlobalVersionFile!
SET /P currentVersion=<!basePath!AlternatePHP\VERSION
IF "%1" == "help" GOTO help
IF "%1" == "/?" GOTO help
IF "%1" == "license" GOTO license
IF "%1" == "versions" GOTO versions
IF "%1" == "install" GOTO install
IF "%1" == "global" GOTO global
IF "%1" == "rename" GOTO rename
IF "%1" == "self-update" GOTO selfUpdate
GOTO php
:outputVersionInformation
ECHO glady/AlternatePHP !currentVersion! (!basepath!)
EXIT /B 0
:header
ECHO.
CALL :outputVersionInformation
ECHO.
ECHO -------------------------- AlternatePHP --------------------------
ECHO AlternatePHP is a batch file for Windows environments that can
ECHO make different versions of php available. It is inspired by
ECHO phpenv / rbenv which is available on other operating systems.
ECHO.
ECHO Visit on GitHub: https://github.com/glady/AlternatePHP
ECHO ------------------------------------------------------------------
ECHO.
EXIT /B 0
rem /**
rem * Output of help information about this file
rem */
:help
CALL :header
ECHO current settings:
ECHO global: !phpversion! [defined in !loadedGlobalVersionFile!]
ECHO local : -not supported yet-
ECHO.
ECHO call scripts in a specific PHP version
ECHO php [version] [xdebug] script.php
ECHO version = optional, default version is used, when not given
ECHO xdebug = enables xdebug, if php_xdebug.dll is available within ext-folder of version, default disabled
ECHO.
ECHO supported wrapper actions:
ECHO php help [php /?] = This help output
ECHO php license = Show content of license file
ECHO php versions = List all installed php versions
ECHO php install x.y.z = Download and unzip php version x.y.z
ECHO php rename x.y.z alias = Rename php version x.y.z to alias (x.y.z can be alias itself)
ECHO php global x.y.z = Change global php version
ECHO php self-update = Self-Update AlternatePHP
ECHO php [x.y.z] [xdebug] composer ... = run latest composer.phar
GOTO shutdown
rem /**
rem * Output of license information about this file
rem */
:license
CALL :header
IF exist "!basepath!LICENSE" (
for /f "usebackq delims=" %%i in (!basepath!LICENSE) do ECHO %%i
)
IF not exist "!basepath!LICENSE" (
ECHO No LICENSE file found. @see https://github.com/glady/AlternatePHP/blob/master/LICENSE
)
GOTO shutdown
rem /**
rem * Find and call php version by first argument or by configured default version
rem */
:php
SET prepend=
CALL :trimStringLeft params "!params!"
rem shift first argument as version
IF exist "!versionsPath!\%1\php.exe" (
SET phpversion=%1
SHIFT
rem remove version from params
CALL :strlen !phpversion! length
CALL :removeFromStringLeft params "!params!" !length!
CALL :trimStringLeft params "!params!"
)
rem shift next (or first) argument named xdebug
IF [%1] == [xdebug] (
CALL :removeFromStringLeft params "!params!" 7
CALL :trimStringLeft params "!params!"
IF exist "!versionsPath!\!phpversion!\ext\php_xdebug.dll" (
rem prepend "-d" before remaining arguments
SET prepend=-d "zend_extension=!versionsPath!\!phpversion!\ext\php_xdebug.dll"
)
SHIFT
)
IF [%1] == [composer] (
CALL :removeFromStringLeft params "!params!" 9
CALL :trimStringLeft params "!params!"
CALL :initializeComposer
IF exist "!basePath!AlternatePHP\composer.phar" (
SET params="!basePath!\AlternatePHP\composer.phar" !params!
)
SHIFT
)
rem call specific php
IF not exist "!versionsPath!\!phpversion!\php.exe" (
ECHO PHP !phpversion! is currently not installed
ECHO.
GOTO help
)
if "!params:~0,2!" == "-v" (
CALL :outputVersionInformation
)
if "!params:~0,9!" == "--version" (
CALL :outputVersionInformation
)
"!versionsPath!\!phpversion!\php.exe" !prepend! !params!
GOTO shutdown
rem /**
rem * Retrieve a new php version
rem */
:install
CALL :header
SET vc=VC9
SET architecture=x86
SET installVersion=%2
IF "!installVersion!" == "" SET installVersion=!phpversion!
IF exist "!versionsPath!\!installVersion!\php.exe" (
ECHO PHP !installVersion! is already installed
GOTO shutdown
)
IF "!installVersion!" gtr "5.5" (
SET vc=VC11
)
IF "!installVersion!" gtr "7" (
SET vc=VC14
SET architecture=x64
)
IF exist "!versionsPath!\!installVersion!.zip" ECHO !versionsPath!\!installVersion!.zip already downloaded
IF not exist "!versionsPath!\!installVersion!.zip" CALL :downloadRelease
IF not exist "!versionsPath!\!installVersion!.zip" CALL :downloadArchive
IF not exist "!versionsPath!\!installVersion!.zip" CALL :downloadCandidate
IF not exist "!versionsPath!\!installVersion!.zip" (
ECHO Download failed
GOTO shutdown
)
CALL :unzipAndConfigure
IF "%ERRORLEVEL%" NEQ "0" GOTO shutdown
ECHO.
ECHO Successfully added php version !installVersion! [thread safe, !vc!, !architecture!]
GOTO shutdown
:initializeComposer
IF not exist "!basePath!AlternatePHP\composer.phar" (
CALL :download "https://getcomposer.org/composer.phar" "!basePath!AlternatePHP\composer.phar"
)
rem latest phar is snapshot
rem `- switch to stable-channel by self-update with --stable
rem `- do not parse validate and use composer.json within current working dir by disable plugins by --no-plugins
IF exist "!basePath!AlternatePHP\composer.phar" (
echo Self-Update Composer (--no-plugins --stable):
"!versionsPath!\!phpversion!\php.exe" "!basePath!AlternatePHP\composer.phar" self-update --no-plugins --stable
)
EXIT /B %ERRORLEVEL%
:downloadRelease
CALL :downloadPhp releases/php-!installVersion!-Win32-!vc!-!architecture!.zip !versionsPath!\!installVersion!.zip
EXIT /B %ERRORLEVEL%
:downloadArchive
CALL :downloadPhp releases/archives/php-!installVersion!-Win32-!vc!-!architecture!.zip !versionsPath!\!installVersion!.zip
EXIT /B %ERRORLEVEL%
:downloadCandidate
CALL :downloadPhp qa/php-!installVersion!-Win32-!vc!-!architecture!.zip !versionsPath!\!installVersion!.zip
EXIT /B %ERRORLEVEL%
:downloadPhp
SET url=http://windows.php.net/downloads/%1
CALL :download !url! %2
EXIT /B %ERRORLEVEL%
:download
ECHO Download: %1
bitsadmin /rawreturn /transfer "Download" /PRIORITY FOREGROUND %1 %2 > nul 2>&1
IF exist %2 ECHO Successful!
IF not exist %2 ECHO Not successful!
EXIT /B %ERRORLEVEL%
rem /**
rem * unzip package and copy php.ini
rem */
:unzipAndConfigure
ECHO unzip downloaded binaries to !versionsPath!\!installVersion!
CALL :checkUnzip
IF "%ERRORLEVEL%" NEQ "0" (
ECHO no unzip found!
EXIT /B %ERRORLEVEL%
)
unzip -qq !versionsPath!\!installVersion!.zip -d !versionsPath!\!installVersion!
ECHO copy php.ini-production to php.ini
copy !versionsPath!\!installVersion!\php.ini-production !versionsPath!\!installVersion!\php.ini > nul 2>&1
EXIT /B 0
:downloadLatestVersionFile
CALL :download https://raw.githubusercontent.com/glady/AlternatePHP/master/AlternatePHP/VERSION !basePath!AlternatePHP\latestVERSION
EXIT /B %ERRORLEVEL%
:downloadLatestRelease
IF not exist "!basePath!AlternatePHP\!latestVersion!.zip" (
CALL :download https://github.com/glady/AlternatePHP/archive/!latestVersion!.zip !basePath!AlternatePHP\!latestVersion!.zip
)
EXIT /B %ERRORLEVEL%
:selfUpdate
SET /P currentVersion=<!basePath!AlternatePHP\VERSION
IF exist !versionsPath!\latestVERSION (
DEL !versionsPath!\latestVERSION
)
CALL :downloadLatestVersionFile
IF not exist !basePath!AlternatePHP\latestVERSION (
ECHO failed to retrieve latest version file
GOTO shutdown
)
SET /P latestVersion=<!basePath!AlternatePHP\latestVERSION
IF !currentVersion! == !latestVersion! (
ECHO You have already the latest version installed: !latestVersion!
GOTO shutdown
)
CALL :downloadLatestRelease
IF not exist !basePath!AlternatePHP\!latestVersion!.zip (
ECHO download failed!
EXIT /B %ERRORLEVEL%
)
CALL :checkUnzip
IF "%ERRORLEVEL%" NEQ "0" (
ECHO no unzip found!
EXIT /B %ERRORLEVEL%
)
ECHO Unzip new AlternatePHP over old one
SET subFolder=AlternatePHP-!latestVersion:~1!
unzip -qq -o !basePath!AlternatePHP\!latestVersion!.zip -d !basePath!update
"!basePath!update\!subFolder!\AlternatePHP\applyUpdate.bat" "!basePath!update\!subFolder!\" "!basePath!"
rd /s /q "!basePath!update"
ECHO Update applied!
GOTO shutdown
rem /**
rem * list installed versions
rem */
:versions
dir "!versionsPath!" /B /A:D
GOTO shutdown
rem /**
rem * switch global php version
rem */
:global
IF not exist "!versionsPath!\%2\php.exe" ECHO PHP %2 is currently not installed
IF exist "!versionsPath!\%2\php.exe" ECHO %2>!globalVersionFile!
GOTO shutdown
rem /**
rem * rename a php version
rem */
:rename
IF not exist "!versionsPath!\%2\php.exe" (
ECHO PHP %2 is currently not installed
GOTO shutdown
)
IF exist "!versionsPath!\%3\php.exe" (
ECHO PHP %3 is already installed
GOTO shutdown
)
RENAME "!versionsPath!\%2" "%3"
IF exist "!versionsPath!\%3\php.exe" (
ECHO PHP %2 is renamed to %3
)
IF not exist "!versionsPath!\%3\php.exe" (
ECHO Error while renaming %2 to %3
)
GOTO shutdown
rem checking if unzip is available
:checkUnzip
where unzip > nul 2>&1
EXIT /B %ERRORLEVEL%
rem implementation of string length (string, &length)
:strlen
set stringY=%~1%
set /A lengthY=0
:stringLengthLoop
if defined stringY (set stringY=%stringY:~1%&set /A lengthY += 1&goto stringLengthLoop)
set "%~2=%lengthY%"
GOTO :EOF
rem implementation of cutting off first x characters (&out, in length)
:removeFromStringLeft
set stringX=%~2%
set /A lengthX=%3%
:removeFromStringLeftLoop
if %lengthX% gtr 0 (set stringX=%stringX:~1%&SET /A lengthX -= 1&goto removeFromStringLeftLoop)
set "%1=%stringX%"
GOTO :EOF
rem implementation of cutting off first characters that are spaces (left trim) (&out, in)
:trimStringLeft
set stringZ=%~2%
:trimStringLeftLoop
if "%stringZ:~0,1%" == " " (set stringZ=%stringZ:~1%&goto trimStringLeftLoop)
set "%1=%stringZ%"
GOTO :EOF
rem /**
rem * end of script
rem */
:shutdown
ENDLOCAL
EXIT /B %ERRORLEVEL%