How to update WP-CLI on Windows via Composer?

I installed wp-cli back in April on my Windows box using composer. I believe with the following command: composer create-project wp-cli/wp-cli --no-dev as outlined in the alternate install methods on here.

I have since used "composer update --no-dev" to update but just realized that it is only updating the dependencies and not the wp-cli package itself. If I run a "wp cli version" it reports version WP-CLI 0.18.0, yet v0.20.4 is the latest released version.

I can't seem to find any way to update wp-cli. I suppose I could just install a new copy each time but that seems silly. Regardless, I did test and if I issue a "composer create-project wp-cli/wp-cli --no-dev" in a new directory it downloads the latest version. I also tried "wp cli update" but it reports back "Error: You can only self-update PHARs."

Topic composer wp-cli windows installation Wordpress

Category Web


Apparently this isn't possible using only composer. Seems kind of silly for it to be a recommended way to install WP-CLI with no direct way to update it later. I ended up installing GIT and then doing a fresh install (e.g. git clone https://github.com/wp-cli/wp-cli.git d:\wp-cli) of wp-cli. Afterwards, issued a composer update --no-dev to install dependencies. Now in the future to update wp-cli I run git pull origin from d:\wp-cli followed by composer update --no-dev.

Let me know if this is wrong! :)

Edit--

So I realized that the above command was updating to the latest commit in the master branch. I couldn't find any automated way to always update to the latest branch so I wrote the following batch file. It queries all the tags from the wp-cli repo, iterates through them to find the latest one (because they are sorted alpha), then either clones or resets (if local repo already exists) to that tag. Then updates the dependencies via composer.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET WPCLI_INSTALL_DRIVE=D:
SET WPCLI_INSTALL_DIR=WP-CLI
SET LATEST_WPCLI_MAJOR=-1
SET LATEST_WPCLI_MINOR=0
SET LATEST_WPCLI_REV=0
echo.
echo COMMIT_ID - TAG_NAME ^=^> VERSION_DETECTION [*] (* ^= Marked as latest)
for /f "tokens=1*" %%A in ('git ls-remote --tags https://github.com/wp-cli/wp-cli.git') do (
    SET "TAG_LINE=%%A"
    IF NOT "%%B"=="" (
        SET "TAG_LINE=!TAG_LINE! - %%B"
        for /f "tokens=2 delims=v" %%C in ("%%B") do (
            echo "%%C"|find /i ".">nul
            IF NOT ERRORLEVEL 1 (
                for /f "tokens=1-3 delims=." %%D in ("%%C") do (
                    SET "TAG_LINE=!TAG_LINE! ^=^> v%%D.%%E.%%F"
                    SET LATEST_WPCLI_FOUND=0

                    IF /I %%D GTR !LATEST_WPCLI_MAJOR! (
                        SET LATEST_WPCLI_FOUND=1
                    ) ELSE (
                        IF /I %%D GEQ !LATEST_WPCLI_MAJOR! (
                            IF /I %%E GTR !LATEST_WPCLI_MINOR! (
                                SET LATEST_WPCLI_FOUND=1
                            ) ELSE (
                                IF /I %%E GEQ !LATEST_WPCLI_MINOR! (
                                    IF /I %%F GTR !LATEST_WPCLI_REV! (
                                        SET LATEST_WPCLI_FOUND=1
                                    )
                                )
                            )
                        )
                    )

                    IF "!LATEST_WPCLI_FOUND!"=="1" (
                        SET "TAG_LINE=!TAG_LINE! *"
                        SET LATEST_WPCLI_MAJOR=%%D
                        SET LATEST_WPCLI_MINOR=%%E
                        SET LATEST_WPCLI_REV=%%F
                    )
                )
            )
        )
    )
    echo !TAG_LINE!
)

echo.
echo.

IF NOT "%LATEST_WPCLI_MAJOR%"=="-1" (
    echo Latest detected tag: v%LATEST_WPCLI_MAJOR%.%LATEST_WPCLI_MINOR%.%LATEST_WPCLI_REV%
    echo.
    IF EXIST "%WPCLI_INSTALL_DRIVE%\%WPCLI_INSTALL_DIR%\bin\wp.bat" (
        echo Current WP-CLI Version:
        call "%WPCLI_INSTALL_DRIVE%\%WPCLI_INSTALL_DIR%\bin\wp.bat" cli version
    ) ELSE (
        echo Current "%WPCLI_INSTALL_DRIVE%\%WPCLI_INSTALL_DIR%\bin\wp.bat" not found to display current version.
    )
    echo.
    choice /m "Continue installing/updating?"
    IF ERRORLEVEL 2 exit /b 0

    IF EXIST "%WPCLI_INSTALL_DRIVE%\%WPCLI_INSTALL_DIR%\.gitignore" (
        CHDIR /D "%WPCLI_INSTALL_DRIVE%\%WPCLI_INSTALL_DIR%"
        git reset --hard "v%LATEST_WPCLI_MAJOR%.%LATEST_WPCLI_MINOR%.%LATEST_WPCLI_REV%"
    ) ELSE (
        git clone --branch "v%LATEST_WPCLI_MAJOR%.%LATEST_WPCLI_MINOR%.%LATEST_WPCLI_REV%" https://github.com/wp-cli/wp-cli.git %WPCLI_INSTALL_DRIVE%\%WPCLI_INSTALL_DIR%
        CHDIR /D "%WPCLI_INSTALL_DRIVE%\%WPCLI_INSTALL_DIR%"
    )
    IF EXIST "composer.json" call composer update --no-dev
) ELSE (
    echo Latest version not found.
)
pause

You need to remove composer.lock before running composer update. composer.lock locks your Composer project to a specific hash.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.