Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include dependencies graph in the Metadata #9

Merged
merged 2 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo_metadata"
version = "0.2.1"
version = "0.2.2"
authors = ["Oliver Schneider <[email protected]>"]
repository = "https://github.com/oli-obk/cargo_metadata"
description = "structured access to the output of `cargo metadata`"
Expand Down
47 changes: 42 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,43 @@ use std::io;
pub struct Metadata {
/// A list of all crates referenced by this crate (and the crate itself)
pub packages: Vec<Package>,
resolve: Option<()>,
/// Dependencies graph
pub resolve: Option<Resolve>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bumped the version because this type changes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well. The field was private, so it's only a small breaking change since the parsed json is tested more strictly. I think it should be a patch version bump, since no real code should break (it should be valid metadata json to begin with)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, don't know what I was thinking there.

version: usize,
}

#[derive(Clone, Deserialize, Debug)]
/// A dependency graph
pub struct Resolve {
/// Nodes in a dependencies graph
pub nodes: Vec<Node>,
}

#[derive(Clone, Deserialize, Debug)]
/// A node in a dependencies graph
pub struct Node {
/// An opaque identifier for a package
pub id: String,
/// List of opaque identifiers for this node's dependencies
pub dependencies: Vec<String>,
}

#[derive(Clone, Deserialize, Debug)]
/// A crate
pub struct Package {
/// Name as given in the `Cargo.toml`
pub name: String,
/// Version given in the `Cargo.toml`
pub version: String,
id: String,
/// An opaque identifier for a package
pub id: String,
source: Option<String>,
/// List of dependencies of this particular package
pub dependencies: Vec<Dependency>,
/// Targets provided by the crate (lib, bin, example, test, ...)
pub targets: Vec<Target>,
features: HashMap<String, Vec<String>>,
/// path containing the `Cargo.toml`
/// Path containing the `Cargo.toml`
pub manifest_path: String,
}

Expand Down Expand Up @@ -102,11 +120,30 @@ impl From<serde_json::Error> for Error {
}
}

/// The main entry point to obtaining metadata
/// Obtain metadata only about the root package and don't fetch dependencies
///
/// # Parameters
///
/// - `manifest_path_arg`: Path to the manifest.
pub fn metadata(manifest_path_arg: Option<&str>) -> Result<Metadata, Error> {
metadata_deps(manifest_path_arg, false)
}

/// The main entry point to obtaining metadata
///
/// # Parameters
///
/// - `manifest_path_arg`: Path to the manifest.
/// - `deps`: Whether to include dependencies.
pub fn metadata_deps(manifest_path_arg: Option<&str>, deps: bool) -> Result<Metadata, Error> {
let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));
let mut cmd = Command::new(cargo);
cmd.arg("metadata").arg("--no-deps");
cmd.arg("metadata");

if !deps {
cmd.arg("--no-deps");
}

cmd.arg("--format-version").arg("1");
if let Some(mani) = manifest_path_arg {
cmd.arg(mani);
Expand Down
20 changes: 19 additions & 1 deletion tests/selftest.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
extern crate cargo_metadata;

#[test]
fn foo() {
fn metadata() {
let metadata = cargo_metadata::metadata(None).unwrap();

assert_eq!(metadata.packages[0].name, "cargo_metadata");
assert_eq!(metadata.packages[0].targets.len(), 2);

Expand All @@ -14,3 +15,20 @@ fn foo() {
assert_eq!(metadata.packages[0].targets[1].kind[0], "test");
assert_eq!(metadata.packages[0].targets[1].crate_types[0], "bin");
}

#[test]
fn metadata_deps() {
let metadata = cargo_metadata::metadata_deps(None, true).unwrap();
let this = metadata.packages.iter().find(|package| package.name == "cargo_metadata").expect("Did not find ourselves");

assert_eq!(this.name, "cargo_metadata");
assert_eq!(this.targets.len(), 2);

assert_eq!(this.targets[0].name, "cargo_metadata");
assert_eq!(this.targets[0].kind[0], "lib");
assert_eq!(this.targets[0].crate_types[0], "lib");

assert_eq!(this.targets[1].name, "selftest");
assert_eq!(this.targets[1].kind[0], "test");
assert_eq!(this.targets[1].crate_types[0], "bin");
}