diff --git a/CHANGELOG.md b/CHANGELOG.md index f9f104c..18da042 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.3.0] - 2021-07-02 + +### Added +- Added ability to change PHP version within an open CMD Window session only. + +### Updated +- Updated readme.md reflecting the new feature. +- Updated version number. + ## [1.2.3] - 2020-08-26 ### Fixed diff --git a/README.md b/README.md index b0c7361..0789bc1 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ WampServer PHP CLI Version Changer is a Microsoft Windows batch script that allo * [How To Use](#how-to-use) * [Text Based User Interface (TUI)](#text-based-user-interface-tui) * [Command Line Interface (CLI)](#command-line-interface-cli) + * [Session Mode](#session-mode) * [FAQ's](#faqs) ## Minimum Requirements @@ -26,7 +27,7 @@ The following are required for the CLI Changer script to function correctly. No installation is required. -At just over 10kB the CLI Changer script is small enough to be saved anywhere in your file system. +At just over 13KB 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. @@ -51,6 +52,7 @@ rem | User Defined Variable(s) | rem +------------------------------------------------+ rem WampServer custom install path. +rem Note: Trailing slash is not required. set $customInstallPath=D:\WampServer 64-Bit ``` @@ -62,6 +64,7 @@ There are two ways you can use the CLI Changer script. * Text based user interface (TUI). * Command line interface (CLI). + * Session mode ### Text Based User Interface (TUI) @@ -128,6 +131,31 @@ 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... +#### Session Mode + +Should you have the need to only change the PHP version number within an open command window (session) and not across your whole system then you can use the `--temp` option. + +From a CMD prompt: +``` +C:\>: "C:\path\to\cli_changer.bat" php7.2.3 --temp +``` + +From a Bash or Powershell prompt: +``` +$ start "C:\path\to\cli_changer.bat" php7.2.3 --temp +``` + +The short command line option `-t` is also available. + +**Note:** Enclosing the script path in quotes if it contains spaces and knowing the available PHP CLI version(s) in advance is still required. + +Following execution, an exit code will be given: + +- `0` - Success +- `1` - Failure + +**Note:** Using the `--temp` or `-t` option will only change the PHP version number within that command window. Multiple command windows can be open and the PHP version number changed without affecting other open command windows. + ## FAQ's ### What are environment path variables and how do they work? @@ -136,11 +164,14 @@ Environment 'path' variables allow the user (and system) to call an executable w When the user (or the user's script) calls `php` or `php.exe`, 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. +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 (or the cmd window 'session' 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...' +Access the environment 'user' and 'system' paths by: + +* Windows 7: Clicking 'Start' -> 'Control Panel' -> 'System' -> 'Advanced system settings' -> 'Environment Variables...' +* Windows 10: Clicking 'Start Search' -> Type 'env' -> Click the result 'Edit system environment variables' -> 'Environment Variables...' ### How do I remove the error at the bottom of the WampServer right-click menu? diff --git a/cli_changer.bat b/cli_changer.bat index 5f85944..5bc472c 100644 --- a/cli_changer.bat +++ b/cli_changer.bat @@ -1,7 +1,5 @@ @echo off -cls setlocal EnableDelayedExpansion EnableExtensions -mode con: cols=75 lines=40 rem +------------------------------------------------+ @@ -9,6 +7,7 @@ rem | User Defined Variable(s) | rem +------------------------------------------------+ rem WampServer custom install path. +rem Note: Trailing slash is not required. set $customInstallPath= @@ -17,10 +16,10 @@ rem +------------------------------------------------+ rem | DO NOT EDIT BELOW THIS LINE | rem +------------------------------------------------+ -rem --------------------- -rem Default Variables -rem --------------------- -set $scriptVersion=1.2.3 +rem ------------------- +rem Default Variables +rem ------------------- +set $scriptVersion=1.3.0 set $defaultInstallPath[0]=C:\wamp set $defaultInstallPath[1]=C:\wamp64 @@ -28,19 +27,42 @@ 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 Set the title. -title WampServer PHP CLI Version Changer v%$scriptVersion% +rem ------------------------- +rem Check Mode of Operation +rem ------------------------- -rem ----------------- -rem Install Paths -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 ( @@ -73,11 +95,11 @@ rem Exit if unable to find installation path. if not defined $installPath goto defaultInstallPathsMissing -rem ------------------- -rem PHP Folder Path -rem ------------------- +rem ----------------------- +rem Check PHP Folder Path +rem ----------------------- -rem Set the path to the PHP folders. +rem Set the absolute path to the PHP folders. if %$installPath:~-1% neq \ ( set $pathToPhpFolders=%$installPath%\%$pathToPhpFolders% ) else ( @@ -87,110 +109,113 @@ if %$installPath:~-1% neq \ ( rem Check the path to the PHP folders exists. if not exist "%$pathToPhpFolders%" goto invalidPathToPhpFoldersGiven -rem Iterate through the folders in the the $pathToPhpFolders variable adding them to the $availablePhpVersionsArray. + +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 $availablePhpVersionsArray[!counter!]=%%a + set $availablePhpArray[!counter!]=%%a ) -rem Set the last available PHP versions array id. -set $lastAvailablePhpVersionsArrayId=!counter! +rem Get the total number of elements in the available PHP array. +set $availablePhpCount=!counter! rem ---------------------------- -rem Users Environmental Path +rem Explode 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 ( - set $usersEnvironmentalPathString=%%b +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 Using recursion, explode the users environmental path string into an array. +rem Explode the path string into an array. set counter=0 :explode -for /F "tokens=1* delims=;" %%a in ("%$usersEnvironmentalPathString%") do ( +for /F "tokens=1* delims=;" %%a in ("%$pathString%") do ( set /A counter=counter+1 - set $usersEnvironmentalPathArray[!counter!]=%%a - set $usersEnvironmentalPathString=%%b + set $pathArray[!counter!]=%%a + set $pathString=%%b ) -if defined $usersEnvironmentalPathString goto explode +if defined $pathString goto explode -rem Set the last users environmental path array id. -set $lastUsersEnvironmentalPathArrayId=!counter! +rem Get the total number of elements in the path array. +set $pathArrayCount=!counter! rem ---------------------------- -rem Match PHP Folder Version +rem Find Active PHP Version(s) rem ---------------------------- -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. +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 -set $currentUserEnvPathId=0 -rem Iterate through the users environmental path array. -for /L %%a in (1,1,%$lastUsersEnvironmentalPathArrayId%) do ( +rem Iterate through the path array. +for /L %%a in (1,1,%$pathArrayCount%) do ( - rem Iterate through the available PHP versions array. - for /L %%b in (1,1,%$lastAvailablePhpVersionsArrayId%) do ( + rem Iterate through the available PHP's array. + for /L %%b in (1,1,%$availablePhpCount%) do ( - 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 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 - set /A $currentUserEnvPathId=$currentUserEnvPathId+%%a goto break ) ) ) + :break -rem -------------------- -rem Operation By CLI -rem -------------------- +rem ------------------ +rem Operation by CLI +rem ------------------ rem Check if the CLI is being used. -if "%~1" neq "" ( +if %$cliMode% equ 1 ( - rem Set the CLI mode flag. - set $cliMode=1 + rem Set the newly selected id. set $newSelectionId=0 rem Iterate through the available PHP versions array. - for /L %%a in (1,1,%$lastAvailablePhpVersionsArrayId%) do ( + 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"=="!$availablePhpVersionsArray[%%a]!" ( + if "%1"=="!$availablePhpArray[%%a]!" ( set $newSelectionId=%%a ) ) - rem Bypass displaying anything. + rem Bypass displaying the TUI. goto checkUserInput ) -rem ----------- -rem Hack(s) -rem ----------- +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 ------------------------ -rem Display PHP Versions -rem ------------------------ - -rem Set the window. -color %$colorNormal% - rem Show the header. echo: echo Available PHP CLI Versions @@ -198,25 +223,25 @@ echo -------------------------- echo: rem Iterate though the available PHP versions array. -for /L %%a in (1,1,%$lastAvailablePhpVersionsArrayId%) do ( +for /L %%a in (1,1,%$availablePhpCount%) do ( rem Check if the listed version matches the current version. if %%a equ %$currentPhpVersionId% ( - echo %%a - !$availablePhpVersionsArray[%%a]! - Current + echo %%a - !$availablePhpArray[%%a]! - Current ) else ( - echo %%a - !$availablePhpVersionsArray[%%a]! + echo %%a - !$availablePhpArray[%%a]! ) ) -rem Prompt the user to make a new selection. +rem Prompt the user to make a selection. echo: -set /p $newSelectionId=%backspace% Selection (1-%$lastAvailablePhpVersionsArrayId%): +set /p $newSelectionId=%backspace% Selection (1-%$availablePhpCount%): echo: -rem -------------------- -rem Check User Input -rem -------------------- +rem ------------------ +rem Check User Input +rem ------------------ :checkUserInput rem Check if the new selection comprises of digits. @@ -224,69 +249,122 @@ 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 %$lastAvailablePhpVersionsArrayId% goto invalidSelectionGiven +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 Update Users Environment Path -rem --------------------------------- +rem -------------------------- +rem Implode Environment Path +rem -------------------------- -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 Rebuild the path string while excluding any and all found PHP paths. +set "$pathString=" -rem Iterate through the users environmental path array. -for /L %%a in (1,1,%$lastUsersEnvironmentalPathArrayId%) do ( +rem Iterate through the path array. +for /L %%a in (1,1,%$pathArrayCount%) do ( rem Remove any trailing slash. - if !$usersEnvironmentalPathArray[%%a]:~-1! equ \ ( - set $path=!$usersEnvironmentalPathArray[%%a]:~0,-1! + if !$pathArray[%%a]:~-1! equ \ ( + set $path=!$pathArray[%%a]:~0,-1! ) else ( - set $path=!$usersEnvironmentalPathArray[%%a]! + set $path=!$pathArray[%%a]! ) rem Get the last segment of the path. for %%b in (!$path!) do ( - set $segment=%%~nxb + set $lastSegment=%%~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 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 users environmental path string. + rem If a match is not found, append the path to the path string and include a trailing semicolon. if !errorlevel! neq 0 ( - set $usersEnvironmentalPathString=!$usersEnvironmentalPathString!!$usersEnvironmentalPathArray[%%a]!; + set $pathString=!$pathString!!$pathArray[%%a]!; ) ) -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 users environmental path string. -setx path "%$usersEnvironmentalPathString%" >nul +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 Exit Subroutines - Success -rem ------------------------------ -rem The update was successful. +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 !$availablePhpVersionsArray[%$newSelectionId%]! + 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 ) -exit 0 -rem A current selection was given. +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 ( @@ -295,16 +373,21 @@ if %$cliMode% equ 0 ( 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 ) -exit 0 - -rem ------------------------------ -rem Exit Subroutines - Failure -rem ------------------------------ +rem ==================================================================================================================== +rem Failure Message +rem ==================================================================================================================== -rem An invalid selection was given. +rem ------------------------- +rem Invalid selection given +rem ------------------------- :invalidSelectionGiven if %$cliMode% equ 0 ( @@ -313,15 +396,21 @@ if %$cliMode% equ 0 ( 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 ) -exit 1 -rem ---------------------------- -rem Exit Subroutines - Error -rem ---------------------------- +rem ==================================================================================================================== +rem Error Messages +rem ==================================================================================================================== -rem An invalid $customInstallPath was given. +rem ----------------------------------- +rem Invalid custom install path given +rem ----------------------------------- :invalidCustomInstallPathGiven if %$cliMode% equ 0 ( @@ -331,29 +420,46 @@ if %$cliMode% equ 0 ( 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 ) -exit 1 -rem Both of the default install paths are missing. +rem ------------------------------- +rem Default install paths missing +rem ------------------------------- :defaultInstallPathsMissing if %$cliMode% equ 0 ( color %$colorFailure% echo: - echo Neither of the default install paths exists. + echo Neither of the default installation paths exists. echo: - echo 1. %$defaultInstallPath[0]% - echo 2. %$defaultInstallPath[1]% + echo 1. %$defaultInstallPath[0]% + echo 2. %$defaultInstallPath[1]% echo: - echo Wampserver does not appear to be installed. + 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 ) -exit 1 -rem An invalid $pathToPhpFolders was given. +rem ---------------------------------- +rem Invalid path to PHP folder given +rem ---------------------------------- :invalidPathToPhpFoldersGiven if %$cliMode% equ 0 ( @@ -365,6 +471,11 @@ if %$cliMode% equ 0 ( echo: echo Press any key to exit. pause >nul -) - -exit 1 \ No newline at end of file + exit 1 +) else ( + echo: + echo Error: The $pathToPhpFolders path "%$pathToPhpFolders%" does not exist. + echo: + echo See the WampServer website for help. + exit /B 1 +) \ No newline at end of file