diff --git a/src/config.rs b/src/config.rs index 4309a71..c6896e6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -255,6 +255,10 @@ pub fn get_config_path() -> Result { } } +/// Read the list of dependencies from the config file +/// +/// If no config file path is provided, then the path is inferred automatically +/// The returned list is sorted by name and version pub fn read_config_deps(path: Option) -> Result> { let path: PathBuf = match path { Some(p) => p, @@ -270,6 +274,9 @@ pub fn read_config_deps(path: Option) -> Result> { for (name, v) in data { dependencies.push(parse_dependency(name, v)?); } + dependencies + .sort_unstable_by(|a, b| a.name().cmp(b.name()).then_with(|| a.version().cmp(b.version()))); + Ok(dependencies) } diff --git a/src/dependency_downloader.rs b/src/dependency_downloader.rs index 554a730..5a30735 100644 --- a/src/dependency_downloader.rs +++ b/src/dependency_downloader.rs @@ -18,6 +18,9 @@ use yansi::Paint as _; pub type Result = std::result::Result; +/// Download the dependencies from the list in parallel +/// +/// Note: the dependencies list should be sorted by name and version pub async fn download_dependencies( dependencies: &[Dependency], clean: bool, @@ -39,15 +42,17 @@ pub async fn download_dependencies( let mut set = JoinSet::new(); for dep in dependencies { set.spawn({ - let dep = dep.clone(); - async move { download_dependency(&dep, true).await } + let d = dep.clone(); + async move { download_dependency(&d, true).await } }); } - let mut results = Vec::::new(); + let mut results = Vec::new(); while let Some(res) = set.join_next().await { results.push(res??); } + // sort to make the order consistent with the input dependencies list (which should be sorted) + results.sort_unstable_by(|a, b| a.name.cmp(&b.name).then_with(|| a.version.cmp(&b.version))); Ok(results) } @@ -66,6 +71,8 @@ pub fn unzip_dependencies(dependencies: &[Dependency]) -> Result<()> { #[derive(Debug, Clone)] pub struct DownloadResult { + pub name: String, + pub version: String, pub hash: String, pub url: String, } @@ -93,17 +100,24 @@ pub async fn download_dependency( None => get_dependency_url_remote(dependency).await?, }; download_via_http(&url, dep, &dependency_directory).await?; - DownloadResult { hash: sha256_digest(dep), url } + DownloadResult { + name: dep.name.clone(), + version: dep.version.clone(), + hash: sha256_digest(dep), + url, + } } Dependency::Git(dep) => { let hash = download_via_git(dep, &dependency_directory).await?; - DownloadResult { hash, url: dep.git.clone() } + DownloadResult { + name: dep.name.clone(), + version: dep.version.clone(), + hash, + url: dep.git.clone(), + } } }; - println!( - "{}", - format!("Dependency {}-{} downloaded!", dependency.name(), dependency.version()).green() - ); + println!("{}", format!("Dependency {dependency} downloaded!").green()); Ok(res) } diff --git a/src/lib.rs b/src/lib.rs index c742f46..cdd5627 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1021,9 +1021,8 @@ libs = ["dependencies"] } } - let path_dependency = DEPENDENCY_DIR.join("forge-std-1.9.1"); let lock_test = get_current_working_dir().join("test").join("soldeer.lock"); - assert!(path_dependency.exists()); + assert!(find_forge_std_path().exists()); assert!(lock_test.exists()); clean_test_env(target_config); } @@ -1037,14 +1036,12 @@ libs = ["dependencies"] let submodules_path = get_current_working_dir().join(".gitmodules"); let lib_path = get_current_working_dir().join("lib"); - let path_dependency = DEPENDENCY_DIR.join("forge-std-1.9.1"); let lock_test = get_current_working_dir().join("test").join("soldeer.lock"); //remove it just in case let _ = remove_file(&submodules_path); let _ = remove_dir_all(&lib_path); let _ = remove_file(&lock_test); - let _ = remove_dir_all(&path_dependency); let mut file: std::fs::File = fs::OpenOptions::new().create_new(true).write(true).open(&submodules_path).unwrap(); @@ -1073,7 +1070,7 @@ libs = ["dependencies"] } } - assert!(path_dependency.exists()); + assert!(find_forge_std_path().exists()); assert!(lock_test.exists()); assert!(!submodules_path.exists()); assert!(!lib_path.exists()); @@ -1131,4 +1128,16 @@ libs = ["dependencies"] } String::from(target.to_str().unwrap()) } + + fn find_forge_std_path() -> PathBuf { + for entry in fs::read_dir(DEPENDENCY_DIR.clone()).unwrap().filter_map(Result::ok) { + let path = entry.path(); + if path.is_dir() && + path.file_name().unwrap().to_string_lossy().starts_with("forge-std-") + { + return path; + } + } + panic!("could not find forge-std folder in dependency dir"); + } }