Skip to content

Commit

Permalink
fix: improve logging, rename binary to executable
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgar2017 committed Sep 13, 2024
1 parent d0c93a7 commit 4fee77e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//!
//! The compiler downloader binary config.
//! The compiler downloader executable config.
//!

pub mod protocol;
Expand All @@ -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,
}
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
6 changes: 3 additions & 3 deletions era-compiler-downloader/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
//! 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.
///
#[derive(Debug, Deserialize)]
pub struct Config {
/// The compiler binaries to download.
pub binaries: BTreeMap<String, Binary>,
pub binaries: BTreeMap<String, Executable>,
/// The compiler platform directory names.
pub platforms: Option<HashMap<String, String>>,
}
Expand Down
51 changes: 35 additions & 16 deletions era-compiler-downloader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

///
Expand Down Expand Up @@ -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
)
Expand All @@ -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,
Expand All @@ -115,6 +129,11 @@ impl Downloader {
}
Protocol::CompilerBinList => {
if destination_path.exists() {
println!(
" {} executable {:?}. Already exists.",
"Skipping".bright_green().bold(),
destination_path,
);
continue;
}

Expand All @@ -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,
Expand Down

0 comments on commit 4fee77e

Please sign in to comment.