-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdrupal-updater
More file actions
executable file
·152 lines (133 loc) · 5.2 KB
/
drupal-updater
File metadata and controls
executable file
·152 lines (133 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env bash
#ddev-generated
#annertech-ddev
## Description: Update Drupal modules and core, optionally committing each step
## Usage: drupal-updater
## Example: "ddev drupal-updater -cgado" \n"ddev drupal-updater drupal/module-name --commit-steps" \n"ddev drupal-updater --all --with-core --commit-steps" \n"ddev drupal-updater --dry-run --omit-snapshot"
## Flags: [{"Name":"commit-steps","Shorthand":"g","Usage":"Create a git commit for each update step","Type":"bool"},{"Name":"with-core","Shorthand":"c", "Usage":"Include core updates in --all","Type":"bool"},{"Name":"all","Shorthand":"a","Usage":"Update all modules","Type":"bool"},{"Name":"dry-run","Usage":"Run the update steps without making changes","Type":"bool"},{"Name":"omit-snapshot", "Shorthand": "o", "Usage":"Omit snapshot creation before updating","Type":"bool"},{"Name":"run-db-updates","Shorthand":"d","Usage":"Run database updates between module updates","Type":"bool"}]
## ProjectTypes: drupal10,drupal11
## This script is brought to you by:
## - Valentin Zsigmond (drupal.org/u/vzsigmond)
## - Bill Seremetis (drupal.org/u/bserem)
## - Annertech (annertech.com)
# Determine docroot or fallback to web
if [ -z ${DDEV_DOCROOT+x} ]; then
PROJECT_DOCROOT=web # Sensible default value
else
PROJECT_DOCROOT=$DDEV_DOCROOT
fi
# Function to create a snapshot
create_snapshot() {
if [ -z "$omit_snapshot" ]; then
ddev snapshot
fi
}
# Function to ensure the current branch is recreated from the from-branch
ensure_from_branch() {
local FROM_BRANCH_NAME=$1
local CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$CURRENT_BRANCH" != "$FROM_BRANCH_NAME" ]; then
echo "Resetting current branch $CURRENT_BRANCH from $FROM_BRANCH_NAME"
# Reset the branch from the target branch
git clean -f -d
git fetch origin
git reset --hard "origin/$FROM_BRANCH_NAME"
else
echo "Current branch is already $FROM_BRANCH_NAME, nothing to do."
fi
}
# Function to update a single module
update_module() {
local MODULE_NAME=$1
# Get the current version before updating
local FROM_VERSION
FROM_VERSION=$(ddev composer show "$MODULE_NAME" | grep versions | awk '{print $4}')
if [ "$dry_run" = true ]; then
ddev composer update "$MODULE_NAME" --with-all-dependencies --dry-run
else
# Run the update
ddev composer update "$MODULE_NAME" --with-all-dependencies --bump-after-update --no-progress --quiet
# Get the updated version after the update
local MODULE_VERSION
MODULE_VERSION=$(ddev composer show "$MODULE_NAME" | grep versions | awk '{print $4}')
# Check if the version has changed
if [ "$FROM_VERSION" = "$MODULE_VERSION" ]; then
echo "No update needed for $MODULE_NAME. The version is already $MODULE_VERSION."
else
# Run drush commands if there was an update
if [ "$updb" = true ]; then
ddev drush updb -y
ddev drush cex -y
fi
# Commit if --commit-steps flag is provided
if [ "$commit_steps" = true ]; then
git add -u
git add vendor/
# Strip drupal prefix to get installation path
git add ${PROJECT_DOCROOT}/modules/contrib${MODULE_NAME#drupal}
git add config/
git commit -m "$MODULE_NAME updated from $FROM_VERSION to $MODULE_VERSION (#drupal-updater)"
# Commit any remaining changes in modules/contrib/ (e.g. updated dependencies)
if git status --short "${PROJECT_DOCROOT}/modules/contrib/" | grep -q .; then
git add ${PROJECT_DOCROOT}/modules/contrib/
git commit -m "$MODULE_NAME updated from $FROM_VERSION to $MODULE_VERSION (#drupal-updater) EXTRA FILES"
fi
fi
fi
fi
}
# Function to update all modules and core based on flags
update_all_modules() {
local MODULES
MODULES=$(ddev composer outdated --direct --minor-only | grep "drupal/" | grep -v "drupal/core" | awk '{print $1}')
local CORE_PACKAGES
CORE_PACKAGES=$(ddev composer show | grep "drupal/core" | awk '{print $1}')
for MODULE in $MODULES; do
echo "Updating: " $MODULE
update_module "$MODULE"
done
if [ "$with_core" = true ]; then
for CORE_PACKAGE in $CORE_PACKAGES; do
update_module "$CORE_PACKAGE"
done
fi
# if [ "$commit_steps" != true ]; then
# git add -A
# git commit -m "Updated all applicable Drupal modules and core packages"
# fi
}
# Parse flags and arguments
while [[ $# -gt 0 ]]; do
case $1 in
-*) # Handle grouped short options like -cugado
OPTS="${1:1}" # Remove leading dash
for (( i=0; i<${#OPTS}; i++ )); do
opt="${OPTS:$i:1}"
case $opt in
c) with_core=true ;;
g) commit_steps=true ;;
a) action="update_all" ;;
d) updb=true ;;
o) omit_snapshot=true ;;
*) echo "WARN: Unknown option -$opt" ;;
esac
done
shift
;;
--commit-steps) commit_steps=true; shift ;;
--with-core) with_core=true; shift ;;
--all) action="update_all"; shift ;;
--dry-run) dry_run=true; shift ;;
--omit-snapshot) omit_snapshot=true; shift ;;
--run-db-updates) updb=true; shift ;;
--) shift; break ;;
*) break ;;
esac
done
if [ "$action" = "update_all" ]; then
create_snapshot
update_all_modules
else
create_snapshot
update_module "$1"
fi