Repository navigation
Expand file tree
/
Copy pathmill-functions.zsh
More file actions
218 lines (190 loc) · 7.31 KB
/
Copy pathmill-functions.zsh
File metadata and controls
218 lines (190 loc) · 7.31 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
# Get the available mill binary or script
function mill_executable() {
local millexec
if [[ -x "mill" ]]; then
millexec="./mill"
elif [ -x "$(command -v mill)" ] >/dev/null 2>&1; then
millexec="mill"
else
echo "No mill executable found in the current directory or in PATH."
return 1
fi
"$millexec" "$@"
}
# This function gets the current mill version from .mill-version file or the build files
function get_mill_version() {
# Fast path: .mill-version file
if [ -f ".mill-version" ]; then
head -1 .mill-version
return
fi
# Next: build.mill file (avoid grep if possible)
if [ -f "build.mill" ]; then
awk '/mill-version/ {print $3; exit}' build.mill | tr -d '"' | grep -q . &&
awk '/mill-version/ {print $3; exit}' build.mill | tr -d '"' && return
fi
# Fallback: mill_executable --version (slow)
if [ -f "build.sc" ] || [ -f "build.mill" ] || [ -f "build.mill.scala" ]; then
mill_executable --version 2>/dev/null | awk 'NR==1{print $5; exit}'
return
fi
# Not found
return 1
}
compare_semver() {
local v1="$1"
local v2="$2"
# Parse version components including optional commit count
parse_version() {
local version="$1"
local major minor patch commit_count
# Split by first dash to separate base version from commit info
local base_version="${version%%-*}"
local commit_info="${version#*-}"
# If no dash found, commit_info will equal version
if [ "$commit_info" = "$version" ]; then
commit_count=0
else
# Extract just the first field after dash (commit count)
commit_count="${commit_info%%-*}"
# Validate it's a number, default to 0 if not
case "$commit_count" in
'' | *[!0-9]*) commit_count=0 ;;
esac
fi
# Parse base version (major.minor.patch)
IFS='.' read major minor patch <<<"$base_version"
# Set defaults for missing parts
major=${major:-0}
minor=${minor:-0}
patch=${patch:-0}
echo "$major $minor $patch $commit_count"
}
# Parse both versions
v1_parts=$(parse_version "$v1")
v2_parts=$(parse_version "$v2")
read v1_major v1_minor v1_patch v1_commits <<<"$v1_parts"
read v2_major v2_minor v2_patch v2_commits <<<"$v2_parts"
# Compare major version
if [ "$v1_major" -gt "$v2_major" ]; then
echo "$v1"
return
elif [ "$v1_major" -lt "$v2_major" ]; then
echo "$v2"
return
fi
# Compare minor version
if [ "$v1_minor" -gt "$v2_minor" ]; then
echo "$v1"
return
elif [ "$v1_minor" -lt "$v2_minor" ]; then
echo "$v2"
return
fi
# Compare patch version
if [ "$v1_patch" -gt "$v2_patch" ]; then
echo "$v1"
return
elif [ "$v1_patch" -lt "$v2_patch" ]; then
echo "$v2"
return
fi
# Compare commit count (only if base versions are equal)
if [ "$v1_commits" -gt "$v2_commits" ]; then
echo "$v1"
return
elif [ "$v1_commits" -lt "$v2_commits" ]; then
echo "$v2"
return
fi
# Versions are equal
echo "$v1"
}
# This function is used by Zsh P10k prompt. To use, add `mill_version` in the `p10k.zsh` file:
# typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(
# status # already exists
# ...
# mill_version
# ...
# )
function prompt_mill_version() {
local millver
millver=$(get_mill_version)
if [[ -z "$millver" ]]; then
# If no mill version is found, do not show anything
return
fi
#If keepMajor is true, functions will only use major versions (no daily builds)
keepMajorMillVersion=true
local cache_dir=${XDG_CACHE_HOME:-$HOME/.cache}/p10k-${USER}
mkdir -p "$cache_dir" # just ensuring that it exists
local cache_file="$cache_dir/latest_mill_version"
local cache_file_snap="$cache_dir/latest_mill_version_snapshot"
# Keep cache for 24 hours
local timeout_in_hours=24
local timeout_in_seconds=$(($timeout_in_hours * 60 * 60))
# Get latest version from maven repo if cache is older than timeout
if [[ ! (-f "$cache_file" && -f "$cache_file_snap" && $(($(date +%s) - $(stat -c '%Y' "$cache_file") < $timeout_in_seconds)) -gt 0) ]]; then
local latest_mill_version_maven
latest_mill_version_maven=$(curl -sL https://repo1.maven.org/maven2/com/lihaoyi/mill-libs_3/maven-metadata.xml | grep "<version>" | grep -v "\-M" | tail -1 | sed -e 's/<[^>]*>//g' | tr -d " ")
echo "$latest_mill_version_maven" >"$cache_file_snap"
if [ "$keepMajorMillVersion" = true ]; then
latest_mill_version_maven=$(echo "$latest_mill_version_maven" | cut -d- -f1)
fi
if [[ -n "$latest_mill_version_maven" ]]; then
echo "$latest_mill_version_maven" >"$cache_file"
else
touch "$cache_file"
fi
fi
local latest_mill_version
latest_mill_version=$(<"$cache_file")
latest_mill_version_snap=$(<"$cache_file_snap")
if [[ -n "$latest_mill_version" && $millver =~ \d+.\d+.\d+-\d+-.* && "$millver" == "$latest_mill_version_snap" && "$millver" != $(echo "$latest_mill_version" | cut -d- -f1) ]]; then
# Project uses a snapshot version which is up-to-date, show current version in yellow
p10k segment -s "UP_TO_DATE" -f yellow -i '' -t "⇡ Mill $millver"
elif [[ -n "$latest_mill_version" && $millver =~ \d+.\d+.\d+-\d+-.* && "$millver" != "$latest_mill_version_snap" && "$millver" != $(echo "$latest_mill_version" | cut -d- -f1) ]]; then
# Project uses a snapshot version which is outdated, show current version and latest available snapshot in yellow
p10k segment -s "NOT_UP_TO_DATE" -f red -i '' -t "⇣ Mill $millver [$latest_mill_version_snap]"
elif [[ -n "$latest_mill_version" && "$millver" != "$latest_mill_version" ]]; then
# Mill is not up to date, show current version and latest version brackets in red
p10k segment -s "NOT_UP_TO_DATE" -f red -i '' -t "⇣ Mill $millver [$latest_mill_version]"
else
# Mill is up to date, show current version in blue
p10k segment -s "UP_TO_DATE" -f blue -i '' -t "Mill $millver"
fi
}
update_mill_version() {
local ver=$1
if [ -f ".mill-version" ]; then
echo "$ver" >.mill-version
elif [ -f "build.mill" ]; then
# Replace the mill version in build.mill in line "//| mill-version: 1.0.1"
sed -i -E "s/^\/\/\| mill-version: [0-9]+\.[0-9]+\.[0-9]+$/\/\/\| mill-version: $ver/g" build.mill
fi
}
# Update Scala Mill `.mill-version` file with latest build
# Usage: `millupd` or `millupd -s` to update with latest snapshot build
millupd() {
snapshotBuilds="${1:-true}"
if [ "$snapshotBuilds" = "-s" ]; then
keepMajorMillVersion=false
else
keepMajorMillVersion=true
fi
rm -rf "${XDG_CACHE_HOME:-$HOME/.cache}"/p10k-${USER}/millversion/latest_mill_version
latest_mill_version=$(curl -sL https://repo1.maven.org/maven2/com/lihaoyi/mill-libs_3/maven-metadata.xml | grep "<version>" | grep -v "\-M" | tail -1 | sed -e 's/<[^>]*>//g' | tr -d " ")
echo "Latest mill version is $latest_mill_version..."
if [ "$keepMajorMillVersion" = true ]; then
latest_mill_version=$(echo "$latest_mill_version" | cut -d- -f1)
echo "Will stick to major version $latest_mill_version"
fi
local millver=$(get_mill_version)
if [[ -n "$latest_mill_version" && "$millver" != "$latest_mill_version" ]]; then
echo "Version differs, currently in $millver... updating to $latest_mill_version."
update_mill_version "$latest_mill_version"
echo "Mill version updated to $latest_mill_version."
else
echo "Mill is already up-to-date."
fi
}