Skip to content

Instantly share code, notes, and snippets.

@gMagicScott
Last active July 18, 2017 02:14
Show Gist options
  • Save gMagicScott/3a9a4401beb7bc0b886532d5598a7bc0 to your computer and use it in GitHub Desktop.
Save gMagicScott/3a9a4401beb7bc0b886532d5598a7bc0 to your computer and use it in GitHub Desktop.
Migrate Composer Globals to CGR
#!/usr/bin/env bash
# Helper script to help migrate Composer (https://getcomposer.org/)
# global scripts to being managed by CGR (https://github.com/consolidation/cgr)
#
# Copyright (c) 2017 Scott Lesovic
# Released under the MIT License
# Sanity check Composer
which composer
if [ "$?" -ne 0 ]; then
echo "Composer binary not in \$PATH"
exit 1
else
echo 'Found Composer binary... now checking for global bin directory in \$PATH'
fi
# Locate Composer's global and package bin directory
globalDir=$(composer global config data-dir)
globalBinDir="$globalDir/$(composer global config bin-dir)"
globalVendorDir="$globalDir/$(composer global config vendor-dir)"
# Sanity check... $PATH
if [[ $PATH == ${PATH//$globalBinDir} ]]; then
echo "Composer's global bin directory isn't in your \$PATH"
exit 1
else
echo "Found Composer's bin directory. Migrating..."
fi
# If CGR is already installed... why go on?
which cgr
if [ "$?" -eq 0 ]; then
echo 'CGR already installed'
exit 1
fi
# Save current global composer packages
packages=$(composer global show -D -N --no-ansi 2> /dev/null | awk -vORS=" " '{ print $1 }' | sed 's/\s$/\n/')
# Find any Composer plugins
plugins=$(find "$globalVendorDir" -name composer.json | xargs grep -i -r 'composer-plugin' | awk '{print $1}' | uniq | sed "s,$globalVendorDir/,,g" | sed 's,/composer\.json:$,,g')
# Filter them from the main package list
for plugin in $plugins; do packages=${packages//$plugin/}; done
# Remove current global packages (except plugins)
composer global remove -n `echo $packages`
# Install CGR
composer global require consolidation/cgr
# Reinstall global packages that were removed using CGR
for i in $packages; do cgr $i; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment