Skip to content

Instantly share code, notes, and snippets.

@nicoverbruggen
Last active November 22, 2021 16:38
Show Gist options
  • Save nicoverbruggen/9f2b9ee02d9f1069309b0878ad51fdc7 to your computer and use it in GitHub Desktop.
Save nicoverbruggen/9f2b9ee02d9f1069309b0878ad51fdc7 to your computer and use it in GitHub Desktop.
Easily swap in dependencies linked in a directory (./packages); filled in with examples I use
#!/bin/sh
# This script swaps out a production version of the packages with a local development version (or back).
echo "Removing vendor/nicoverbruggen/atlas-core..."
rm -rf ./vendor/nicoverbruggen/atlas-core
echo "Removing vendor/nicoverbruggen/gatekeeper..."
rm -rf ./vendor/nicoverbruggen/gatekeeper
# We’ll create a backup file that we can simply restore when we have to roll back the dependencies to a production version.
FILE=./composer.json.bak
if [ -f "$FILE" ]; then
# If the file exists, then we’ve already swapped out our dependencies, so we’ll restore them.
echo "$FILE exists, returning to production dependencies!"
echo "Restoring original composer file!"
rm ./composer.json
mv ./composer.json.bak ./composer.json
else
# If the file does not exist, we’ll make a backup of that original file and swap out the relevant lines in composer.json.
echo "Making a backup of the original composer file!"
cp 'composer.json' 'composer.json.bak'
echo "Swapping out dependencies..."
# Note: intended for use with macOS’ version of `sed` (`gsed` syntax differs: `gsed -i 'original|new|g' 'composer.json')
sed -i '' 's|{ "type": "vcs", "url": "git@gitlab.com:nicoverbruggen/atlas-core.git" }|{ "type": "path", "url": "../packages/atlas-core" }|g' 'composer.json'
sed -i '' 's|{ "type": "vcs", "url": "git@gitlab.com:nicoverbruggen/gatekeeper.git" }|{ "type": "path", "url": "../packages/gatekeeper" }|g' 'composer.json'
sed -i '' 's|"nicoverbruggen/atlas-core": "^*.*"|"nicoverbruggen/atlas-core": "@dev"|g' 'composer.json'
sed -i '' 's|"nicoverbruggen/gatekeeper": "^*.*"|"nicoverbruggen/gatekeeper": "@dev"|g' 'composer.json'
fi
# Next up, we’ll update the dependencies, but only the swapped packages!
echo "Running composer update (only for the swapped packages)..."
composer update nicoverbruggen/atlas-core nicoverbruggen/gatekeeper
echo "The dependencies have been swapped out!";
if [ -f "$FILE" ]; then
# Finally, if the backup exists now we’ve successfully swapped, so let’s symlink the assets directory!
# But before we can do that we need to throw the specific assets away.
rm -rf ./public/vendor/atlas-core
# After that, we’ll force a soft link so our assets don’t need to be published each time during dev work.
ln -svF "$(pwd)/vendor/nicoverbruggen/atlas-core/resources/assets" "$(pwd)/public/vendor/atlas-core"
echo "Atlas now uses the local symlinked version. You may need to start the watcher."
else
# If the backup does not exist, we’re back to the production version of our package.
echo "We're back to the production version of Atlas!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment