diff --git a/crates/moon/src/cli/version.rs b/crates/moon/src/cli/version.rs index 1c086fcc..4f4707cf 100644 --- a/crates/moon/src/cli/version.rs +++ b/crates/moon/src/cli/version.rs @@ -90,12 +90,12 @@ pub fn run_version(cmd: VersionSubcommand) -> anyhow::Result { (true, false) => { if nopath_flag { println!("moon {}", moon_version); - println!("moonc {}", moonc_version); - println!("moonc {}", moonrun_version); + println!("moonc {}", moonc_version?); + println!("moonc {}", moonrun_version?); } else { println!("moon {} {}", moon_version, get_moon_path()?); - println!("moonc {} {}", moonc_version, get_moonc_path()?); - println!("{} {}", moonrun_version, get_moonrun_path()?); + println!("moonc {} {}", moonc_version?, get_moonc_path()?); + println!("{} {}", moonrun_version?, get_moonrun_path()?); } } (false, true) => { @@ -132,7 +132,7 @@ pub fn run_version(cmd: VersionSubcommand) -> anyhow::Result { }, moonutil::common::VersionItem { name: "moonc".to_string(), - version: moonc_version, + version: moonc_version?, path: if nopath_flag { None } else { @@ -141,7 +141,7 @@ pub fn run_version(cmd: VersionSubcommand) -> anyhow::Result { }, moonutil::common::VersionItem { name: "moonrun".to_string(), - version: moonrun_version, + version: moonrun_version?, path: if nopath_flag { None } else { diff --git a/crates/moonbuild/src/upgrade.rs b/crates/moonbuild/src/upgrade.rs index 6989f173..634bcb76 100644 --- a/crates/moonbuild/src/upgrade.rs +++ b/crates/moonbuild/src/upgrade.rs @@ -22,7 +22,8 @@ use console::Term; use dialoguer::Confirm; use futures::stream::{self, StreamExt, TryStreamExt}; use moonutil::common::{ - get_moon_version, get_moonc_version, CargoPathExt, VersionItems, MOONBITLANG_CORE, + get_moon_version, get_moonc_version, get_moonrun_version, CargoPathExt, VersionItems, + MOONBITLANG_CORE, }; use moonutil::moon_dir::{self, moon_tmp_dir}; use reqwest; @@ -143,16 +144,19 @@ fn test_extract_date() { fn should_upgrade(latest_version_info: &VersionItems) -> Option { let moon_version = get_moon_version(); - let moonc_version = get_moonc_version(); + let moonrun_version = get_moonrun_version().ok()?; + let moonc_version = get_moonc_version().ok()?; // extract date from moon_version and moonc_version, compare with latest let moon_date = extract_date(&moon_version)?; + let moonrun_date = extract_date(&moonrun_version)?; let moonc_date = extract_date(&moonc_version)?; let mut should_upgrade = false; for item in &latest_version_info.items { let latest_date = extract_date(&item.version)?; if ((item.name == "moon") && latest_date > moon_date) + || (item.name == "moonrun" && latest_date > moonrun_date) || (item.name == "moonc" && latest_date > moonc_date) { should_upgrade = true; @@ -179,13 +183,16 @@ pub fn upgrade(cmd: UpgradeSubcommand) -> Result { println!("Checking latest toolchain version ..."); let version_url = format!("{}/version.json", root); - let latest_version_info = reqwest::blocking::get(version_url)?.json::()?; - match should_upgrade(&latest_version_info) { - Some(false) if !cmd.force => { - println!("Your toolchain is up to date."); - return Ok(0); + if !cmd.force { + // if any step(network request, serde json...) fail, just do upgrade + if let Ok(data) = reqwest::blocking::get(version_url) { + if let Ok(latest_version_info) = data.json::() { + if let Some(false) = should_upgrade(&latest_version_info) { + println!("Your toolchain is up to date."); + return Ok(0); + } + } } - _ => {} } println!("{}", "Warning: moon upgrade is highly experimental.".bold()); diff --git a/crates/moonutil/src/common.rs b/crates/moonutil/src/common.rs index 1de81bc3..6f7aba9d 100644 --- a/crates/moonutil/src/common.rs +++ b/crates/moonutil/src/common.rs @@ -21,7 +21,6 @@ use crate::module::{MoonMod, MoonModJSON}; use crate::package::{convert_pkg_json_to_package, MoonPkg, MoonPkgJSON}; use anyhow::{bail, Context}; use clap::ValueEnum; -use colored::Colorize; use fs4::FileExt; use indexmap::IndexMap; use serde::{Deserialize, Serialize}; @@ -626,34 +625,40 @@ pub fn get_moon_version() -> String { ) } -pub fn get_moonc_version() -> String { +pub fn get_moonc_version() -> anyhow::Result { let output = std::process::Command::new("moonc").arg("-v").output(); - if let Ok(output) = &output { - if output.status.success() { - return std::str::from_utf8(&output.stdout) - .unwrap() - .trim() - .to_string(); + match output { + Ok(output) => { + if output.status.success() { + return Ok(std::str::from_utf8(&output.stdout)?.trim().to_string()); + } else { + anyhow::bail!( + "failed to get moonc version: {}", + std::str::from_utf8(&output.stderr)? + ); + } } + Err(e) => anyhow::bail!("failed to get moonc version: {}", e), } - println!("{}: failed to get moonc version", "error".red().bold()); - std::process::exit(1); } -pub fn get_moonrun_version() -> String { +pub fn get_moonrun_version() -> anyhow::Result { let output = std::process::Command::new("moonrun") .arg("--version") .output(); - if let Ok(output) = &output { - if output.status.success() { - return std::str::from_utf8(&output.stdout) - .unwrap() - .trim() - .to_string(); + match output { + Ok(output) => { + if output.status.success() { + return Ok(std::str::from_utf8(&output.stdout)?.trim().to_string()); + } else { + anyhow::bail!( + "failed to get moonrun version: {}", + std::str::from_utf8(&output.stderr)? + ); + } } + Err(e) => anyhow::bail!("failed to get moonrun version: {}", e), } - println!("{}: failed to get moonrun version", "error".red().bold()); - std::process::exit(1); } #[test] @@ -661,7 +666,7 @@ fn test_get_version() { let v = get_moon_version(); println!("moon_version: {}", v); assert!(!v.is_empty()); - let v = get_moonc_version(); + let v = get_moonc_version().unwrap(); println!("moonc_version: {}", v); assert!(!v.is_empty()); } diff --git a/crates/moonutil/src/dirs.rs b/crates/moonutil/src/dirs.rs index 7b65a3ed..d4d17c4a 100644 --- a/crates/moonutil/src/dirs.rs +++ b/crates/moonutil/src/dirs.rs @@ -170,7 +170,7 @@ fn hash_file(path: &str) -> u64 { fn _get_fingerprint(moon_files: &[String], pkg_files: &[String]) -> Fingerprint { let mut fingerprint = Fingerprint { moon_version: get_moon_version(), - moonc_version: get_moonc_version(), + moonc_version: get_moonc_version().unwrap(), moon_pkgs: vec![], mbt_files: HashSet::new(), };