Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fb update environment argument #44

Merged
merged 15 commits into from
Nov 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 101 additions & 41 deletions bin/update-helper.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#!/bin/bash
# Vars:
set -e

# Vars:
environments='@self'
updated_packages=""

function header1() {
printf '// %s //\n\n' "$1"
}

function header2() {
printf '/// %s ///\n\n' "$1"
}


function show_help() {
cat << EOF

Expand All @@ -13,66 +25,92 @@ Update includes:
- For each package try to update and commit it (recovers previous state if fails)

Usage: ${0##*/} [--author=Name <user@example.com>]
-h|--help Show this help and exit.

-h|--help Show this help and exit.

--author Overrides default Git author name. Example Name <user@example.com>
--author Overrides default Git author name. Example Name <user@example.com>

--no-dev Disables search in require-dev packages.
--no-dev Disables search in require-dev packages.

--envs Force drush to work with an especific environments alias
EOF
}

function composer_update_outdated() {
DRUPAL_VERSION=$1
drupal_version=$1
environments=$2

# Outdated, minor version, just name, direct dependencies:
for c in $($updates)
do
echo -e "\n/// UPDATING: " $c "///////////////////////////////"
echo -e "\n"
header2 "Updating: $c"

package_version_from=$(composer show $c | grep versions | awk '{print $4}')

set +e
composer update $c --with-dependencies
if [[ $? -ne 0 ]]; then
echo -e "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "Updating package FAILED: recovering previous state."
echo -e "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
printf '\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
printf 'Updating package FAILED: recovering previous state.'
printf '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
git checkout composer.json composer.lock
continue
fi
set -e

package_version_to=$(composer show $c | grep versions | awk '{print $4}')

# Composer files:
git add composer.json composer.lock

# Drupal scaffold files:
git add web

# Clear caches to prevent problems with updated code.
if [[ $DRUPAL_VERSION -eq 7 ]]; then
drush cc all
if [[ $drupal_version -eq 7 ]]; then
run_drush "$environments" cc all
fi
if [[ $DRUPAL_VERSION -gt 8 ]]; then
drush cr
if [[ $drupal_version -gt 8 ]]; then
run_drush "$environments" cr
fi

drush updb -y
run_drush "$environments" updb -y

if [[ $DRUPAL_VERSION -gt 8 ]]; then
echo "Exporting any new configuration:"
drush cex -y
if [[ $drupal_version -gt 8 ]]; then
printf 'Exporting any new configuration: \n'
run_drush "$environments" cex -y
git add config
fi

git commit -m "UPDATE - $c" "$author_commit" -n || echo "No changes to commit"
echo -e "\n\n"
git commit -m "UPDATE - $c" "$author_commit" -n || printf "No changes to commit\n"

if [ "$package_version_from" != "$package_version_to" ]
then
updated_packages="$c from $package_version_from to $package_version_to\n$updated_packages"
fi

done
}

function run_drush() {
environments=$1
commands="${@:2}"
IFS=',' read -a environment_list <<< $environments
for environment in "${environment_list[@]}"
do
printf 'Running drush %s on the "%s" environment:\n' "$commands" "$environment"
drush $environment $commands
printf '\n'
done
}

## Defaults:
author_commit=""
drush="vendor/bin/drush"
updates="composer show -omND"
updates="composer show -oND"

echo -e "\n"
header1 "SETUP"

# Process script options.
#########################
Expand All @@ -85,13 +123,17 @@ do
;;
--author=*)
author="${i#*=}"
echo "GIT author will be overriden with: $author"
printf "GIT author will be overriden with: %s\n" "$author"
author_commit="--author=\"$author\""
;;
--no-dev)
echo "Updates without require-dev packages."
printf "Updates without require-dev packages.\n"
updates+=" --no-dev"
;;
--envs=*)
environments="${i#*=}"
printf "Environments used will be %s\n" "$environments"
;;
-?*|*)
printf 'ERROR: Unknown option: %s\n' "$1" >&2
show_help
Expand All @@ -101,33 +143,51 @@ do
shift
done

echo -e "\n"
header1 "SUMMARY"
echo " 1. Checking outdated packages"
echo " 2. Consolidating configuration"
echo " 3. Updating packages"
echo " 4. Report"

echo -e "\n"
header1 "1. CHECKING OUTDATED PACKAGES"

# Get the packages to be updated (direct dependencies): outdated, minor version only
packages_to_update=$($updates)
DRUPAL_VERSION="$(${drush} status --format=list 'Drupal version' | cut -d. -f1 -)"
drupal_version="$(drush status --format=list 'Drupal version' | cut -d. -f1 -)"

echo -e "\nPackages to update:"
echo "$packages_to_update"
printf '\n'

# Revert any overriden config to only export new configurations provided by module updates.
if [[ $DRUPAL_VERSION -gt 8 ]]; then
echo "Reverting any overriden configuration (drush cim)."
drush cr
drush cim -y
if [[ $drupal_version -gt 8 ]]; then
run_drush $environments cr
run_drush $environments cim -y

echo "Consolidating configuration (drush cex + git add):"
# estabilize current config (do not commit not exported config assiciated to a module):
drush cex -y
git add config && git commit -m "CONFIG - Consolidate current config stored in database" "$author_commit" -n || echo "No changes to commit"
echo -e "\n"
header1 "2. CONSOLIDATING CONFIGURATION"
# Estabilize current config (do not commit not exported config associated to a module):
run_drush $environments cex -y
git add config && git commit -m "CONFIG - Consolidate current configuration" "$author_commit" -n || echo "No changes to commit"

echo "Clearing cache"
drush cr
run_drush $environments cr

echo "Re-importing configuration"
drush cim -y
run_drush $environments cim -y
fi

echo -e "\nUpdating packages:"
composer_update_outdated $DRUPAL_VERSION
echo -e "\n"
header1 "3. UPDATING PACKAGES"
composer_update_outdated $drupal_version $environments

header1 "4. REPORT"

if [ "$updated_packages" != "" ]
then
echo -e "\n"
header2 "Updated Packages"
echo -e "$updated_packages\n"
fi

echo -e "\nPackages that were not updated:"
composer show -omD
header2 "Not Updated Packages"
composer show -oD