Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Add JSON output capabilities #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ cargo = "0.20"
env_logger = "0.4"
petgraph = "0.4"
rustc-serialize = "0.3"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
73 changes: 72 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ extern crate cargo;
extern crate env_logger;
extern crate petgraph;
extern crate rustc_serialize;
extern crate serde;
#[macro_use] extern crate serde_derive;
extern crate serde_json;

use cargo::{Config, CliResult};
use cargo::core::{PackageId, Package, Resolve, Workspace};
Expand All @@ -16,7 +19,7 @@ use cargo::util::{self, important_paths, CargoResult, Cfg, CargoError};
use petgraph::EdgeDirection;
use petgraph::graph::NodeIndex;
use petgraph::visit::EdgeRef;
use std::collections::{HashSet, HashMap};
use std::collections::{HashSet, HashMap, BTreeMap};
use std::collections::hash_map::Entry;
use std::env;
use std::str::{self, FromStr};
Expand Down Expand Up @@ -50,6 +53,7 @@ Options:
--charset CHARSET Set the character set to use in output. Valid
values: utf8, ascii [default: utf8]
-f, --format FORMAT Format string for printing dependencies
-j, --json Print a JSON representation of the tree
Copy link
Owner

Choose a reason for hiding this comment

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

I think it'd make sense to match the --output-format flag that Cargo uses, and make the JSON representation compatible with that.

--manifest-path PATH Path to the manifest to analyze
-v, --verbose Use verbose output
-q, --quiet No output printed to stdout other than the tree
Expand All @@ -71,6 +75,7 @@ struct Flags {
flag_no_indent: bool,
flag_all: bool,
flag_charset: Charset,
flag_json: bool,
flag_format: Option<String>,
flag_manifest_path: Option<String>,
flag_verbose: u32,
Expand Down Expand Up @@ -214,6 +219,14 @@ fn real_main(flags: Flags, config: &Config) -> CliResult {
flags.flag_all);
println!("");
}
} else if flags.flag_json {
println!("{}", serde_json::to_string_pretty(
&graph_to_tree(package.package_id(),
Copy link
Owner

Choose a reason for hiding this comment

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

It seems a bit strange that this mode would ignore a bunch of the flags the other modes respect.

&packages,
&resolve,
kind,
&graph,
direction)?).expect("serialization failed"))
} else {
print_tree(root,
kind,
Expand Down Expand Up @@ -317,6 +330,13 @@ struct Graph<'a> {
nodes: HashMap<&'a PackageId, NodeIndex>,
}

#[derive(Debug, Serialize, Deserialize)]
Copy link
Owner

Choose a reason for hiding this comment

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

No need to implement Deserialize I don't think.

struct Tree {
version: String,
features: Vec<String>,
dependencies: BTreeMap<String, Box<Tree>>,
}

fn build_graph<'a>(resolve: &'a Resolve,
packages: &'a PackageSet,
root: &'a PackageId,
Expand Down Expand Up @@ -368,6 +388,57 @@ fn build_graph<'a>(resolve: &'a Resolve,
Ok(graph)
}

fn graph_to_tree<'a>(package_id: &'a PackageId,
packages: &'a PackageSet,
resolve: &'a Resolve,
kind: Kind,
graph: &Graph<'a>,
direction: EdgeDirection) -> CargoResult<Tree> {
let mut visited_deps = HashSet::new();
graph_to_tree_(package_id,
packages,
resolve,
kind,
graph,
direction,
&mut visited_deps)
}

fn graph_to_tree_<'a>(package_id: &'a PackageId,
packages: &'a PackageSet,
resolve: &'a Resolve,
kind: Kind,
graph: &Graph<'a>,
direction: EdgeDirection,
visited_deps: &mut HashSet<&'a PackageId>)
-> CargoResult<Tree> {
let deps = graph.graph
.edges_directed(graph.nodes[package_id], direction)
.filter(|edge| edge.weight() == &kind)
.map(|edge| {
match direction {
EdgeDirection::Incoming => &graph.graph[edge.source()],
EdgeDirection::Outgoing => &graph.graph[edge.target()],
}
});

let mut deps_ = BTreeMap::new();
for dep in deps {
let name = packages.get(dep.id)?.name().to_owned();
deps_.insert(name, Box::new(graph_to_tree_(
dep.id, packages, resolve, kind, graph, direction, visited_deps)?));
}

let package = packages.get(package_id)?;

Ok(Tree {
version: package.version().to_string(),
features: resolve.features_sorted(package_id)
.into_iter().map(|x| x.to_owned()).collect(),
dependencies: deps_,
})
}

fn print_tree<'a>(package: &'a PackageId,
kind: Kind,
graph: &Graph<'a>,
Expand Down