From 45cc902a9a02b3aeb82b703fa3a78260f2af191b Mon Sep 17 00:00:00 2001 From: Matthew Rath Date: Sun, 19 May 2019 15:42:48 +1000 Subject: [PATCH] Added ability to handle multiple PHP paths in the user path variable Added ability to remove from the environment user path multiple PHP version paths. Added ability to remove from the environment user path any PHP version path(s) that are not in the list of available PHP versions. Updated readme.md --- CHANGELOG.md | 9 +++++++ README.md | 56 +++++++++++++++++++++++++++++++++++---- cli_changer.bat | 69 +++++++++++++++++++++++++++++++++---------------- 3 files changed, 107 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83908ca..187cd33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## [Unreleased] +## [1.2.0] - 2019-05-19 + +### Added +- Ability to remove from the environment user path multiple PHP version paths. +- Ability to remove from the environment user path any PHP version path(s) that are not in the list of available PHP versions. + +### Changed +- Updated readme.md + ## [1.1.1] - 2018-12-23 ### Fixed diff --git a/README.md b/README.md index 4cff026..ced1b4e 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,42 @@ # WampServer PHP CLI Version Changer + +[![GitHub version](https://img.shields.io/github/tag/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?label=WampServer-PHP-CLI-Version-Changer&logo=github)](https://github.com/midnight-coding/WampServer-PHP-CLI-Version-Changer/releases) +![Maintained](https://img.shields.io/static/v1.svg?label=maintened&message=yes&color=informational&logo=github) +[![Stars](https://img.shields.io/github/stars/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?color=brightgreen&logo=github)](https://github.com/midnight-coding/WampServer-PHP-CLI-Version-Changer/stargazers) + +[![GitHub License](https://img.shields.io/github/license/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?color=informational&logo=github)](https://github.com/midnight-coding/WampServer-PHP-CLI-Version-Changer/blob/master/LICENSE-MIT) +[![GitHub last commit](https://img.shields.io/github/last-commit/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?logo=github)](https://github.com/midnight-coding/grunt-cache-killer/commits/master) +[![GitHub open issues](https://img.shields.io/github/issues-raw/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?color=brightgreen&logo=github)](https://github.com/midnight-coding/WampServer-PHP-CLI-Version-Changer/issues?q=is%3Aopen+is%3Aissue) +[![GitHub closed issues](https://img.shields.io/github/issues-closed-raw/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?color=brightgreen&logo=github)](https://github.com/midnight-coding/WampServer-PHP-CLI-Version-Changer/issues?q=is%3Aissue+is%3Aclosed) + WampServer PHP CLI Version Changer is a Microsoft Windows batch script that allows you to easily change between installed WampServer PHP CLI versions using the users environmental path. +## Table Of Contents + +* [Minimum Requirements](#minimum-requirements) +* [Installation](#installation) +* [Configuration](#configuration) +* [How To Use](#how-to-use) + * [Text Based User Interface (TUI)](#text-based-user-interface--TUI-) + * [Command Line Interface (CLI)](#command-line-interface--CLI-) +* [FAQ's](#faq-s) + ## Minimum Requirements + The following are required for the CLI Changer script to function correctly. - Microsoft Windows 7 and up. - WampServer v3.0.0 and up. ## Installation + No installation is required. -At just over 7kB the CLI Changer script is small enough to be saved anywhere in your file system. +At just over 10kB the CLI Changer script is small enough to be saved anywhere in your file system. **Tip:** Once you have save the CLI Changer script, create a desktop shortcut to it for quick and easy access. ## Configuration + No configuration is necessary if your installed WampServer in its default directory. The default installation directories are: @@ -24,7 +47,7 @@ If you installed WampServer in a custom directory, then follow the steps below: 1. Open the CLI Changer script in your preferred text editor. 2. Append your custom install path to the `$customInstallPath` variable. -3. Save the file and close your editor. +3. Save the file and close your text editor. Example: A customised WampServer install path. ``` @@ -39,12 +62,14 @@ set $customInstallPath=D:\WampServer 64-Bit **IMPORTANT:** Do not add quotation marks around your custom installation path, even if the path contains spaces. ## How To Use + There are two ways you can use the CLI Changer script. * Text based user interface (TUI). * Command line interface (CLI). ### Text Based User Interface (TUI) + Upon running the CLI Changer script you will be presented with a numbered list of installed PHP versions that you can select from. The exact list of installed PHP CLI versions is dependent on what you currently have in your WampServer PHP addon folder. @@ -84,6 +109,7 @@ To exit the CLI Changer script without making any changes just press the ENTER k Should you make an invalid selection or should the selection be the same as the currently selected version then you will receive feedback indicating so. ### Command Line Interface (CLI) + To update the PHP CLI version number directly from the command line, you can pass in the desired PHP version via the scripts first argument. This will bypass the selection screen and go straight to setting the desired version number. From a CMD prompt: @@ -108,7 +134,21 @@ Following execution, an exit code will be given: **Tip:** Calling scripts via the command line is common during (automated) development, testing and deployment. EG: Incorporate it into your build files... ## FAQ's + +### How does the environment 'user' and 'system' path variables work? + +Environment 'path' variables allow the user (and system) to call an executable without the need to use the executables absolute (full) path. Windows parses the path variables from left to right, with the 'user' path being appended to the 'system' path. (IE: path = system.path + user.path) + +When the user (or the user's script) calls the `php.exe` executable, the path pointing to the executable will be used. If an environment path contains two or more paths to a PHP executable, then only the first one found is executed. The other php executables will never be called, ever. + +Based on this information and pursuant to the successful selection of a PHP version number, this script scans and then removes any and all reference to any php executable path(s) found within the environment 'user' path prior to appending the selected PHP version path. + +The PHP CLI version number returned by typing `php -v` at the command prompt should be the same as that selected by you when using this script. If it is not, there is a strong chance that there is reference to a PHP executable within the environment 'system' path. To correct this situation, you must remove this reference from the environment 'system' path manually. + +Both the environment 'user' and 'system' paths can be accessed by clicking 'Start' -> 'Control Panel' -> 'System' -> 'Advanced system settings' -> 'Environment Variables...' + ### How do I remove the error at the bottom of the WampServer right-click menu? + As of WampServer v3.1.2 the below error message may be displayed. ``` @@ -137,11 +177,17 @@ Press ENTER to continue... This error can be suppressed by right-clicking the WampServer icon in the taskbar notification area and selecting: _Wamp Settings -> Do not verify PATH_ -**Question 1:** Why would you want to suppress this error? Because currently, WampServer does not have the ability to change the CLI version of PHP should your script(s) require a specific version. +#### Why would you want to suppress this error? + +Because currently, WampServer does not have the ability to change the CLI version of PHP should your script(s) require a specific version. + +#### But isn't that what right-clicking the WampServer icon in the taskbar notification area and selecting: _Tools -> Change PHP CLI version_ does? + +No, it doesn't. This selection currently changes the CLI version that the _WampServer's scripts use_, not what your scripts use when called from the command line. -**Question 2:** But isn't that what right-clicking the WampServer icon in the taskbar notification area and selecting: _Tools -> Change PHP CLI version_ does? No, it doesn't. This selection currently changes the CLI version that the _WampServer's scripts use_, not what your scripts use when called from the command line. +#### So can I safely use this script? -**Question 3:** So can I safely use this script? Currently, yes (but this may change if WampServer decides in the future to begin using either or both of your systems environmental path variables). +Currently, yes (but this may change if WampServer decides in the future to begin using either or both of your systems environmental path variables). ### How do I add more PHP versions? To add more PHP versions to your WampServer v3 installation visit [SourceForge](https://sourceforge.net/projects/wampserver/files/WampServer%203/WampServer%203.0.0/Addons/Php/). \ No newline at end of file diff --git a/cli_changer.bat b/cli_changer.bat index 8436547..735603d 100644 --- a/cli_changer.bat +++ b/cli_changer.bat @@ -20,7 +20,7 @@ rem +------------------------------------------------+ rem --------------------- rem Default Variables rem --------------------- -set $scriptVersion=1.1.1 +set $scriptVersion=1.2.0 set $defaultInstallPath[0]=C:\wamp set $defaultInstallPath[1]=C:\wamp64 @@ -77,17 +77,17 @@ rem ------------------- rem PHP Folder Path rem ------------------- -rem Set the $pathToPhpFolders path. +rem Set the path to the PHP folders. if %$installPath:~-1% neq \ ( set $pathToPhpFolders=%$installPath%\%$pathToPhpFolders% ) else ( set $pathToPhpFolders=%$installPath%%$pathToPhpFolders% ) -rem Check the $pathToPhpFolders path exists. +rem Check the path to the PHP folders exists. if not exist "%$pathToPhpFolders%" goto invalidPathToPhpFoldersGiven -rem Iterate through folders in the the $pathToPhpFolders path adding them to the availablePhpVersionsArray. +rem Iterate through the folders in the the $pathToPhpFolders variable adding them to the $availablePhpVersionsArray. set counter=0 for /F "delims=" %%a in ('dir %$pathToPhpFolders% /AD /B') do ( @@ -104,11 +104,11 @@ rem Users Environmental Path rem ---------------------------- rem Get the users environmental path string. -for /f "usebackq tokens=2,*" %%a in (`reg.exe query HKCU\Environment /v PATH`) do ( +for /F "usebackq tokens=2,*" %%a in (`reg.exe query HKCU\Environment /v PATH`) do ( set $usersEnvironmentalPathString=%%b ) -rem Explode the users environmental path string into an array. +rem Using recursion, explode the users environmental path string into an array. set counter=0 :explode @@ -128,16 +128,18 @@ rem ---------------------------- rem Match PHP Folder Version rem ---------------------------- -rem Only the first PHP path is used by the computer if more than one PHP path be detected in the users -rem environmental path. Therefore, there is no need to detect multiple PHP paths, only the first one. +rem If there is more than one PHP path in the users environmental path, the operating system +rem will only use the first one. Therefore, we only need to match the first one. set $currentPhpVersionId=0 set $currentUserEnvPathId=0 -rem Loop through the $usersEnvironmentalPathArray. +rem Iterate through the users environmental path array. for /L %%a in (1,1,%$lastUsersEnvironmentalPathArrayId%) do ( - rem Loop through the $availablePhpVersionsArray. + + rem Iterate through the available PHP versions array. for /L %%b in (1,1,%$lastAvailablePhpVersionsArrayId%) do ( - rem Check if the users environmental path string matches the path to the available PHP version string. + + rem Check if the users environmental path string matches the (combined) full path of the available PHP version string. if "!$usersEnvironmentalPathArray[%%a]!"=="%$pathToPhpFolders%\!$availablePhpVersionsArray[%%b]!" ( rem Force the 'for' command parameters into type 'integer'. set /A $currentPhpVersionId=currentPhpVersionId+%%b @@ -160,8 +162,10 @@ if "%~1" neq "" ( set $cliMode=1 set $newSelectionId=0 - rem Find the $newSelectionId by matching the name to the id. + rem Iterate through the available PHP versions array. for /L %%a in (1,1,%$lastAvailablePhpVersionsArrayId%) do ( + + rem If a matching installed PHP folder name is found, set the new selection id. if "%1"=="!$availablePhpVersionsArray[%%a]!" ( set $newSelectionId=%%a ) @@ -177,7 +181,7 @@ rem Hack(s) 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 +for /F %%a in ('"prompt $H &echo on &for %%b in (1) do rem"') do set backspace=%%a rem ------------------------ @@ -193,8 +197,10 @@ echo Available PHP CLI Versions echo -------------------------- echo: -rem List all installed PHP folder names. +rem Iterate though the available PHP versions array. for /L %%a in (1,1,%$lastAvailablePhpVersionsArrayId%) do ( + + rem Check if the listed version matches the current version. if %%a equ %$currentPhpVersionId% ( echo %%a - !$availablePhpVersionsArray[%%a]! - Current ) else ( @@ -214,7 +220,7 @@ rem -------------------- :checkUserInput rem Check if the new selection comprises of digits. -echo %$newSelectionId%| findstr /r "^[1-9][0-9]*$">nul +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. @@ -228,20 +234,39 @@ rem --------------------------------- rem Update Users Environment Path rem --------------------------------- -rem Rebuild the $usersEnvironmentalPathString excluding any previously set PHP folder path. +rem Rebuild the users environmental path string excluding any and all previously +rem set PHP folder paths no matter where they are located within the string. set "$usersEnvironmentalPathString=" +rem Iterate through the users environmental path array. for /L %%a in (1,1,%$lastUsersEnvironmentalPathArrayId%) do ( - if !$currentUserEnvPathId! neq %%a ( - set $result=!$result!!$usersEnvironmentalPathArray[%%a]!; + + rem Remove any trailing slash. + if !$usersEnvironmentalPathArray[%%a]:~-1! equ \ ( + set $path=!$usersEnvironmentalPathArray[%%a]:~0,-1! + ) else ( + set $path=!$usersEnvironmentalPathArray[%%a]! + ) + + rem Get the last segment of the path. + for %%b in (!$path!) do ( + set $segment=%%~nxb + ) + + rem Check the segment for a matching regex expression. IE: Any PHP folder. + echo !$segment! | 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 users environmental path string. + if !errorlevel! neq 0 ( + set $usersEnvironmentalPathString=!$usersEnvironmentalPathString!!$usersEnvironmentalPathArray[%%a]!; ) ) -rem Add the selected PHP folder path to the end of the $usersEnvironmentalPathString. -set $result=%$result%%$pathToPhpFolders%\!$availablePhpVersionsArray[%$newSelectionId%]! +rem Add the selected PHP folder path to the end of the users environmental path string. +set $usersEnvironmentalPathString=%$usersEnvironmentalPathString%%$pathToPhpFolders%\!$availablePhpVersionsArray[%$newSelectionId%]! -rem Set the $usersEnvironmentalPathString. -setx path "%$result%" >nul +rem Set the users environmental path string. +setx path "%$usersEnvironmentalPathString%" >nul rem ------------------------------