|
| 1 | +//! Gets metadata about a workspace from Cargo |
| 2 | +
|
| 3 | +/// Describes how this module can fail |
| 4 | +#[derive(Debug, thiserror::Error)] |
| 5 | +pub enum Error { |
| 6 | + #[error("Failed to run cargo metadata: {0:?}")] |
| 7 | + Launching(#[from] std::io::Error), |
| 8 | + #[error("Failed get output from cargo metadata: {0:?}")] |
| 9 | + GettingMetadata(String), |
| 10 | + #[error("Failed parse JSON output from cargo metadata: {0:?}")] |
| 11 | + ParsingJson(#[from] serde_json::Error), |
| 12 | + #[error("Failed find expected JSON element {0} in output from cargo metadata")] |
| 13 | + MissingJsonElement(&'static str), |
| 14 | + #[error("Failed find expected JSON element {0} in output from cargo metadata for package {1}")] |
| 15 | + MissingJsonElementForPackage(String, String), |
| 16 | +} |
| 17 | + |
| 18 | +/// Describes one of our dependencies |
| 19 | +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 20 | +pub struct Dependency { |
| 21 | + /// The name of the package |
| 22 | + pub name: String, |
| 23 | + /// The version number |
| 24 | + pub version: String, |
| 25 | + /// The license it is under |
| 26 | + pub license: String, |
| 27 | + /// The list of authors from the package metadata |
| 28 | + pub authors: Vec<String>, |
| 29 | +} |
| 30 | + |
| 31 | +/// Use `cargo` to get a list of dependencies and their license data |
| 32 | +/// |
| 33 | +/// Any dependency with a path beginning with `root_path` is ignored, as we |
| 34 | +/// assume `reuse` has covered it already. |
| 35 | +pub fn get( |
| 36 | + cargo: &std::path::Path, |
| 37 | + manifest_path: &std::path::Path, |
| 38 | + root_path: &std::path::Path, |
| 39 | +) -> Result<Vec<Dependency>, Error> { |
| 40 | + if manifest_path.file_name() != Some(std::ffi::OsStr::new("Cargo.toml")) { |
| 41 | + panic!("cargo_manifest::get requires a path to a Cargo.toml file"); |
| 42 | + } |
| 43 | + let metadata_output = std::process::Command::new(cargo) |
| 44 | + .arg("metadata") |
| 45 | + .arg("--format-version=1") |
| 46 | + .arg("--all-features") |
| 47 | + .arg("--manifest-path") |
| 48 | + .arg(manifest_path) |
| 49 | + .env("RUSTC_BOOTSTRAP", "1") |
| 50 | + .output() |
| 51 | + .map_err(|e| Error::Launching(e))?; |
| 52 | + if !metadata_output.status.success() { |
| 53 | + return Err(Error::GettingMetadata( |
| 54 | + String::from_utf8(metadata_output.stderr).expect("UTF-8 output from cargo"), |
| 55 | + )); |
| 56 | + } |
| 57 | + let metadata_json: serde_json::Value = serde_json::from_slice(&metadata_output.stdout)?; |
| 58 | + let packages = metadata_json["packages"] |
| 59 | + .as_array() |
| 60 | + .ok_or_else(|| Error::MissingJsonElement("packages array"))?; |
| 61 | + let mut v = Vec::new(); |
| 62 | + for package in packages { |
| 63 | + let package = |
| 64 | + package.as_object().ok_or_else(|| Error::MissingJsonElement("package object"))?; |
| 65 | + // println!("Package: {}", serde_json::to_string_pretty(package).expect("JSON encoding")); |
| 66 | + let manifest_path = package |
| 67 | + .get("manifest_path") |
| 68 | + .and_then(|v| v.as_str()) |
| 69 | + .map(std::path::Path::new) |
| 70 | + .ok_or_else(|| Error::MissingJsonElement("package.manifest_path"))?; |
| 71 | + if manifest_path.starts_with(&root_path) { |
| 72 | + // it's an in-tree dependency and reuse covers it |
| 73 | + continue; |
| 74 | + } |
| 75 | + // otherwise it's an out-of-tree dependency |
| 76 | + let get_string = |field_name: &str, package_name: &str| { |
| 77 | + package.get(field_name).and_then(|v| v.as_str()).ok_or_else(|| { |
| 78 | + Error::MissingJsonElementForPackage( |
| 79 | + format!("package.{field_name}"), |
| 80 | + package_name.to_owned(), |
| 81 | + ) |
| 82 | + }) |
| 83 | + }; |
| 84 | + |
| 85 | + let name = get_string("name", "unknown")?; |
| 86 | + let license = get_string("license", name)?; |
| 87 | + let version = get_string("version", name)?; |
| 88 | + let authors_list = package |
| 89 | + .get("authors") |
| 90 | + .and_then(|v| v.as_array()) |
| 91 | + .ok_or_else(|| Error::MissingJsonElement("package.authors"))?; |
| 92 | + let authors: Vec<String> = |
| 93 | + authors_list.iter().filter_map(|v| v.as_str()).map(|s| s.to_owned()).collect(); |
| 94 | + |
| 95 | + v.push(Dependency { |
| 96 | + name: name.to_owned(), |
| 97 | + version: version.to_owned(), |
| 98 | + license: license.to_owned(), |
| 99 | + authors, |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + Ok(v) |
| 104 | +} |
0 commit comments