Skip to content

Commit

Permalink
fix upgrade (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash authored Sep 4, 2024
1 parent d83b776 commit 8f6ab08
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
12 changes: 6 additions & 6 deletions crates/moon/src/cli/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ pub fn run_version(cmd: VersionSubcommand) -> anyhow::Result<i32> {
(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) => {
Expand Down Expand Up @@ -132,7 +132,7 @@ pub fn run_version(cmd: VersionSubcommand) -> anyhow::Result<i32> {
},
moonutil::common::VersionItem {
name: "moonc".to_string(),
version: moonc_version,
version: moonc_version?,
path: if nopath_flag {
None
} else {
Expand All @@ -141,7 +141,7 @@ pub fn run_version(cmd: VersionSubcommand) -> anyhow::Result<i32> {
},
moonutil::common::VersionItem {
name: "moonrun".to_string(),
version: moonrun_version,
version: moonrun_version?,
path: if nopath_flag {
None
} else {
Expand Down
23 changes: 15 additions & 8 deletions crates/moonbuild/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,16 +144,19 @@ fn test_extract_date() {

fn should_upgrade(latest_version_info: &VersionItems) -> Option<bool> {
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;
Expand All @@ -179,13 +183,16 @@ pub fn upgrade(cmd: UpgradeSubcommand) -> Result<i32> {

println!("Checking latest toolchain version ...");
let version_url = format!("{}/version.json", root);
let latest_version_info = reqwest::blocking::get(version_url)?.json::<VersionItems>()?;
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::<VersionItems>() {
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());
Expand Down
45 changes: 25 additions & 20 deletions crates/moonutil/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -626,42 +625,48 @@ pub fn get_moon_version() -> String {
)
}

pub fn get_moonc_version() -> String {
pub fn get_moonc_version() -> anyhow::Result<String> {
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<String> {
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]
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());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/moonutil/src/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
Expand Down

0 comments on commit 8f6ab08

Please sign in to comment.