-
Notifications
You must be signed in to change notification settings - Fork 7
/
clean-release.sh
60 lines (50 loc) · 1.41 KB
/
clean-release.sh
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
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 <name> [number_of_releases_to_keep]"
exit 1
fi
name="$1"
keep_count="${2:-1}"
is_version_newer() {
local ver1=(${1//./ })
local ver2=(${2//./ })
local i
for i in {0..3}; do
if (( ${ver1[i]:-0} > ${ver2[i]:-0} )); then
return 0
elif (( ${ver1[i]:-0} < ${ver2[i]:-0} )); then
return 1
fi
done
return 1
}
declare -A versioned_files
for file in ${name}/${name}*; do
version=$(echo "$file" | grep -oP '\d+\.\d+\.\d+\.\d+')
if [ -n "$version" ]; then
versioned_files["$version"]+="$file "
fi
done
versions=("${!versioned_files[@]}")
for ((i = 0; i < ${#versions[@]}; i++)); do
for ((j = i + 1; j < ${#versions[@]}; j++)); do
if is_version_newer "${versions[j]}" "${versions[i]}"; then
temp="${versions[i]}"
versions[i]="${versions[j]}"
versions[j]="$temp"
fi
done
done
versions_to_keep=("${versions[@]:0:$keep_count}")
echo "Keeping all files for the newest $keep_count versions for $name:"
for version in "${versions_to_keep[@]}"; do
for file in ${versioned_files[$version]}; do
echo "Keeping newer version file: $file"
done
done
for version in "${versions[@]:$keep_count}"; do
for file in ${versioned_files[$version]}; do
echo "Deleting older version file: $file"
sudo rm $file
done
done