From e0bc2f399f1a886d1469396a4524a252c4abe048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 9 Aug 2024 11:40:19 +0900 Subject: [PATCH] fix(cargo-link): Patch all packages (#46) --- Cargo.lock | 2 +- apps/cargo-link2/Cargo.toml | 2 +- apps/cargo-link2/src/main.rs | 21 ++++++++++++--------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a19ad98..7bfc73f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -164,7 +164,7 @@ dependencies = [ [[package]] name = "cargo-link2" -version = "0.1.3" +version = "0.1.4" dependencies = [ "anyhow", "cargo-subcommand-metadata", diff --git a/apps/cargo-link2/Cargo.toml b/apps/cargo-link2/Cargo.toml index c5ac507..431d312 100644 --- a/apps/cargo-link2/Cargo.toml +++ b/apps/cargo-link2/Cargo.toml @@ -5,7 +5,7 @@ edition = { workspace = true } license = { workspace = true } name = "cargo-link2" repository = { workspace = true } -version = "0.1.3" +version = "0.1.4" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/apps/cargo-link2/src/main.rs b/apps/cargo-link2/src/main.rs index 12b02a1..b8aa899 100644 --- a/apps/cargo-link2/src/main.rs +++ b/apps/cargo-link2/src/main.rs @@ -84,7 +84,7 @@ fn list_of_crates(target_dir: &Path) -> Result> { .collect()) } -fn add_patch_section(working_dir: &Path, link_candidates: &[PatchPkg]) -> Result> { +fn add_patch_section(working_dir: &Path, link_candidates: &[PatchPkg]) -> Result> { let md = MetadataCommand::new() .current_dir(working_dir) .exec() @@ -112,7 +112,7 @@ fn add_patch_section(working_dir: &Path, link_candidates: &[PatchPkg]) -> Result ) })?; - let (crates_to_link, all_deps) = find_used_crates(&md, link_candidates) + let (_, all_deps) = find_used_crates(&md, link_candidates) .with_context(|| format!("failed to find used crates in '{}'", working_dir.display()))?; if doc.get("patch").is_none() { @@ -126,7 +126,7 @@ fn add_patch_section(working_dir: &Path, link_candidates: &[PatchPkg]) -> Result let crates_io = patch["crates-io"].as_table_mut().unwrap(); - for PatchPkg { name, path } in &crates_to_link { + for PatchPkg { name, path } in &all_deps { let mut v = toml_edit::table(); v["path"] = toml_edit::value(path.display().to_string()); crates_io[&**name] = v; @@ -150,7 +150,7 @@ fn find_root_manifest_path(md: &Metadata) -> Result { fn find_used_crates( md: &Metadata, link_candidates: &[PatchPkg], -) -> Result<(Vec, Vec)> { +) -> Result<(Vec, Vec)> { let mut direct_deps = HashSet::new(); let mut all_deps = HashSet::new(); @@ -176,19 +176,22 @@ fn find_used_crates( let mut direct_deps = direct_deps.into_iter().collect::>(); direct_deps.sort(); - let mut all_deps = all_deps.into_iter().collect::>(); - all_deps.sort(); + let all_pkgs = link_candidates + .iter() + .filter(|c| all_deps.contains(&c.name)) + .cloned() + .collect::>(); - Ok((direct_deps, all_deps)) + Ok((direct_deps, all_pkgs)) } -fn run_cargo_update(dir: &PathBuf, crates: &[String]) -> Result<()> { +fn run_cargo_update(dir: &PathBuf, crates: &[PatchPkg]) -> Result<()> { let mut cmd = std::process::Command::new(cargo_bin()); cmd.current_dir(dir); cmd.arg("update"); for pkg in crates { cmd.arg("--package"); - cmd.arg(pkg); + cmd.arg(&pkg.name); } eprintln!("Running: {:?}", cmd);