|
| 1 | +// Copyright (c) Aptos Foundation |
| 2 | +// Licensed pursuant to the Innovation-Enabling Source Code License, available at https://github.com/aptos-labs/aptos-core/blob/main/LICENSE |
| 3 | + |
| 4 | +use anyhow::{anyhow, Context, Result}; |
| 5 | +use clap::Args; |
| 6 | +use self_update::{ |
| 7 | + backends::github::{ReleaseList, Update}, |
| 8 | + version::bump_is_greater, |
| 9 | +}; |
| 10 | + |
| 11 | +#[derive(Args, Debug, Clone)] |
| 12 | +pub struct UpdateArgs { |
| 13 | + /// Check for updates without downloading. |
| 14 | + #[arg(long)] |
| 15 | + pub check: bool, |
| 16 | + |
| 17 | + #[arg(long, default_value = "aptos-labs", hide = true)] |
| 18 | + pub repo_owner: String, |
| 19 | + |
| 20 | + #[arg(long, default_value = "aptos-ai", hide = true)] |
| 21 | + pub repo_name: String, |
| 22 | +} |
| 23 | + |
| 24 | +/// `bump_is_greater` only handles `x.y.z`; strip suffixes it can't parse. |
| 25 | +pub(crate) fn strip_prerelease(v: &str) -> &str { |
| 26 | + if let Some(stem) = v.strip_suffix(".beta").or_else(|| v.strip_suffix(".rc")) { |
| 27 | + stem |
| 28 | + } else { |
| 29 | + v |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub fn run(args: &UpdateArgs) -> Result<()> { |
| 34 | + let releases = ReleaseList::configure() |
| 35 | + .repo_owner(&args.repo_owner) |
| 36 | + .repo_name(&args.repo_name) |
| 37 | + .build() |
| 38 | + .context("failed to configure release list")? |
| 39 | + .fetch() |
| 40 | + .context("failed to fetch releases from GitHub")?; |
| 41 | + |
| 42 | + let latest = releases |
| 43 | + .into_iter() |
| 44 | + .find(|r| r.version.starts_with("move-flow-v")) |
| 45 | + .ok_or_else(|| { |
| 46 | + anyhow!( |
| 47 | + "no move-flow release found in {}/{}", |
| 48 | + args.repo_owner, |
| 49 | + args.repo_name |
| 50 | + ) |
| 51 | + })?; |
| 52 | + |
| 53 | + let latest_version = &latest.version["move-flow-v".len()..]; |
| 54 | + let current_version = env!("CARGO_PKG_VERSION"); |
| 55 | + let needs_update = bump_is_greater(current_version, strip_prerelease(latest_version)) |
| 56 | + .context("failed to compare versions")?; |
| 57 | + |
| 58 | + if args.check { |
| 59 | + if needs_update { |
| 60 | + println!("Update available: v{latest_version}"); |
| 61 | + } else { |
| 62 | + println!("Already up to date (v{current_version})"); |
| 63 | + } |
| 64 | + return Ok(()); |
| 65 | + } |
| 66 | + |
| 67 | + if !needs_update { |
| 68 | + println!("Already up to date (v{current_version})"); |
| 69 | + return Ok(()); |
| 70 | + } |
| 71 | + |
| 72 | + Update::configure() |
| 73 | + .repo_owner(&args.repo_owner) |
| 74 | + .repo_name(&args.repo_name) |
| 75 | + .bin_name("move-flow") |
| 76 | + .current_version(current_version) |
| 77 | + .target_version_tag(&latest.version) |
| 78 | + .no_confirm(true) |
| 79 | + .build() |
| 80 | + .context("failed to configure updater")? |
| 81 | + .update() |
| 82 | + .map_err(|e| { |
| 83 | + let msg = e.to_string(); |
| 84 | + if msg.contains("permission denied") || msg.contains("Access is denied") { |
| 85 | + anyhow!("cannot replace binary: permission denied (try sudo)") |
| 86 | + } else if msg.contains("could not find binary") || msg.contains("no asset") { |
| 87 | + anyhow!( |
| 88 | + "no release asset for the current platform — download manually from \ |
| 89 | + https://github.com/{}/{}/releases", |
| 90 | + args.repo_owner, |
| 91 | + args.repo_name |
| 92 | + ) |
| 93 | + } else { |
| 94 | + anyhow!("update failed: {e:#}") |
| 95 | + } |
| 96 | + })?; |
| 97 | + |
| 98 | + println!("Updated move-flow v{current_version} → v{latest_version}"); |
| 99 | + Ok(()) |
| 100 | +} |
0 commit comments