-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathupdate-versions.sh
More file actions
executable file
·289 lines (239 loc) · 9.63 KB
/
update-versions.sh
File metadata and controls
executable file
·289 lines (239 loc) · 9.63 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/bash
set -e
# Script to update package versions in Directory.Packages.props based on semver
# Usage: ./update-versions.sh "package1:breaking,package2:feature,package3:fix"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PACKAGES_PROPS_FILE="$SCRIPT_DIR/Directory.Packages.props"
# Check if Directory.Packages.props exists
if [ ! -f "$PACKAGES_PROPS_FILE" ]; then
echo "Error: Directory.Packages.props not found at $PACKAGES_PROPS_FILE"
exit 1
fi
# Function to increment version based on semver
increment_version() {
local version=$1
local change_type=$2
# Parse version components
local major=$(echo "$version" | cut -d'.' -f1)
local minor=$(echo "$version" | cut -d'.' -f2)
local patch=$(echo "$version" | cut -d'.' -f3)
case "$change_type" in
"breaking")
major=$((major + 1))
minor=0
patch=0
;;
"feature")
minor=$((minor + 1))
patch=0
;;
"fix")
patch=$((patch + 1))
;;
*)
echo "Error: Invalid change type '$change_type'. Use: breaking, feature, or fix"
exit 1
;;
esac
echo "$major.$minor.$patch"
}
# Function to get current version for a package
get_current_version() {
local package_id=$1
# Extract Version attribute from <PackageVersion Include="..." Version="..." />
local version=$(grep "Include=\"$package_id\"" "$PACKAGES_PROPS_FILE" | sed 's/.*Version="\([^"]*\)".*/\1/')
if [ -z "$version" ]; then
echo "Error: Could not find version for package '$package_id' in Directory.Packages.props"
exit 1
fi
echo "$version"
}
# Function to update version in Directory.Packages.props
update_version_in_props() {
local package_id=$1
local new_version=$2
# Update the Version attribute for the matching PackageVersion entry
sed -i "s|\(Include=\"$package_id\" Version=\"\)[^\"]*\(\"\)|\1$new_version\2|" "$PACKAGES_PROPS_FILE"
echo "Updated $package_id to $new_version"
}
# Caches to avoid repeated expensive operations
declare -A project_dependencies_cache
declare -A project_package_id_cache
# Preload all project dependencies to avoid repeated dotnet calls
preload_all_dependencies() {
local projects=$(get_all_projects)
while IFS= read -r project_path; do
if [ -n "$project_path" ] && [ -z "${project_dependencies_cache[$project_path]}" ]; then
local deps_array=()
# Get project references and resolve to package IDs
while IFS= read -r line; do
if [[ "$line" == *.csproj ]]; then
# Normalize Windows-style paths to Unix-style
local ref_project_path=$(echo "$line" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//' | sed 's/\\/\//g')
# Resolve relative paths
if [ ! -f "$ref_project_path" ]; then
ref_project_path="$(dirname "$project_path")/$ref_project_path"
fi
if [ -f "$ref_project_path" ]; then
local package_id=$(get_project_package_id "$ref_project_path")
if [ -n "$package_id" ] && [[ "$package_id" == War3Net* ]]; then
deps_array+=("$package_id")
fi
fi
fi
done <<< "$(dotnet list "$project_path" reference 2>/dev/null)"
local deps_result=$(printf '%s\n' "${deps_array[@]}" | sort -u | tr '\n' ' ' | sed 's/ $//')
project_dependencies_cache["$project_path"]="$deps_result"
fi
done <<< "$projects"
}
# Get package ID for a project (cached)
get_project_package_id() {
local project_path=$1
if [ -n "${project_package_id_cache[$project_path]}" ]; then
echo "${project_package_id_cache[$project_path]}"
return
fi
if [ ! -f "$project_path" ]; then
project_package_id_cache["$project_path"]=""
return
fi
# Extract PackageId from project file or derive from directory name
local package_id=$(grep "<PackageId>" "$project_path" | sed 's/.*<PackageId>\(.*\)<\/PackageId>.*/\1/' | head -n1)
if [ -z "$package_id" ]; then
local project_name=$(basename "$(dirname "$project_path")")
if [[ "$project_name" == War3Net* ]]; then
package_id="$project_name"
fi
fi
project_package_id_cache["$project_path"]="$package_id"
echo "$package_id"
}
# Get cached project dependencies
get_project_dependencies() {
echo "${project_dependencies_cache[$1]}"
}
# Get all packable projects from solution filter
get_all_projects() {
grep '\.csproj"' War3NetPublish.slnf | sed 's/.*"\(.*\)".*/\1/' | sed 's/\\/\//g'
}
# Find projects that depend on a given project
get_dependent_projects() {
local target_project=$1
local dependents=""
local projects=$(get_all_projects)
while IFS= read -r project_path; do
if [ -n "$project_path" ]; then
local deps=$(get_project_dependencies "$project_path")
if echo "$deps" | grep -q "\b$target_project\b"; then
local package_id=$(get_project_package_id "$project_path")
if [ -n "$package_id" ] && [[ "$package_id" == War3Net* ]]; then
dependents="$dependents$package_id;"
fi
fi
fi
done <<< "$projects"
echo "$dependents"
}
# Determine version bumps for all affected projects
determine_version_bumps() {
preload_all_dependencies
declare -A version_bumps
declare -A processed
local updates_made=true
local max_iterations=10
local iteration=0
# Apply direct updates from user input
if [ -n "$DIRECT_UPDATES" ]; then
IFS=',' read -ra UPDATE_PAIRS <<< "$DIRECT_UPDATES"
for pair in "${UPDATE_PAIRS[@]}"; do
IFS=':' read -ra PARTS <<< "$pair"
local package_id="${PARTS[0]}"
local change_type="${PARTS[1]}"
version_bumps["$package_id"]="$change_type"
done
fi
# Determine cascading version bumps
while [ "$updates_made" = true ] && [ $iteration -lt $max_iterations ]; do
updates_made=false
iteration=$((iteration + 1))
echo "=== Determining cascading updates - Iteration $iteration ===" >&2
# Process each project with a version bump
for project in "${!version_bumps[@]}"; do
if [ -z "${processed[$project]}" ]; then
processed["$project"]=true
# Find projects that depend on this one and update their version bumps
local dependents=$(get_dependent_projects "$project")
while IFS= read -r dependent; do
if [ -n "$dependent" ]; then
local current_bump="${version_bumps[$dependent]}"
local required_bump="${version_bumps[$project]}"
# Determine the more significant bump type
local new_bump=""
if [ -z "$current_bump" ]; then
new_bump="$required_bump"
else
case "$current_bump:$required_bump" in
"fix:feature"|"fix:breaking"|"feature:breaking")
new_bump="$required_bump"
;;
*)
new_bump="$current_bump"
;;
esac
fi
if [ "$new_bump" != "$current_bump" ]; then
version_bumps["$dependent"]="$new_bump"
updates_made=true
echo " → $dependent needs $new_bump update due to $project" >&2
fi
fi
done <<< "$(echo "$dependents" | tr ';' '\n')"
fi
done
done
if [ $iteration -eq $max_iterations ]; then
echo "Warning: Maximum iterations reached while determining version bumps." >&2
fi
# Return the associative array by printing it
for project in "${!version_bumps[@]}"; do
echo "$project:${version_bumps[$project]}"
done
}
# Main function to process version updates
update_versions_recursive() {
echo "=== Determining all version bumps ==="
# Get all version bumps first
local bump_list=$(determine_version_bumps)
echo ""
echo "=== Applying version updates ==="
# Apply all version bumps
while IFS= read -r line; do
if [ -n "$line" ]; then
IFS=':' read -ra PARTS <<< "$line"
local package_id="${PARTS[0]}"
local change_type="${PARTS[1]}"
local current_version=$(get_current_version "$package_id")
local new_version=$(increment_version "$current_version" "$change_type")
update_version_in_props "$package_id" "$new_version"
fi
done <<< "$bump_list"
}
# Parse command line arguments
if [ $# -ne 1 ]; then
echo "Usage: $0 \"package1:change_type,package2:change_type,...\""
echo ""
echo "Change types: breaking, feature, fix"
echo "Example: $0 \"War3Net.Common:feature,War3Net.IO.Mpq:fix\""
echo ""
echo "Note: Use actual NuGet package IDs (e.g., War3Net.CSharpLua, not CSharp.lua)"
exit 1
fi
DIRECT_UPDATES="$1"
echo "=== War3Net Version Update Script ==="
echo "Processing updates: $DIRECT_UPDATES"
# Process updates recursively
update_versions_recursive
echo ""
echo "=== Version Update Complete ==="
echo "All version updates have been applied to Directory.Packages.props"