This repository has been archived by the owner on Jun 5, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 39
Add JSON output capabilities #21
Open
Twey
wants to merge
1
commit into
sfackler:master
Choose a base branch
from
hadeaninc:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
@@ -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}; | ||
|
@@ -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 | ||
--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 | ||
|
@@ -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, | ||
|
@@ -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(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -317,6 +330,13 @@ struct Graph<'a> { | |
nodes: HashMap<&'a PackageId, NodeIndex>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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>, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.