diff --git a/Cargo.lock b/Cargo.lock index 4839c92..3a9fb85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,6 +77,15 @@ dependencies = [ "libc", ] +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "anyhow" version = "1.0.86" @@ -301,6 +310,7 @@ dependencies = [ "iana-time-zone", "js-sys", "num-traits 0.2.19", + "serde", "wasm-bindgen", "windows-targets 0.52.5", ] @@ -337,6 +347,30 @@ dependencies = [ "inout", ] +[[package]] +name = "clap" +version = "2.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +dependencies = [ + "ansi_term", + "atty", + "bitflags 1.3.2", + "strsim 0.8.0", + "textwrap 0.11.0", + "unicode-width", + "vec_map", +] + +[[package]] +name = "clap-nested" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faa5865f3ba063d4f7db76f514e14cfa8af09b5f2c584371452a8c86d1c383b4" +dependencies = [ + "clap", +] + [[package]] name = "clipboard-win" version = "5.3.1" @@ -472,7 +506,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim", + "strsim 0.10.0", "syn 1.0.109", ] @@ -653,6 +687,17 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "errno" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + [[package]] name = "errno" version = "0.3.9" @@ -663,6 +708,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "error-code" version = "3.2.0" @@ -750,6 +805,21 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.30" @@ -757,6 +827,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -765,6 +836,34 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.65", +] + [[package]] name = "futures-sink" version = "0.3.30" @@ -783,10 +882,16 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ + "futures-channel", "futures-core", + "futures-io", + "futures-macro", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -882,6 +987,18 @@ dependencies = [ "walkdir", ] +[[package]] +name = "guess_host_triple" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b35a8ce923c7490629d84e12fa2f75e1733f1ec692a47c264f9b7fd632855afc" +dependencies = [ + "errno 0.2.8", + "libc", + "log", + "winapi", +] + [[package]] name = "h2" version = "0.4.5" @@ -1101,6 +1218,33 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" +[[package]] +name = "idf-env" +version = "1.2.31" +source = "git+https://github.com/Hahihula/idf-env.git?rev=f36ff4657a04ab36ff4a3d178570583a8ed8d888#f36ff4657a04ab36ff4a3d178570583a8ed8d888" +dependencies = [ + "anyhow", + "clap", + "clap-nested", + "dirs", + "flate2", + "git2", + "guess_host_triple", + "json", + "md5", + "num_cpus", + "reqwest", + "tar", + "tokio", + "walkdir", + "widestring", + "winapi", + "winreg 0.10.1", + "wmi", + "xz2", + "zip", +] + [[package]] name = "idf-im-lib" version = "0.1.0" @@ -1109,6 +1253,7 @@ dependencies = [ "decompress", "dirs", "git2", + "idf-env", "log", "regex", "reqwest", @@ -1230,6 +1375,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" + [[package]] name = "junction" version = "1.1.0" @@ -1496,6 +1647,12 @@ dependencies = [ "digest", ] +[[package]] +name = "md5" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771" + [[package]] name = "memchr" version = "2.7.2" @@ -2319,7 +2476,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ "bitflags 2.5.0", - "errno", + "errno 0.3.9", "libc", "linux-raw-sys", "windows-sys 0.52.0", @@ -2445,7 +2602,7 @@ dependencies = [ "rustpython-parser-core", "syn 1.0.109", "syn-ext", - "textwrap", + "textwrap 0.15.2", ] [[package]] @@ -2909,6 +3066,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "strsim" version = "0.10.0" @@ -3062,6 +3225,15 @@ dependencies = [ "libc", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "textwrap" version = "0.15.2" @@ -3576,6 +3748,12 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.9.4" @@ -3745,6 +3923,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" dependencies = [ "windows-core", + "windows-implement", + "windows-interface", "windows-targets 0.52.5", ] @@ -3757,6 +3937,28 @@ dependencies = [ "windows-targets 0.52.5", ] +[[package]] +name = "windows-implement" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12168c33176773b86799be25e2a2ba07c7aab9968b37541f1094dbd7a60c8946" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.65", +] + +[[package]] +name = "windows-interface" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d8dc32e0095a7eeccebd0e3f09e9509365ecb3fc6ac4d6f5f14a3f6392942d1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.65", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -3924,6 +4126,20 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "wmi" +version = "0.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc2f0a4062ca522aad4705a2948fd4061b3857537990202a8ddd5af21607f79a" +dependencies = [ + "chrono", + "futures", + "log", + "serde", + "thiserror", + "windows", +] + [[package]] name = "xattr" version = "1.3.1" diff --git a/Cargo.toml b/Cargo.toml index dca777d..e74579d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ sha2 = "0.10.8" log = "0.4.21" dirs = "5.0.1" tera = "1.20.0" +idf-env = { git = "https://github.com/Hahihula/idf-env.git", rev="f36ff4657a04ab36ff4a3d178570583a8ed8d888" } [target.'cfg(windows)'.dependencies] winreg = "0.52.0" diff --git a/src/drivers.rs b/src/drivers.rs new file mode 100644 index 0000000..3605ec9 --- /dev/null +++ b/src/drivers.rs @@ -0,0 +1,105 @@ +use std::path::{Path, PathBuf}; + +use log::error; + +use idf_env::driver::{self, install_driver}; + +use crate::{decompress_archive, download_file, verify_file_checksum}; + +#[derive(Debug, Default, Clone)] + +pub struct Driver { + url: &'static str, + name: &'static str, + file_name: &'static str, + sha256: &'static str, + install_file_name: &'static str, +} + +pub fn get_drivers_list() -> Vec { + // TODO: maintaing the hardcoded sha somewhere besides the downloads + [ + Driver { + url: "https://www.silabs.com/documents/public/software/CP210x_Universal_Windows_Driver.zip", + name: "silabs", + file_name: "cp210x.zip", + sha256: "414345bda1b0149f5daa567abdfa71e6d1a4405b7e0302bbc0dc46319fa154ab", + install_file_name: "silabser.inf", + }, + Driver { + url: "https://www.ftdichip.com/Driver/CDM/CDM%20v2.12.28%20WHQL%20Certified.zip", + name: "ftdi", + file_name: "ftdi.zip", + sha256: "82db36f089d391f194c8ad6494b0bf44c508b176f9d3302777c041dad1ef7fe6", + install_file_name:"ftdiport.inf", + }, + Driver { + url: "https://dl.espressif.com/dl/idf-driver/idf-driver-esp32-usb-jtag-2021-07-15.zip", + name: "espressif", + file_name: "idf-driver-esp32-usb-jtag-2021-07-15.zip", + sha256: "84e741dbec5526e3152bded421b4f06f990cd2d1d7e83b907c40e81f9db0f30e", + install_file_name:"usb_jtag_debug_unit.inf", + }, + Driver { + url: "https://www.wch.cn/downloads/file/314.html", + name: "wch", + file_name: "whc-ch343ser.zip", + sha256: "f57328f58769899aecda4b4192a8c288ab3bfd2198f1e157f4ef14a1b6020b35", + install_file_name:"CH343SER/Driver/CH343SER.INF", + }, + ].to_vec() +} + +pub async fn donwload_drivers( + progress_function: &dyn Fn(u64, u64), + drivers: Vec, + download_dir: &str, +) { + for driver in drivers { + println!("Downloading {}...", driver.name); + let mut file = PathBuf::new(); + file.push(download_dir); + file.push(driver.file_name); + // let download_path = format!("{}/{}", download_dir, driver.name); + match verify_file_checksum(driver.sha256, file.to_str().unwrap()) { + Ok(true) => { + println!("Checksum matched for {}, skipping download.", driver.name); + continue; + } + Ok(false) => { + println!( + "Checksum did not match for {}, downloading again.", + driver.name + ); + } + Err(e) => { + error!("Error verifying checksum for {}: {}", driver.name, e); + continue; + } + } + match download_file( + &driver.url, + download_dir, + &progress_function, + Some(driver.file_name), + ) + .await + { + Ok(_) => { + println!("Download of {} completed successfully.", driver.name); + } + Err(e) => { + error!("Error downloading {}: {}", driver.name, e); + continue; + } + } + let mut decompress_folder = PathBuf::new(); + decompress_folder.push(download_dir); + decompress_folder.push(driver.name); + decompress_archive(file.to_str().unwrap(), decompress_folder.to_str().unwrap()).unwrap(); + let mut install_file = PathBuf::new(); + install_file.push(decompress_folder); + install_file.push(driver.install_file_name); + install_driver(install_file.to_string_lossy().to_string()); + } +} diff --git a/src/lib.rs b/src/lib.rs index 30db692..a4628fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use reqwest::Client; use sha2::{Digest, Sha256}; use tera::{Context, Tera}; +pub mod drivers; pub mod idf_tools; pub mod idf_versions; pub mod python_utils; @@ -270,6 +271,7 @@ pub async fn download_file( url: &str, destination_path: &str, show_progress: &dyn Fn(u64, u64), + to_filename: Option<&str>, ) -> Result<(), std::io::Error> { // Create a new HTTP client let client = Client::new(); @@ -287,7 +289,10 @@ pub async fn download_file( })?; // Extract the filename from the URL - let filename = Path::new(&url).file_name().unwrap().to_str().unwrap(); + let filename = match to_filename { + Some(f) => f, + None => Path::new(&url).file_name().unwrap().to_str().unwrap(), + }; // Create a new file at the specified destination path let mut file = File::create(Path::new(&destination_path).join(Path::new(filename)))?;