From 666de17f795f20530423bb88c912efe45d5a092e Mon Sep 17 00:00:00 2001 From: Oleksandr Zarudnyi Date: Fri, 13 Sep 2024 16:58:03 +0800 Subject: [PATCH] fix: improve logging, rename binary to executable (#21) --- .../src/config/{binary => executable}/mod.rs | 10 ++-- .../config/{binary => executable}/protocol.rs | 4 +- era-compiler-downloader/src/config/mod.rs | 6 +-- era-compiler-downloader/src/lib.rs | 51 +++++++++++++------ 4 files changed, 45 insertions(+), 26 deletions(-) rename era-compiler-downloader/src/config/{binary => executable}/mod.rs (57%) rename era-compiler-downloader/src/config/{binary => executable}/protocol.rs (76%) diff --git a/era-compiler-downloader/src/config/binary/mod.rs b/era-compiler-downloader/src/config/executable/mod.rs similarity index 57% rename from era-compiler-downloader/src/config/binary/mod.rs rename to era-compiler-downloader/src/config/executable/mod.rs index cd4d7b8..102e48e 100644 --- a/era-compiler-downloader/src/config/binary/mod.rs +++ b/era-compiler-downloader/src/config/executable/mod.rs @@ -1,5 +1,5 @@ //! -//! The compiler downloader binary config. +//! The compiler downloader executable config. //! pub mod protocol; @@ -9,16 +9,16 @@ use serde::Deserialize; use self::protocol::Protocol; /// -/// The compiler downloader binary config. +/// The compiler downloader executable config. /// #[derive(Debug, Deserialize)] -pub struct Binary { - /// Whether downloading the binary is enabled. +pub struct Executable { + /// Whether downloading the executable is enabled. pub is_enabled: bool, /// The downloading protocol. pub protocol: Protocol, /// The downloaded data source. pub source: String, - /// The downloaded binary file destination. + /// The downloaded executable file destination. pub destination: String, } diff --git a/era-compiler-downloader/src/config/binary/protocol.rs b/era-compiler-downloader/src/config/executable/protocol.rs similarity index 76% rename from era-compiler-downloader/src/config/binary/protocol.rs rename to era-compiler-downloader/src/config/executable/protocol.rs index 252b691..37cb919 100644 --- a/era-compiler-downloader/src/config/binary/protocol.rs +++ b/era-compiler-downloader/src/config/executable/protocol.rs @@ -1,11 +1,11 @@ //! -//! The compiler downloader binary download protocol. +//! The compiler downloader executable download protocol. //! use serde::Deserialize; /// -/// The compiler downloader binary download protocol. +/// The compiler downloader executable download protocol. /// #[derive(Debug, Deserialize)] #[allow(clippy::upper_case_acronyms)] diff --git a/era-compiler-downloader/src/config/mod.rs b/era-compiler-downloader/src/config/mod.rs index 0c05e0d..7554ac7 100644 --- a/era-compiler-downloader/src/config/mod.rs +++ b/era-compiler-downloader/src/config/mod.rs @@ -2,15 +2,15 @@ //! The compiler downloader config. //! -pub mod binary; pub(crate) mod compiler_list; +pub mod executable; use std::collections::BTreeMap; use std::collections::HashMap; use serde::Deserialize; -use self::binary::Binary; +use self::executable::Executable; /// /// The compiler downloader config. @@ -18,7 +18,7 @@ use self::binary::Binary; #[derive(Debug, Deserialize)] pub struct Config { /// The compiler binaries to download. - pub binaries: BTreeMap, + pub binaries: BTreeMap, /// The compiler platform directory names. pub platforms: Option>, } diff --git a/era-compiler-downloader/src/lib.rs b/era-compiler-downloader/src/lib.rs index bf85bd7..c0723f4 100644 --- a/era-compiler-downloader/src/lib.rs +++ b/era-compiler-downloader/src/lib.rs @@ -9,8 +9,8 @@ use std::str::FromStr; use colored::Colorize; -use self::config::binary::protocol::Protocol; use self::config::compiler_list::CompilerList; +use self::config::executable::protocol::Protocol; use self::config::Config; /// @@ -57,40 +57,49 @@ impl Downloader { let platform_directory = config.get_remote_platform_directory()?; - for (version, binary) in config.binaries.iter() { - if !binary.is_enabled { + for (version, executable) in config.binaries.iter() { + if !executable.is_enabled { continue; } - let source_path = binary + let source_path = executable .source .replace("${PLATFORM}", platform_directory.as_str()) .replace("${VERSION}", version.as_str()) + (std::env::consts::EXE_SUFFIX); - let destination_path = binary.destination.replace("${VERSION}", version.as_str()); + let destination_path = executable + .destination + .replace("${VERSION}", version.as_str()); let destination_path = PathBuf::from_str( format!("{}{}", destination_path, std::env::consts::EXE_SUFFIX).as_str(), ) - .map_err(|_| anyhow::anyhow!("Binary `{}` destination is invalid", destination_path))?; + .map_err(|_| { + anyhow::anyhow!("Executable `{}` destination is invalid", destination_path) + })?; - let data = match binary.protocol { + let data = match executable.protocol { Protocol::File => { if source_path == destination_path.to_string_lossy() { + println!( + " {} executable {:?}. The source and destination are the same.", + "Skipping".bright_green().bold(), + destination_path, + ); continue; } println!( - " {} binary `{}` => {:?}", + " {} executable `{}` => {:?}", "Copying".bright_green().bold(), source_path, destination_path, ); - std::fs::copy(source_path.as_str(), binary.destination.as_str()).map_err( + std::fs::copy(source_path.as_str(), executable.destination.as_str()).map_err( |error| { anyhow::anyhow!( - "Binary {:?} copying error: {}", + "Executable {:?} copying error: {}", source_path.as_str(), error ) @@ -100,13 +109,18 @@ impl Downloader { } Protocol::HTTPS => { if destination_path.exists() { + println!( + " {} executable {:?}. Already exists.", + "Skipping".bright_green().bold(), + destination_path, + ); continue; } let source_url = reqwest::Url::from_str(source_path.as_str()).expect("Always valid"); println!( - " {} binary `{}` => {:?}", + " {} executable `{}` => {:?}", "Downloading".bright_green().bold(), source_url, destination_path, @@ -115,6 +129,11 @@ impl Downloader { } Protocol::CompilerBinList => { if destination_path.exists() { + println!( + " {} executable {:?}. Already exists.", + "Skipping".bright_green().bold(), + destination_path, + ); continue; } @@ -127,23 +146,23 @@ impl Downloader { return Ok(config); } - let source_binary_name = + let source_executable_name = match compiler_list.releases.get(version.to_string().as_str()) { - Some(source_binary_name) => source_binary_name, + Some(source_executable_name) => source_executable_name, None => anyhow::bail!( - "Binary for version v{} not found in the compiler JSON list", + "Executable for version v{} not found in the compiler JSON list", version ), }; let mut source_path = compiler_list_path; source_path.pop(); - source_path.push(source_binary_name); + source_path.push(source_executable_name); let source_url = reqwest::Url::from_str(source_path.to_str().expect("Always valid")) .expect("Always valid"); println!( - " {} binary `{}` => {:?}", + " {} executable `{}` => {:?}", "Downloading".bright_green().bold(), source_url, destination_path,