Last active
March 18, 2021 11:25
-
-
Save aadmathijssen/56d3ae4efef584bb88dfd2d798dfd299 to your computer and use it in GitHub Desktop.
Composer CLI version selector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
for tool in jq composer1.phar composer2.phar; do | |
if ! [ -x "$(command -v ${tool})" ]; then | |
echo "Error: ${tool} is not installed." >&2 | |
exit 1 | |
fi | |
done | |
if [ ! -f composer.lock ] || jq -e '.["plugin-api-version"] | .[0:1] == "2"' composer.lock > /dev/null; then | |
composer2.phar "$@" | |
else | |
composer1.phar "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirements
The following tools are installed and available in
$PATH
:jq
composer1.phar
composer2.phar
Installation
Download the snippet file named
composer
to one of your$PATH
folders, and give the file executable permissions.Make sure the file is loaded before any other file named
composer
(such as a HomeBrew version in/usr/local/bin
).Usage
Run
composer
with any arguments from the root of your git repository to automatically run Composer using the best matching version; this also works with any tool that uses Composer and referencescomposer
without explicit paths.The Composer version is matched by inspecting the
plugin-api-version
property of the project'scomposer.lock
:2
, Composer 2 is used, otherwise Composer 1 is used.composer.lock
files generated using a version of Composer older than 1.10.0 do not contain aplugin-api-version
key (see also the corresponding commit).composer.lock
file itself is missing, Composer 2 is used; this makes sure Composer 2 is used when setting up Composer for the first time in a new project.