Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Created October 17, 2024 10:38
Show Gist options
  • Save peterjaap/35854ea3bb470c8ac03275f042496026 to your computer and use it in GitHub Desktop.
Save peterjaap/35854ea3bb470c8ac03275f042496026 to your computer and use it in GitHub Desktop.
Mage OS migration shell script - you can use this if you want to move from Magento community edition to MageOS. This updates your composer.json file and your local patches.
#!/bin/bash
# Path to the composer.json file
COMPOSER_FILE="composer.json"
TEMP_FILE="composer_temp.json"
# Checkout the composer.json file to avoid unwanted changes
git checkout "$COMPOSER_FILE"
# Create a new file to hold updated content
touch "$TEMP_FILE"
# Read the composer.json and process with jq
jq '(
. as $in |
# Handle allow-plugins: no changes to magento/ in this section
.config["allow-plugins"] as $allowPlugins |
# Modify all other sections of composer.json
$in |
.require |= with_entries(
if .key == "magento/product-community-edition" then
.key = "mage-os/product-community-edition" | .value = "1.0.4"
elif .key != "magento/magento-coding-standard" and .key != "magento/quality-patches" then
.key |= gsub("magento/"; "mage-os/")
else
.
end
) |
# Reassign unchanged allow-plugins section back to config
.config["allow-plugins"] = $allowPlugins
)' "$COMPOSER_FILE" > "$TEMP_FILE"
# Replace the original composer.json with the updated version, preserving indentation
jq --indent 4 '.' "$TEMP_FILE" > "$COMPOSER_FILE"
# Clean up the temp file
rm "$TEMP_FILE"
# Read the composer.json and use jq to find the required lines
jq '.config["allow-plugins"] | to_entries[] | select(.key | startswith("magento/")) | .key' composer.json | while read -r line; do
# Remove the quotes around the line
original_plugin=$(echo $line | tr -d '"')
# Replace "magento/" with "mage-os/"
new_plugin=$(echo $original_plugin | sed 's/magento\//mage-os\//')
# Use jq to add the new plugin entry while preserving indentation
jq --indent 4 --arg new_plugin "$new_plugin" --arg original_plugin "$original_plugin" \
'.config["allow-plugins"][$new_plugin] = .config["allow-plugins"][$original_plugin]' composer.json > composer.tmp
# Replace composer.json with the updated version
mv composer.tmp composer.json
done
echo "composer.json updated successfully. Now processing any patches."
# Path to the patches directory
PATCHES_DIR="patches"
# Find all patch files
patch_files=$(find "$PATCHES_DIR" -type f -name "*.patch")
# Loop through each patch file
for patch in $patch_files; do
if grep -q '@package magento/' "$patch"; then
echo "Processing patch: $patch"
# Replace @package magento/ with @package mage-os/
sed -i 's/@package magento\//@package mage-os\//g' "$patch"
echo "Updated @package header in $patch"
fi
done
echo "All applicable patch files have been processed."
# Ask the user if they want to run composer update -W
read -p "Do you want to run 'composer update -W' now? (y/n): " response
if [[ "$response" == "y" || "$response" == "Y" ]]; then
composer update -W
else
echo "You chose not to run 'composer update -W'."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment