Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ required-features = ["offline"]
tauri-build = { version = "2.3.1", features = [], optional = true }

[dependencies]
reqwest = "0.12.4"
reqwest = { version = "0.12.4", features = ["stream", "json"] }
serde = { version = "1.0", features = ["derive"] }
serde_derive = "1.0"
serde_json = "1.0"
Expand Down Expand Up @@ -82,6 +82,8 @@ once_cell = "1.21.3"
idf-env = { git = "https://github.com/espressif/idf-env", rev="fd69ab4f550ef35647bb32d1584caa6623cbfc4e" }
fs_extra = { version = "1.3.0", optional = true }
lnk = "0.6.3"
futures = "0.3.31"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this added dependency is unused if you do not plan to use it remove it

url = "2.5.7"


# GUI-related dependencies (optional)
Expand Down
144 changes: 110 additions & 34 deletions src-tauri/src/cli/prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use idf_im_lib::settings::Settings;
use idf_im_lib::system_dependencies;
use log::{debug, info};
use rust_i18n::t;
// no runtime creation here; we run inside the app's existing Tokio runtime

use crate::cli::helpers::generic_confirm_with_default;

Expand Down Expand Up @@ -176,47 +177,122 @@ pub fn check_and_install_python(
Ok(())
}

pub fn select_mirrors(mut config: Settings) -> Result<Settings, String> {
if (config.wizard_all_questions.unwrap_or_default()
|| config.idf_mirror.is_none()
|| config.is_default("idf_mirror"))
&& config.non_interactive == Some(false)
pub async fn select_mirrors(mut config: Settings) -> Result<Settings, String> {
// Sort mirrors by latency and produce entries (url, score).
async fn sorted_entries(mirrors: Vec<String>) -> Vec<(String, u32)> {
let latency_map = idf_im_lib::utils::calculate_mirror_latency_map(&mirrors).await;
let mut entries: Vec<(String, u32)> = mirrors
.into_iter()
.map(|m| {
let score = *latency_map.get(&m).unwrap_or(&u32::MAX);
(m, score)
})
.collect();
entries.sort_by(|a, b| {
let ascore = if a.1 == u32::MAX { u32::MAX } else { a.1 };
let bscore = if b.1 == u32::MAX { u32::MAX } else { b.1 };
ascore.cmp(&bscore)
});
entries
}
fn entries_to_display(entries: &[(String, u32)]) -> Vec<String> {
entries
.iter()
.map(|(u, s)| {
if *s == u32::MAX {
format!("{} (timeout)", u)
} else {
format!("{} ({} ms)", u, s)
}
})
.collect()
}

// IDF mirror
if config.non_interactive == Some(false)
&& (config.wizard_all_questions.unwrap_or_default()
|| config.idf_mirror.is_none()
|| config.is_default("idf_mirror"))
{
config.idf_mirror = Some(generic_select(
"wizard.idf.mirror",
&idf_im_lib::get_idf_mirrors_list()
.iter()
.map(|&s| s.to_string())
.collect(),
)?)
let idf_candidates: Vec<String> = idf_im_lib::get_idf_mirrors_list().iter().map(|&s| s.to_string()).collect();
let entries = sorted_entries(idf_candidates).await;
let display = entries_to_display(&entries);
let selected = generic_select("wizard.idf.mirror", &display)?;
let url = selected
.split(" (")
.next()
.unwrap_or(&selected)
.to_string();
config.idf_mirror = Some(url);
} else if config.idf_mirror.is_none() || config.is_default("idf_mirror") {
let idf_candidates: Vec<String> = idf_im_lib::get_idf_mirrors_list().iter().map(|&s| s.to_string()).collect();
let entries = sorted_entries(idf_candidates).await;
if let Some((url, score)) = entries.first() {
if *score == u32::MAX {
info!("Selected IDF mirror: {} (timeout)", url);
} else {
info!("Selected IDF mirror: {} ({} ms)", url, score);
}
config.idf_mirror = Some(url.clone());
}
}

if (config.wizard_all_questions.unwrap_or_default()
|| config.mirror.is_none()
|| config.is_default("mirror"))
&& config.non_interactive == Some(false)
// Tools mirror
if config.non_interactive == Some(false)
&& (config.wizard_all_questions.unwrap_or_default()
|| config.mirror.is_none()
|| config.is_default("mirror"))
{
config.mirror = Some(generic_select(
"wizard.tools.mirror",
&idf_im_lib::get_idf_tools_mirrors_list()
.iter()
.map(|&s| s.to_string())
.collect(),
)?)
let tools_candidates: Vec<String> = idf_im_lib::get_idf_tools_mirrors_list().iter().map(|&s| s.to_string()).collect();
let entries = sorted_entries(tools_candidates).await;
let display = entries_to_display(&entries);
let selected = generic_select("wizard.tools.mirror", &display)?;
let url = selected
.split(" (")
.next()
.unwrap_or(&selected)
.to_string();
config.mirror = Some(url);
} else if config.mirror.is_none() || config.is_default("mirror") {
let tools_candidates: Vec<String> = idf_im_lib::get_idf_tools_mirrors_list().iter().map(|&s| s.to_string()).collect();
let entries = sorted_entries(tools_candidates).await;
if let Some((url, score)) = entries.first() {
if *score == u32::MAX {
info!("Selected Tools mirror: {} (timeout)", url);
} else {
info!("Selected Tools mirror: {} ({} ms)", url, score);
}
config.mirror = Some(url.clone());
}
}

if (config.wizard_all_questions.unwrap_or_default()
|| config.pypi_mirror.is_none()
|| config.is_default("pypi_mirror"))
&& config.non_interactive == Some(false)
// PyPI mirror
if config.non_interactive == Some(false)
&& (config.wizard_all_questions.unwrap_or_default()
|| config.pypi_mirror.is_none()
|| config.is_default("pypi_mirror"))
{
config.pypi_mirror = Some(generic_select(
"wizard.pypi.mirror",
&idf_im_lib::get_pypi_mirrors_list()
.iter()
.map(|&s| s.to_string())
.collect(),
)?)
let pypi_candidates: Vec<String> = idf_im_lib::get_pypi_mirrors_list().iter().map(|&s| s.to_string()).collect();
let entries = sorted_entries(pypi_candidates).await;
let display = entries_to_display(&entries);
let selected = generic_select("wizard.pypi.mirror", &display)?;
let url = selected
.split(" (")
.next()
.unwrap_or(&selected)
.to_string();
config.pypi_mirror = Some(url);
} else if config.pypi_mirror.is_none() || config.is_default("pypi_mirror") {
let pypi_candidates: Vec<String> = idf_im_lib::get_pypi_mirrors_list().iter().map(|&s| s.to_string()).collect();
let entries = sorted_entries(pypi_candidates).await;
if let Some((url, score)) = entries.first() {
if *score == u32::MAX {
info!("Selected PyPI mirror: {} (timeout)", url);
} else {
info!("Selected PyPI mirror: {} ({} ms)", url, score);
}
config.pypi_mirror = Some(url.clone());
}
}

Ok(config)
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/cli/wizard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> {
config = select_targets_and_versions(config).await?;

// mirrors select
config = select_mirrors(config)?;
config = select_mirrors(config).await?;

config = select_installation_path(config)?;

Expand Down
55 changes: 35 additions & 20 deletions src-tauri/src/gui/commands/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::gui::{
utils::is_path_empty_or_nonexistent,
};

use log::info;
use log::{info, warn};
use serde_json::{json, Value};
use std::{
fs::File,
Expand Down Expand Up @@ -37,7 +37,7 @@ pub fn load_settings(app_handle: AppHandle, path: &str) {
})
.expect("Failed to load settings");
log::debug!("settings after load {:?}", settings);
});
}).expect("Failed to update settings");
send_message(
&app_handle,
t!("gui.settings.loaded_successfully", path = path).to_string(),
Expand Down Expand Up @@ -207,7 +207,7 @@ pub fn set_versions(app_handle: AppHandle, versions: Vec<String>) -> Result<(),

/// Gets the list of available IDF mirrors
#[tauri::command]
pub fn get_idf_mirror_list(app_handle: AppHandle) -> Value {
pub async fn get_idf_mirror_list(app_handle: AppHandle) -> Value {
let settings = match get_settings_non_blocking(&app_handle) {
Ok(s) => s,
Err(e) => {
Expand All @@ -220,17 +220,22 @@ pub fn get_idf_mirror_list(app_handle: AppHandle) -> Value {
};

let mirror = settings.idf_mirror.clone().unwrap_or_default();
let mut available_mirrors = idf_im_lib::get_idf_mirrors_list().to_vec();
let mut available_mirrors: Vec<String> = idf_im_lib::get_idf_mirrors_list()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unnecessary allocation, you can work with the &str, no need to copy it over.

.iter()
.map(|s| s.to_string())
.collect();

if !available_mirrors.contains(&mirror.as_str()) {
let mut new_mirrors = vec![mirror.as_str()];
if !available_mirrors.iter().any(|m| m == &mirror) {
let mut new_mirrors = vec![mirror.clone()];
new_mirrors.extend(available_mirrors);
available_mirrors = new_mirrors;
}

// Pick the lowest-latency mirror to present as selected
let mirror_latency_map = idf_im_lib::utils::calculate_mirror_latency_map(&available_mirrors).await;

json!({
"mirrors": available_mirrors,
"selected": mirror,
"mirrors": mirror_latency_map
})
}

Expand All @@ -252,7 +257,7 @@ pub fn set_idf_mirror(app_handle: AppHandle, mirror: String) -> Result<(), Strin

/// Gets the list of available tools mirrors
#[tauri::command]
pub fn get_tools_mirror_list(app_handle: AppHandle) -> Value {
pub async fn get_tools_mirror_list(app_handle: AppHandle) -> Value {
let settings = match get_settings_non_blocking(&app_handle) {
Ok(s) => s,
Err(e) => {
Expand All @@ -265,17 +270,22 @@ pub fn get_tools_mirror_list(app_handle: AppHandle) -> Value {
};

let mirror = settings.mirror.clone().unwrap_or_default();
let mut available_mirrors = idf_im_lib::get_idf_tools_mirrors_list().to_vec();
let mut available_mirrors: Vec<String> = idf_im_lib::get_idf_tools_mirrors_list()
.iter()
.map(|s| s.to_string())
.collect();

if !available_mirrors.contains(&mirror.as_str()) {
let mut new_mirrors = vec![mirror.as_str()];
if !available_mirrors.iter().any(|m| m == &mirror) {
let mut new_mirrors = vec![mirror.clone()];
new_mirrors.extend(available_mirrors);
available_mirrors = new_mirrors;
}

// Pick the lowest-latency mirror to present as selected
let mirror_latency_map = idf_im_lib::utils::calculate_mirror_latency_map(&available_mirrors).await;

json!({
"mirrors": available_mirrors,
"selected": mirror,
"mirrors": mirror_latency_map
})
}

Expand All @@ -297,7 +307,7 @@ pub fn set_tools_mirror(app_handle: AppHandle, mirror: String) -> Result<(), Str

/// Gets the list of available tools mirrors
#[tauri::command]
pub fn get_pypi_mirror_list(app_handle: AppHandle) -> Value {
pub async fn get_pypi_mirror_list(app_handle: AppHandle) -> Value {
let settings = match get_settings_non_blocking(&app_handle) {
Ok(s) => s,
Err(e) => {
Expand All @@ -310,17 +320,22 @@ pub fn get_pypi_mirror_list(app_handle: AppHandle) -> Value {
};

let mirror = settings.pypi_mirror.clone().unwrap_or_default();
let mut available_mirrors = idf_im_lib::get_pypi_mirrors_list().to_vec();
let mut available_mirrors: Vec<String> = idf_im_lib::get_pypi_mirrors_list()
.iter()
.map(|s| s.to_string())
.collect();

if !available_mirrors.contains(&mirror.as_str()) {
let mut new_mirrors = vec![mirror.as_str()];
if !available_mirrors.iter().any(|m| m == &mirror) {
let mut new_mirrors = vec![mirror.clone()];
new_mirrors.extend(available_mirrors);
available_mirrors = new_mirrors;
}

// Pick the lowest-latency mirror to present as selected
let mirror_latency_map = idf_im_lib::utils::calculate_mirror_latency_map(&available_mirrors).await;

json!({
"mirrors": available_mirrors,
"selected": mirror,
"mirrors": mirror_latency_map
})
}

Expand Down
Loading
Loading