Closed BatmanAoD closed 7 months ago
Okay, this is reasonably easy with cargo_metadata
.
let resolved_crates = MetadataCommand::new().exec()?.resolve.ok_or_else(|| anyhow!("could not load Cargo metadata"))?;
let root_id = resolved_crates.root.ok_or_else(|| anyhow!("cargo metadata missing '.resolved.root'"))?;
let root = resolved_crates
.nodes
.into_iter()
.find(|node| node.id == root_id)
.ok_or_else(|| anyhow!("cargo metadata missing root node in '.resolved.nodes'"))?;
let results = root.dependencies.into_iter().filter_map(|dep| {
let id = dep.to_string();
if <matches> {
Some(id)
} else {
None
}
});
let mut deps_writer = BufWriter::new(Vec::new());
serde_json::Serializer::new(&mut deps_writer)
.collect_seq(results)?;
println!(
"cargo:rustc-env=QCS_DEPENDENCIES={}",
String::from_utf8(deps_writer.into_inner()?)?
);
The Vergen API could take, as an argument, a filter predicate to determine which dependencies to include.
This is great! Thanks!
One minor documentation note: it wasn't immediately clear to me that cargo_dependencies_name_filter
doesn't do anything unless you also call cargo_dependencies()
, so that may be worth mentioning in the doc comment.
First, thank you for building this; it was very easy to get working and has lots of nice features.
It would be nice to have a way to emit the versions of specific Cargo dependencies; I don't know if that's possible without actually parsing the Cargo lockfile, though.