diff --git a/dist/.ddev/config.wunderio.yaml b/dist/.ddev/config.wunderio.yaml index cb5d0ed..32226ed 100644 --- a/dist/.ddev/config.wunderio.yaml +++ b/dist/.ddev/config.wunderio.yaml @@ -9,6 +9,8 @@ hooks: - exec-host: ".ddev/wunderio/core/_run-scripts.sh hooks-host-post-start.sh" # Script to run on every start. - exec: "/var/www/html/.ddev/wunderio/core/_run-scripts.sh hooks-web-post-start.sh" + # Wunderio/ddev-drupal update check. + - exec: "UPDATE_SCRIPT=\"/var/www/html/vendor/wunderio/ddev-drupal/scripts/update_check.sh\"; [ -f \"$UPDATE_SCRIPT\" ] && $UPDATE_SCRIPT || true" web_environment: # Both this project and wunderio/lando-drupal use the same file # at drush/sites/local.site.yml. To make sure that the local alias diff --git a/scripts/update_check.sh b/scripts/update_check.sh new file mode 100755 index 0000000..a89ab5b --- /dev/null +++ b/scripts/update_check.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +# +# Wunderio/ddev-drupal package update check executed after DDEV has started. +# + +set -eu +if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then + set -x +fi +export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/var/www/html/vendor/bin + +source /var/www/html/.ddev/wunderio/core/_helpers.sh + +# Get the latest version of a package via Composer. +get_latest_version() { + # Run composer show to get information about the package + package_info=$(composer show wunderio/ddev-drupal --all) + + # Extract the versions and get the latest one + version=$(echo "$package_info" | grep -oP '(?<=versions : \* ).*' | tr -d ' ' | cut -d ',' -f 1) + + # Remove leading and trailing spaces + version=$(echo "$version" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + + echo "$version" +} + +# Get the current version of a package via Composer. +get_current_version() { + # Check the installed version of the package + current_version=$(composer show | grep 'wunderio/ddev-drupal' | awk '{print $2}') + + echo "$current_version" +} + +# Get the current version from the first argument. +current_version=$(get_current_version) + +# Get the latest version from Composer. +latest_version=$(get_latest_version) + +# Compare the current version with the latest version. +# If they are the same, exit the script. +if [[ $current_version == $latest_version ]]; then + exit 0 +fi + +# Prompt the user with a yes or no question. +# Separate the prompt from the rest of the output for +# better readability. +echo "" +display_status_message "A newer version of wunderio/ddev-drupal is available." +read -rp "$(display_status_message "Do you want to update to the latest version? This will run 'composer require wunderio/ddev-drupal --dev' (yes/no): [y] ")" answer + +# Convert the input to lowercase for case-insensitive comparison. +answer=${answer,,} +# If the answer is empty (user pressed Enter), default to "y". +answer=${answer:-y} + +# Check the user's input and perform actions accordingly +if [[ $answer == "yes" ]] || [[ $answer == "y" ]]; then + composer require wunderio/ddev-drupal --dev + echo "" + display_status_message "Staging the changes to GIT. " + read -rp "$(display_status_message "wunderio/ddev-drupal is updated, let's now add changes to GIT. This will run 'git add .ddev/ composer.json composer.lock' (yes/no): [y] ")" answer2 + # Convert the input to lowercase for case-insensitive comparison. + answer2=${answer2,,} + # If the answer is empty (user pressed Enter), default to "y". + answer2=${answer2:-y} + if [[ $answer2 == "yes" ]] || [[ $answer2 == "y" ]]; then + git add .ddev/ composer.json composer.lock + display_status_message "Done! Please verify the staged changes and commit (git status / git commit)." + else + display_status_message "Skipping the GIT staging. You need to manually stage the changes to GIT." + fi +else + display_status_message "Skipping the wunderio/ddev-drupal update." +fi