mirror of
https://github.com/esp-rs/espup.git
synced 2025-09-29 22:01:07 +00:00
refactor: ♻️ Move version functions to XtensaRust
This commit is contained in:
parent
76c903b9da
commit
52da41d243
28
src/main.rs
28
src/main.rs
@ -17,14 +17,10 @@ use espup::{
|
||||
},
|
||||
gcc::{get_toolchain_name, install_gcc_targets},
|
||||
llvm::Llvm,
|
||||
rust::{
|
||||
check_rust_installation, get_latest_xtensa_rust_version, install_riscv_target, Crate,
|
||||
XtensaRust,
|
||||
},
|
||||
rust::{check_rust_installation, install_riscv_target, Crate, XtensaRust},
|
||||
},
|
||||
};
|
||||
use log::{debug, info, warn};
|
||||
use regex::Regex;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fs::{remove_dir_all, remove_file, File},
|
||||
@ -36,8 +32,6 @@ use std::{
|
||||
const DEFAULT_EXPORT_FILE: &str = "export-esp.ps1";
|
||||
#[cfg(not(windows))]
|
||||
const DEFAULT_EXPORT_FILE: &str = "export-esp.sh";
|
||||
/// Xtensa Rust Toolchain version regex.
|
||||
const RE_TOOLCHAIN_VERSION: &str = r"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)\.(?P<subpatch>0|[1-9]\d*)?$";
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(
|
||||
@ -104,7 +98,7 @@ pub struct InstallOpts {
|
||||
#[arg(short = 't', long, default_value = "all")]
|
||||
pub targets: String,
|
||||
/// Xtensa Rust toolchain version.
|
||||
#[arg(short = 'v', long, value_parser = parse_version)]
|
||||
#[arg(short = 'v', long, value_parser = XtensaRust::parse_version)]
|
||||
pub toolchain_version: Option<String>,
|
||||
}
|
||||
|
||||
@ -117,7 +111,7 @@ pub struct UpdateOpts {
|
||||
#[arg(short = 'l', long, default_value = "info", value_parser = ["debug", "info", "warn", "error"])]
|
||||
pub log_level: String,
|
||||
/// Xtensa Rust toolchain version.
|
||||
#[arg(short = 'v', long, value_parser = parse_version)]
|
||||
#[arg(short = 'v', long, value_parser = XtensaRust::parse_version)]
|
||||
pub toolchain_version: Option<String>,
|
||||
}
|
||||
|
||||
@ -128,18 +122,6 @@ pub struct UninstallOpts {
|
||||
pub log_level: String,
|
||||
}
|
||||
|
||||
/// Parses the version of the Xtensa toolchain.
|
||||
fn parse_version(arg: &str) -> Result<String> {
|
||||
let re = Regex::new(RE_TOOLCHAIN_VERSION).unwrap();
|
||||
if !re.is_match(arg) {
|
||||
bail!(
|
||||
"{} Invalid toolchain version, must be in the form of '<major>.<minor>.<patch>.<subpatch>'",
|
||||
emoji::ERROR
|
||||
);
|
||||
}
|
||||
Ok(arg.to_string())
|
||||
}
|
||||
|
||||
/// Installs the Rust for ESP chips environment
|
||||
fn install(args: InstallOpts) -> Result<()> {
|
||||
initialize_logger(&args.log_level);
|
||||
@ -157,7 +139,7 @@ fn install(args: InstallOpts) -> Result<()> {
|
||||
let xtensa_rust: XtensaRust = if let Some(toolchain_version) = &args.toolchain_version {
|
||||
XtensaRust::new(toolchain_version, &host_triple)
|
||||
} else {
|
||||
let latest_version = get_latest_xtensa_rust_version()?;
|
||||
let latest_version = XtensaRust::get_latest_version()?;
|
||||
XtensaRust::new(&latest_version, &host_triple)
|
||||
};
|
||||
Some(xtensa_rust)
|
||||
@ -318,7 +300,7 @@ fn update(args: UpdateOpts) -> Result<()> {
|
||||
let xtensa_rust: XtensaRust = if let Some(toolchain_version) = args.toolchain_version {
|
||||
XtensaRust::new(&toolchain_version, &host_triple)
|
||||
} else {
|
||||
let latest_version = get_latest_xtensa_rust_version()?;
|
||||
let latest_version = XtensaRust::get_latest_version()?;
|
||||
XtensaRust::new(&latest_version, &host_triple)
|
||||
};
|
||||
|
||||
|
@ -10,14 +10,19 @@ use crate::{
|
||||
use anyhow::{bail, Result};
|
||||
use embuild::cmd;
|
||||
use log::{debug, info, warn};
|
||||
use regex::Regex;
|
||||
use reqwest::header;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::Debug;
|
||||
use std::{env, fs::remove_dir_all, path::PathBuf, process::Stdio};
|
||||
|
||||
/// Xtensa Rust Toolchain repository
|
||||
const DEFAULT_XTENSA_RUST_REPOSITORY: &str =
|
||||
"https://github.com/esp-rs/rust-build/releases/download";
|
||||
/// Xtensa Rust Toolchain API URL
|
||||
const XTENSA_RUST_API_URL: &str = "https://api.github.com/repos/esp-rs/rust-build/releases/latest";
|
||||
/// Xtensa Rust Toolchain version regex.
|
||||
const RE_TOOLCHAIN_VERSION: &str = r"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)\.(?P<subpatch>0|[1-9]\d*)?$";
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct XtensaRust {
|
||||
@ -44,6 +49,30 @@ pub struct XtensaRust {
|
||||
}
|
||||
|
||||
impl XtensaRust {
|
||||
/// Get the latest version of Xtensa Rust toolchain.
|
||||
pub fn get_latest_version() -> Result<String> {
|
||||
let mut headers = header::HeaderMap::new();
|
||||
headers.insert("Accept", "application/vnd.github.v3+json".parse().unwrap());
|
||||
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.user_agent("foo")
|
||||
.build()
|
||||
.unwrap();
|
||||
let res = client
|
||||
.get(XTENSA_RUST_API_URL)
|
||||
.headers(headers)
|
||||
.send()?
|
||||
.text()?;
|
||||
let json: serde_json::Value = serde_json::from_str(&res)?;
|
||||
let mut version = json["tag_name"].to_string();
|
||||
|
||||
version.retain(|c| c != 'v');
|
||||
Self::parse_version(&version)?;
|
||||
debug!("{} Latest Xtensa Rust version: {}", emoji::DEBUG, version);
|
||||
Ok(version)
|
||||
}
|
||||
|
||||
/// Installs the Xtensa Rust toolchain.
|
||||
pub fn install(&self) -> Result<()> {
|
||||
#[cfg(unix)]
|
||||
@ -150,6 +179,18 @@ impl XtensaRust {
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses the version of the Xtensa toolchain.
|
||||
pub fn parse_version(arg: &str) -> Result<String> {
|
||||
let re = Regex::new(RE_TOOLCHAIN_VERSION).unwrap();
|
||||
if !re.is_match(arg) {
|
||||
bail!(
|
||||
"{} Invalid toolchain version, must be in the form of '<major>.<minor>.<patch>.<subpatch>'",
|
||||
emoji::ERROR
|
||||
);
|
||||
}
|
||||
Ok(arg.to_string())
|
||||
}
|
||||
|
||||
/// Removes the Xtensa Rust toolchain.
|
||||
pub fn uninstall(&self) -> Result<()> {
|
||||
info!("{} Uninstalling Xtensa Rust toolchain", emoji::WRENCH);
|
||||
@ -266,26 +307,3 @@ fn install_rust_nightly(version: &str) -> Result<()> {
|
||||
.run()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the latest version of Xtensa Rust toolchain.
|
||||
pub fn get_latest_xtensa_rust_version() -> Result<String> {
|
||||
let mut headers = header::HeaderMap::new();
|
||||
headers.insert("Accept", "application/vnd.github.v3+json".parse().unwrap());
|
||||
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.user_agent("foo")
|
||||
.build()
|
||||
.unwrap();
|
||||
let res = client
|
||||
.get(XTENSA_RUST_API_URL)
|
||||
.headers(headers)
|
||||
.send()?
|
||||
.text()?;
|
||||
let json: serde_json::Value = serde_json::from_str(&res)?;
|
||||
let mut version = json["tag_name"].to_string();
|
||||
|
||||
version.retain(|c| c != 'v');
|
||||
debug!("{} Latest Xtensa Rust version: {}", emoji::DEBUG, version);
|
||||
Ok(version)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user