Repository navigation
Expand file tree
/
Copy pathupdate-version.sh
More file actions
executable file
·269 lines (235 loc) · 8.14 KB
/
Copy pathupdate-version.sh
File metadata and controls
executable file
·269 lines (235 loc) · 8.14 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash
# ============================================================================
# Version Update Script
# ============================================================================
#
# PURPOSE:
# Updates version numbers in project files and creates git commits/tags.
#
# USAGE:
# ./update-version.sh [-f|--force] [-n|--dry-run] <version|major|minor|patch>
#
# OPTIONS:
# -f, --force Force update even if the new version is the same or lower
# -n, --dry-run Do everything except creating git commits and tags
# -h, --help Show help instructions
#
# ARGUMENTS:
# version Specific version number (e.g., 1.2.3)
# major Increment major version (e.g., 1.2.3 -> 2.0.0)
# minor Increment minor version (e.g., 1.2.3 -> 1.3.0)
# patch Increment patch version (e.g., 1.2.3 -> 1.2.4)
#
# EXAMPLES:
# # Increment patch version (1.2.3 -> 1.2.4)
# ./update-version.sh patch
#
# # Increment minor version (1.2.3 -> 1.3.0)
# ./update-version.sh minor
#
# # Increment major version (1.2.3 -> 2.0.0)
# ./update-version.sh major
#
# # Set a specific version
# ./update-version.sh 1.5.0
#
# # Force update to a specific version (even if lower or same)
# ./update-version.sh --force 1.2.3
#
# # Dry-run to see what would happen without making changes
# ./update-version.sh --dry-run minor
#
# # Show help
# ./update-version.sh --help
#
# WHAT IT DOES:
# 1. Reads current version from ./order-daemon.php
# 2. Updates version in:
# - ./order-daemon.php (Version header and ODCM_VERSION constant)
# - ./README.txt (Stable tag)
# 3. Replaces "@since next" placeholders with the new version
# 4. Creates a git commit with the version as the message
# 5. Creates an annotated git tag (e.g., v1.2.3)
#
# AFTER RUNNING:
# To push the versioned code to the remote repository, run:
# git push && git push --tags
#
# REQUIREMENTS:
# - Git must be initialized in the project
# - ./order-daemon.php must exist with a Version header
# - ./README.txt must exist with a Stable tag
#
# ============================================================================
show_instructions() {
echo "Usage: $0 [-f|--force] [-n|--dry-run] <version|major|minor|patch>"
echo "Options:"
echo " -f, --force Force update even if the new version is the same or lower"
echo " -n, --dry-run Do everything except creating git commits and tags"
echo " -h, --help Show these instructions"
echo "Examples:"
echo " $0 patch"
echo " $0 1.2.3"
echo " $0 --force 1.2.3"
echo " $0 --dry-run minor"
echo " $0 --help"
}
get_current_version() {
local current_version=""
current_version=$(grep -Eo 'Version:[[:space:]]*[0-9]+\.[0-9]+\.[0-9]+' "$file_name" | awk '{print $2}')
if [[ -z "$current_version" ]]; then
echo "Could not find current version in $file_name."
exit 1
fi
echo "Current version: $current_version"
echo "$current_version"
}
get_incremented_version() {
local current_version="$1"
local target_segment="${2:-patch}" # accepted: major|minor|patch
IFS='.' read -r -a version_parts <<<"$current_version"
local major="${version_parts[0]:-0}"
local minor="${version_parts[1]:-0}"
local patch="${version_parts[2]:-0}"
case "$target_segment" in
major)
major=$((major + 1))
minor=0
patch=0
;;
minor)
minor=$((minor + 1))
patch=0
;;
patch)
patch=$((patch + 1))
;;
*)
echo "Invalid target segment: $target_segment" >&2
return 1
;;
esac
echo "$major.$minor.$patch"
}
# Detect GNU vs BSD sed for in-place editing
setup_sed_inplace() {
if sed --version >/dev/null 2>&1; then
SED_INPLACE=(-i) # GNU sed
else
SED_INPLACE=(-i '') # BSD/macOS sed (empty backup extension)
fi
}
update_version_and_commit() {
local file_name="$1"
local current_version="$2"
local requested_version="$3"
local force="${4:-false}"
local dry_run="${5:-false}"
echo "Updating version from $current_version to $requested_version..."
sed -E "${SED_INPLACE[@]}" \
"s/(Version:)[[:space:]]*[0-9]+\.[0-9]+\.[0-9]+/\1 $requested_version/g" "$file_name"
sed -E "${SED_INPLACE[@]}" \
"s/(define\([[:space:]]*'ODCM_VERSION',[[:space:]]*')[0-9]+\.[0-9]+\.[0-9]+(')/\1$requested_version\2/g" "$file_name"
sed -E "${SED_INPLACE[@]}" \
"s/(Stable tag:)[[:space:]]*[0-9]+\.[0-9]+\.[0-9]+/\1 $requested_version/g" "./README.txt"
# Replace common placeholders used before release
# Matches: @since next
while IFS= read -r -d '' file; do
sed -E "${SED_INPLACE[@]}" \
-e "s/@since([[:space:]]+)next/@since\1$requested_version/g" \
"$file"
done < <(find . -type f \( -name "*.php" -o -name "*.js" -o -name "*.css" \) -print0)
if [[ "$dry_run" == true ]]; then
echo "[dry-run] Skipping git add/commit and tag creation."
echo "[dry-run] Would have created commit with message: $requested_version"
echo "[dry-run] Would have created tag: v$requested_version"
return 0
fi
git add -A
git commit --allow-empty -m "$requested_version"
tag="v$requested_version"
if [[ "$force" == true ]]; then
echo "Forcing tag $tag to point to the new commit."
git tag -a -f "$tag" -m "$tag"
else
echo "Adding version tag $tag."
git tag -a "$tag" -m "$tag"
fi
}
main() {
local file_name="./order-daemon.php"
local current_version=""
current_version=$(grep -Eo 'Version:[[:space:]]*[0-9]+\.[0-9]+\.[0-9]+' "$file_name" | awk '{print $2}')
if [[ -z "$current_version" ]]; then
echo "Could not find current version in $file_name."
exit 1
else
echo "Current version: $current_version"
fi
if [[ $# -eq 0 ]]; then
show_instructions
exit 1
fi
local requested_version=""
local force=false
local dry_run=false
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
show_instructions
exit 1
;;
-f|--force)
force=true
shift
;;
-n|--dry-run)
dry_run=true
echo "Dry-run mode enabled: commits and tags will NOT be created."
shift
;;
*)
if [[ -z "$requested_version" ]]; then
requested_version="$1"
shift
else
echo "Unknown argument: $1"
show_instructions
exit 1
fi
;;
esac
done
setup_sed_inplace
if [[ -z "$requested_version" ]]; then
show_instructions
exit 1
fi
local normalized_version=""
if [[ "$requested_version" =~ ^(major|minor|patch)$ && -n "$current_version" ]]; then
normalized_version="$(get_incremented_version "$current_version" "$requested_version")"
echo "Bumping $requested_version version."
else
normalized_version="$requested_version"
fi
if ! [[ "$normalized_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Invalid version: $requested_version"
exit 1
fi
if [[ "$force" == true ]]; then
echo "Forcing version update."
update_version_and_commit "$file_name" "$current_version" "$normalized_version" true "$dry_run"
exit 0
fi
if [[ "$current_version" == "$normalized_version" ]]; then
echo -e "Current version ($current_version) is the same as new version ($normalized_version).\nNo update needed."
exit 0
elif [[ "$(echo -e "$normalized_version\n$current_version" | sort -V | head -n 1)" == "$normalized_version" ]]; then
echo -e "New version ($normalized_version) is less than current version ($current_version).\nVersion must be incremented."
exit 1
else
update_version_and_commit "$file_name" "$current_version" "$normalized_version" false "$dry_run"
exit 0
fi
}
main "$@"