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

Add --depth option #19

Merged
merged 4 commits into from
Dec 7, 2023
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
21 changes: 11 additions & 10 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Arg, ArgAction, Command};
use clap::{value_parser, Arg, ArgAction, Command};

pub(crate) struct Config {
pub build_deps: bool,
Expand All @@ -10,8 +10,8 @@ pub(crate) struct Config {
pub include: Vec<String>,
pub root: Vec<String>,
pub workspace_only: bool,
pub direct_dependencies_only: bool,
pub focus: Vec<String>,
pub depth: Option<u32>,

pub features: Vec<String>,
pub all_features: bool,
Expand Down Expand Up @@ -109,12 +109,6 @@ pub(crate) fn parse_options() -> Config {
.action(ArgAction::SetTrue)
.help("Exclude all packages outside of the workspace"),
)
.arg(
Arg::new("direct_dependencies_only")
.long("direct-dependencies-only")
.action(ArgAction::SetTrue)
.help("Only list direct dependencies of workspace crates"),
)
.arg(
Arg::new("focus")
.long("focus")
Expand All @@ -127,6 +121,13 @@ pub(crate) fn parse_options() -> Config {
list or as multiple arguments",
),
)
.arg(
Arg::new("depth")
.long("depth")
.value_parser(value_parser!(u32))
.action(ArgAction::Set)
.help("Limit the depth of the dependency graph")
)
// Options to pass through to `cargo metadata`
.arg(
Arg::new("features")
Expand Down Expand Up @@ -205,8 +206,8 @@ pub(crate) fn parse_options() -> Config {
let include = matches.get_many("include").map_or_else(Vec::new, collect_owned);
let root = matches.get_many("root").map_or_else(Vec::new, collect_owned);
let workspace_only = matches.get_flag("workspace_only");
let direct_dependencies_only = matches.get_flag("direct_dependencies_only");
let focus = matches.get_many("focus").map_or_else(Vec::new, collect_owned);
let depth = matches.get_one("depth").cloned();

let features = matches.get_many("features").map_or_else(Vec::new, collect_owned);
let all_features = matches.get_flag("all_features");
Expand All @@ -228,8 +229,8 @@ pub(crate) fn parse_options() -> Config {
include,
root,
workspace_only,
direct_dependencies_only,
focus,
depth,
features,
all_features,
no_default_features,
Expand Down
14 changes: 8 additions & 6 deletions src/graph/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ pub(crate) fn get_dep_graph(metadata: Metadata, config: &Config) -> anyhow::Resu
}

let node_idx = graph.add_node(Package::new(pkg, true));
deps_add_queue.push_back(pkg_id.clone());
deps_add_queue.push_back((pkg_id.clone(), 0u32));
let old_val = node_indices.insert(pkg_id.clone(), node_idx);
assert!(old_val.is_none());
}

// Add dependencies of the roots
while let Some(pkg_id) = deps_add_queue.pop_front() {
while let Some((pkg_id, depth)) = deps_add_queue.pop_front() {
let pkg = get_package(&metadata.packages, &pkg_id);

let parent_idx = *node_indices
Expand Down Expand Up @@ -88,6 +88,11 @@ pub(crate) fn get_dep_graph(metadata: Metadata, config: &Config) -> anyhow::Resu
continue;
}

// Don't add dependencies of dependencies if we're at the depth limit
if config.depth.is_some_and(|max_depth| depth >= max_depth) {
continue;
}

let dep_pkg = &get_package(&metadata.packages, &dep.pkg);
let dep_pkg = Package::new(dep_pkg, is_workspace_member);

Expand All @@ -99,10 +104,7 @@ pub(crate) fn get_dep_graph(metadata: Metadata, config: &Config) -> anyhow::Resu

let idx = graph.add_node(dep_pkg);

// Don't add any more dependencies to the queue if we only want direct dependencies
if !config.direct_dependencies_only {
deps_add_queue.push_back(dep.pkg.clone());
}
deps_add_queue.push_back((dep.pkg.clone(), depth + 1));

v.insert(idx);
idx
Expand Down