Skip to content

Commit

Permalink
Disambiguate multiple crates with the same name
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Jul 11, 2020
1 parent a27e686 commit 70eb0d3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/dep_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct DepInfo {
pub is_optional_direct: bool,

/// whether this edge has been updated by update_dep_info after being inserted into the graph
// TODO: Store separately from DepInfo, make dedicated enum
pub visited: bool,
}

Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod package;

// Contains the `DepGraph` type and most of the graph building / analysis logic
mod graph;
// Contains some auxiliary logic (currently just checking for packages of the same name)
mod util;

// Command-line parsing
mod cli;
Expand All @@ -19,6 +21,7 @@ use self::{
cli::parse_options,
graph::{dedup_transitive_deps, get_dep_graph, update_dep_info},
output::dot,
util::set_name_stats,
};

fn main() -> anyhow::Result<()> {
Expand Down Expand Up @@ -57,6 +60,7 @@ fn main() -> anyhow::Result<()> {
if config.dedup_transitive_deps {
dedup_transitive_deps(&mut graph);
}
set_name_stats(&mut graph);

println!("{:?}", dot(&graph));

Expand Down
17 changes: 14 additions & 3 deletions src/package.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::fmt::{self, Debug, Formatter};
use std::{
cell::Cell,
fmt::{self, Debug, Formatter},
rc::Rc,
};

use cargo_metadata::Source;
use semver::Version;
Expand All @@ -11,6 +15,8 @@ pub struct Package {
pub version: Version,
pub source: Option<Source>,
pub dep_info: Option<DepInfo>,

pub name_uses: Option<Rc<Cell<u16>>>,
}

impl Package {
Expand All @@ -20,6 +26,7 @@ impl Package {
version: pkg.version.clone(),
source: pkg.source.clone(),
dep_info: if is_ws_member { None } else { Some(DepInfo::default()) },
name_uses: None,
}
}

Expand All @@ -29,8 +36,12 @@ impl Package {
}

impl Debug for Package {
// TODO: Allow writing version and such
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name)
write!(f, "{}", self.name)?;
if self.name_uses.as_ref().unwrap().get() > 1 {
write!(f, " {}", self.version)?;
}

Ok(())
}
}
13 changes: 13 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::{cell::Cell, collections::HashMap, rc::Rc};

use crate::graph::DepGraph;

pub fn set_name_stats(graph: &mut DepGraph) {
let mut name_uses_map = HashMap::<String, Rc<Cell<u16>>>::new();
for pkg in graph.node_weights_mut() {
let name_uses = name_uses_map.entry(pkg.name.clone()).or_default().clone();
name_uses.set(name_uses.get() + 1);

pkg.name_uses = Some(name_uses);
}
}

0 comments on commit 70eb0d3

Please sign in to comment.