@@ -14,6 +14,12 @@ r="\033[0m" # Reset
1414
1515AZTEC_HOME=" ${AZTEC_HOME:- $HOME / .aztec} "
1616INSTALL_URI=" ${INSTALL_URI:- https:// install.aztec-labs.com} "
17+ AZTEC_NO_AUTO_UPDATE=" ${AZTEC_NO_AUTO_UPDATE:- } "
18+ AUTO_UPDATE_INTERVAL_SECS=86400 # 24 hours
19+ LAST_UPDATE_CHECK_FILE=" $AZTEC_HOME /.last_update_check"
20+ readonly SELF_UPDATE_UPDATED=0
21+ readonly SELF_UPDATE_FAILED=1
22+ readonly SELF_UPDATE_UP_TO_DATE=2
1723
1824function echo_green {
1925 echo -e " ${g} $1 ${r} "
@@ -37,6 +43,7 @@ function help {
3743 echo " use [<version>] Switch to an installed version (or read from .aztecrc)"
3844 echo " list List installed versions"
3945 echo " uninstall <version> Remove an installed version"
46+ echo " prune Remove all versions except the current one"
4047 echo " self-update Update aztec-up itself to the latest version"
4148 echo " env Output PATH for .aztecrc version (for eval)"
4249 echo
@@ -117,6 +124,72 @@ function resolve_version {
117124 fi
118125}
119126
127+ # Download latest aztec-up and replace if changed.
128+ function download_latest_self {
129+ local self_path=" $AZTEC_HOME /bin/aztec-up"
130+ local tmp_path
131+ tmp_path=$( mktemp)
132+
133+ if ! curl -fsSL " $INSTALL_URI /aztec-up" -o " $tmp_path " ; then
134+ rm -f " $tmp_path "
135+ return " $SELF_UPDATE_FAILED "
136+ fi
137+
138+ # Ensure we didn't get an empty or truncated response
139+ if [ ! -s " $tmp_path " ]; then
140+ rm -f " $tmp_path "
141+ return " $SELF_UPDATE_FAILED "
142+ fi
143+
144+ # Skip replacement if the downloaded script is identical to the current one
145+ if cmp -s " $tmp_path " " $self_path " ; then
146+ rm -f " $tmp_path "
147+ date +%s > " $LAST_UPDATE_CHECK_FILE "
148+ return " $SELF_UPDATE_UP_TO_DATE "
149+ fi
150+
151+ # Replace the current aztec-up binary with the downloaded one
152+ chmod +x " $tmp_path "
153+ if ! mv " $tmp_path " " $self_path " ; then
154+ rm -f " $tmp_path "
155+ return " $SELF_UPDATE_FAILED "
156+ fi
157+
158+ date +%s > " $LAST_UPDATE_CHECK_FILE "
159+ return " $SELF_UPDATE_UPDATED "
160+ }
161+
162+ # Auto-update aztec-up itself if stale
163+ function auto_update {
164+ # Allow users to opt out of auto-update (e.g. in CI or offline environments)
165+ if [ -n " $AZTEC_NO_AUTO_UPDATE " ]; then
166+ return 0
167+ fi
168+
169+ # Skip if we already checked recently (within 24 hours)
170+ local last_check
171+ last_check=$( cat " $LAST_UPDATE_CHECK_FILE " 2> /dev/null || echo 0)
172+ [[ " $last_check " =~ ^[0-9]+$ ]] || last_check=0
173+ local now
174+ now=$( date +%s)
175+ if [ $(( now - last_check)) -lt " $AUTO_UPDATE_INTERVAL_SECS " ]; then
176+ return 0
177+ fi
178+
179+ # Suppress curl output -- auto-update is best-effort
180+ local result=0
181+ download_latest_self 2> /dev/null || result=$?
182+
183+ if [ " $result " -eq " $SELF_UPDATE_UPDATED " ]; then
184+ # The updated script takes effect on the next invocation.
185+ # Current invocation continues with already-parsed old code
186+ # (safe due to the { ... exit } wrapper at the top of the file).
187+ echo_green " aztec-up has been updated. Changes take effect on next run." >&2
188+ elif [ " $result " -eq " $SELF_UPDATE_FAILED " ]; then
189+ echo_yellow " Warning: Failed to check for aztec-up updates" >&2
190+ fi
191+ }
192+
120193# Install a version by downloading and running the version-specific installer
121194function cmd_install {
122195 if [[ " ${1:- } " == " -h" || " ${1:- } " == " --help" ]]; then
@@ -145,6 +218,8 @@ function cmd_install {
145218 exit 1
146219 fi
147220
221+ auto_update
222+
148223 # Resolve alias to actual version
149224 local resolved_version
150225 if ! resolved_version=$( resolve_version " $version " ) ; then
@@ -165,6 +240,12 @@ function cmd_install {
165240
166241 # Update the current symlink
167242 ln -sfn " $AZTEC_HOME /versions/$resolved_version " " $AZTEC_HOME /current"
243+
244+ if [ " $version " != " $resolved_version " ]; then
245+ echo_green " Installed and activated $version ($resolved_version )"
246+ else
247+ echo_green " Installed and activated version $resolved_version "
248+ fi
168249}
169250
170251# Switch to a version
@@ -335,6 +416,68 @@ function cmd_uninstall {
335416 echo_green " Uninstalled version $version "
336417}
337418
419+ # Remove all versions except the current one
420+ function cmd_prune {
421+ if [[ " ${1:- } " == " -h" || " ${1:- } " == " --help" ]]; then
422+ echo " Remove all installed versions except the currently active one"
423+ echo
424+ echo " Usage: aztec-up prune"
425+ echo
426+ echo " Options:"
427+ echo " -h, --help Print help"
428+ echo
429+ echo " Examples:"
430+ echo " aztec-up prune # Remove all versions except current"
431+ return 0
432+ fi
433+
434+ local current_version
435+ current_version=$( get_current_version)
436+
437+ if [ -z " $current_version " ]; then
438+ echo " Error: No version is currently active. Use 'aztec-up use <version>' first."
439+ exit 1
440+ fi
441+
442+ # Collect versions to remove
443+ local to_remove=()
444+ for dir in " $AZTEC_HOME /versions" /* /; do
445+ [ -d " $dir " ] || continue
446+ local version
447+ version=$( basename " $dir " )
448+ if [ " $version " != " $current_version " ]; then
449+ to_remove+=(" $version " )
450+ fi
451+ done
452+
453+ if [ ${# to_remove[@]} -eq 0 ]; then
454+ echo " Nothing to prune. Only the current version ($current_version ) is installed."
455+ return 0
456+ fi
457+
458+ echo " Current version: $current_version "
459+ echo
460+ echo " The following versions will be removed:"
461+ for version in " ${to_remove[@]} " ; do
462+ echo " $version "
463+ done
464+ echo
465+
466+ read -p " Continue? (y/N) " -n 1 -r
467+ echo
468+ if [[ ! $REPLY =~ ^[Yy]$ ]]; then
469+ echo " Aborted."
470+ return 0
471+ fi
472+
473+ for version in " ${to_remove[@]} " ; do
474+ rm -rf " $AZTEC_HOME /versions/$version "
475+ echo " Removed $version "
476+ done
477+
478+ echo_green " Pruned all versions except $current_version "
479+ }
480+
338481# Update aztec-up itself
339482function cmd_self-update {
340483 if [[ " ${1:- } " == " -h" || " ${1:- } " == " --help" ]]; then
@@ -352,22 +495,17 @@ function cmd_self-update {
352495
353496 echo " Updating aztec-up..."
354497
355- local self_path=" $AZTEC_HOME /bin/aztec-up"
356- local tmp_path=$( mktemp)
357-
358- # Download latest aztec-up from root
359- if ! curl -fsSL " $INSTALL_URI /aztec-up" -o " $tmp_path " ; then
498+ local result=0
499+ download_latest_self || result=$?
500+ if [ " $result " -eq " $SELF_UPDATE_FAILED " ]; then
360501 echo " Error: Failed to download latest aztec-up"
361- rm -f " $tmp_path "
362502 exit 1
503+ elif [ " $result " -eq " $SELF_UPDATE_UP_TO_DATE " ]; then
504+ echo_green " aztec-up is already up to date."
505+ else
506+ echo_green " aztec-up updated successfully."
507+ echo " Run 'aztec-up --help' to see available commands."
363508 fi
364-
365- # Replace self
366- chmod +x " $tmp_path "
367- mv " $tmp_path " " $self_path "
368-
369- echo_green " aztec-up updated successfully."
370- echo " Run 'aztec-up --help' to see available commands."
371509}
372510
373511# Output PATH for .aztecrc version (for eval in shell scripts)
@@ -424,7 +562,7 @@ function main {
424562 " " |-h|--help)
425563 help
426564 ;;
427- install|use|list|uninstall|self-update|env)
565+ install|use|list|uninstall|prune| self-update|env)
428566 cmd_$cmd " $@ "
429567 ;;
430568 * )
0 commit comments