Repository navigation
Expand file tree
/
Copy pathupdate-version
More file actions
executable file
·120 lines (101 loc) · 3.27 KB
/
Copy pathupdate-version
File metadata and controls
executable file
·120 lines (101 loc) · 3.27 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
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'HELP'
Usage:
up-version major
up-version minor
up-version patch
up-version 1.0.2
up-version --version 1.0.2
Note:
Run this from the project root (the directory containing Cargo.toml).
Updates:
- Cargo.toml ([package].version)
- src/ui.rs (logo "OMARCHYiso vX.Y.Z")
HELP
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" || -z "${1:-}" ]]; then
usage
exit 0
fi
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
# Only accept running from the project root.
cwd="$(pwd -P)"
if [[ ! -f "${cwd}/Cargo.toml" || ! -f "${cwd}/src/ui.rs" ]]; then
echo "Error: run this from the project root (directory containing Cargo.toml and src/ui.rs)."
echo "Hint: cd ${script_dir}"
exit 1
fi
# Ensure we're in the same project directory where this script lives.
if [[ "${cwd}" != "${script_dir}" ]]; then
echo "Error: run this from ${script_dir}"
exit 1
fi
app_dir="${cwd}"
cargo_toml="${app_dir}/Cargo.toml"
ui_rs="${app_dir}/src/ui.rs"
# Extract current version
# Looks for line: version = "X.Y.Z" under [package] ideally, but grep is simpler
current_version=$(grep -m1 '^version = "[0-9]\+\.[0-9]\+\.[0-9]\+"' "${cargo_toml}" | sed -E 's/version = "([0-9]+\.[0-9]+\.[0-9]+)"/\1/' || true)
if [[ -z "${current_version}" ]]; then
# Fallback for spacing variations
current_version=$(grep -m1 '^version[[:space:]]*=[[:space:]]*"[0-9]\+\.[0-9]\+\.[0-9]\+"' "${cargo_toml}" | sed -E 's/version[[:space:]]*=[[:space:]]*"([0-9]+\.[0-9]+\.[0-9]+)"/\1/' || true)
fi
if [[ -z "${current_version}" ]]; then
echo "Error: Could not extract current version from Cargo.toml"
exit 1
fi
version=""
input="${1:-}"
if [[ "$input" == "major" ]]; then
echo "Current version: ${current_version}"
IFS='.' read -r major minor patch <<< "$current_version"
version="$((major + 1)).0.0"
elif [[ "$input" == "minor" ]]; then
echo "Current version: ${current_version}"
IFS='.' read -r major minor patch <<< "$current_version"
version="${major}.$((minor + 1)).0"
elif [[ "$input" == "patch" ]]; then
echo "Current version: ${current_version}"
IFS='.' read -r major minor patch <<< "$current_version"
version="${major}.${minor}.$((patch + 1))"
else
case "${input}" in
--version)
version="${2:-}"
;;
--*)
version="${input#--}"
;;
*)
version="${input}"
;;
esac
fi
if [[ -z "${version}" ]]; then
echo "Error: missing version."
echo
usage
exit 1
fi
# Simple SemVer-ish validation: X.Y.Z where each is numeric
if ! [[ "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Error: invalid version '${version}'. Expected format: X.Y.Z (e.g. 1.0.2)"
exit 1
fi
echo "Updating version to ${version}"
echo " Cargo.toml: ${cargo_toml}"
echo " ui.rs: ${ui_rs}"
# Update Cargo.toml version only inside the [package] section
NEW_VERSION="${version}" perl -0777 -i -pe '
my $v = $ENV{NEW_VERSION};
s/(\[package\][^\[]*?\bversion\s*=\s*")[^"]*(")/$1$v$2/s
or die "Failed to update [package].version in Cargo.toml\n";
' "${cargo_toml}"
# Update ui.rs logo line: "OMARCHYiso v1.0.1" -> "OMARCHYiso v<version>"
NEW_VERSION="${version}" perl -i -pe '
my $v = $ENV{NEW_VERSION};
s/(OMARCHYiso v)\d+\.\d+\.\d+/${1}$v/g;
' "${ui_rs}"
echo "Done."