Skip to content

Instantly share code, notes, and snippets.

@0m3r
Created January 22, 2020 14:53
Show Gist options
  • Save 0m3r/2a1cd0354db961b3e981dca2fa83c610 to your computer and use it in GitHub Desktop.
Save 0m3r/2a1cd0354db961b3e981dca2fa83c610 to your computer and use it in GitHub Desktop.
#!/bin/bash
if test "$#" -eq 3; then
PACKAGE=${1}
VERSION=${2}
REQUIRE=${3}
else
PACKAGE="magento/product-community-edition"
VERSION=${1}
REQUIRE=${2}
fi
VERSIONS=($(echo $VERSION | tr "," " "))
if [[ ! $REQUIRE ]]; then
REQUIRE="magento/framework"
fi
REQUIRES=($(echo $REQUIRE | tr "," " "))
REPO='https://repo.magento.com/packages.json'
hash composer 2>/dev/null || { echo >&2 "The script requires composer (https://getcomposer.org/download/)"; exit 1; }
hash jq 2>/dev/null || { echo >&2 "The script requires jq (https://stedolan.github.io/jq/download/)"; exit 1; }
USERNAME=$(composer global config http-basic.repo.magento.com.username -q) >/dev/null || {
echo >&2 "Get and set your magento credentials https://devdocs.magento.com/guides/v2.3/install-gde/prereq/connect-auth.html) \n composer config -g http-basic.repo.magento.com [Public Key] [Private Key]";
exit 1;
}
PASSWORD=$(composer global config http-basic.repo.magento.com.password -q)
COMPOSER_COMMAND=''
for REQUIRE in "${REQUIRES[@]}"; do
CONSTRAINTS=''
for VERSION in "${VERSIONS[@]}"; do
JQUERY='.packages."'$PACKAGE'"."'$VERSION'".require."'$REQUIRE'"'
REQUIRE_VERSION=$(curl --silent --basic --user $USERNAME:$PASSWORD $REPO | jq $JQUERY | sed -e 's/^"//' -e 's/"$//')
REQUIRE_VERSION="$(cut -d '.' -f 1 <<< "$REQUIRE_VERSION")"."$(cut -d '.' -f 2 <<< "$REQUIRE_VERSION")"
# echo ''$PACKAGE':'$VERSION' requires '$REQUIRE':'$REQUIRE_VERSION
if [[ ! $CONSTRAINTS ]]; then
CONSTRAINTS="^${REQUIRE_VERSION}"
else
CONSTRAINTS="${CONSTRAINTS}|^${REQUIRE_VERSION}"
fi
done
if [[ ! $COMPOSER_COMMAND ]]; then
COMPOSER_COMMAND="composer require ${REQUIRE}:${CONSTRAINTS}"
else
COMPOSER_COMMAND="${COMPOSER_COMMAND},${REQUIRE}:${CONSTRAINTS}"
fi
echo "composer require ${REQUIRE}:${CONSTRAINTS}"
done
echo $COMPOSER_COMMAND
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment