2019-01-10 21:40:35 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-01-10 19:21:14 +00:00
|
|
|
|
2019-01-14 13:15:25 +00:00
|
|
|
use cargo_metadata::{MetadataCommand, CargoOpt};
|
2019-01-10 19:21:14 +00:00
|
|
|
use ra_arena::{Arena, RawId, impl_arena_id};
|
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
use failure::format_err;
|
|
|
|
|
|
|
|
use crate::Result;
|
|
|
|
|
2019-02-05 22:10:49 +00:00
|
|
|
/// `CargoWorkspace` represents the logical structure of, well, a Cargo
|
2019-01-10 19:21:14 +00:00
|
|
|
/// workspace. It pretty closely mirrors `cargo metadata` output.
|
|
|
|
///
|
2019-02-05 22:10:49 +00:00
|
|
|
/// Note that internally, rust analyzer uses a different structure:
|
2019-01-10 19:21:14 +00:00
|
|
|
/// `CrateGraph`. `CrateGraph` is lower-level: it knows only about the crates,
|
2019-02-11 16:18:27 +00:00
|
|
|
/// while this knows about `Packages` & `Targets`: purely cargo-related
|
2019-01-10 19:21:14 +00:00
|
|
|
/// concepts.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct CargoWorkspace {
|
|
|
|
packages: Arena<Package, PackageData>,
|
|
|
|
targets: Arena<Target, TargetData>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub struct Package(RawId);
|
|
|
|
impl_arena_id!(Package);
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub struct Target(RawId);
|
|
|
|
impl_arena_id!(Target);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct PackageData {
|
2019-02-09 09:51:06 +00:00
|
|
|
name: String,
|
2019-01-10 19:21:14 +00:00
|
|
|
manifest: PathBuf,
|
|
|
|
targets: Vec<Target>,
|
|
|
|
is_member: bool,
|
|
|
|
dependencies: Vec<PackageDependency>,
|
2019-02-10 21:34:29 +00:00
|
|
|
edition: String,
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct PackageDependency {
|
|
|
|
pub pkg: Package,
|
2019-02-09 09:51:06 +00:00
|
|
|
pub name: String,
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct TargetData {
|
|
|
|
pkg: Package,
|
2019-02-09 09:51:06 +00:00
|
|
|
name: String,
|
2019-01-10 19:21:14 +00:00
|
|
|
root: PathBuf,
|
|
|
|
kind: TargetKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum TargetKind {
|
|
|
|
Bin,
|
|
|
|
Lib,
|
|
|
|
Example,
|
|
|
|
Test,
|
|
|
|
Bench,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TargetKind {
|
|
|
|
fn new(kinds: &[String]) -> TargetKind {
|
|
|
|
for kind in kinds {
|
|
|
|
return match kind.as_str() {
|
|
|
|
"bin" => TargetKind::Bin,
|
|
|
|
"test" => TargetKind::Test,
|
|
|
|
"bench" => TargetKind::Bench,
|
|
|
|
"example" => TargetKind::Example,
|
|
|
|
_ if kind.contains("lib") => TargetKind::Lib,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
TargetKind::Other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Package {
|
|
|
|
pub fn name(self, ws: &CargoWorkspace) -> &str {
|
|
|
|
ws.packages[self].name.as_str()
|
|
|
|
}
|
|
|
|
pub fn root(self, ws: &CargoWorkspace) -> &Path {
|
|
|
|
ws.packages[self].manifest.parent().unwrap()
|
|
|
|
}
|
2019-02-10 21:34:29 +00:00
|
|
|
pub fn edition(self, ws: &CargoWorkspace) -> &str {
|
|
|
|
&ws.packages[self].edition
|
|
|
|
}
|
2019-01-10 19:21:14 +00:00
|
|
|
pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a {
|
|
|
|
ws.packages[self].targets.iter().cloned()
|
|
|
|
}
|
|
|
|
#[allow(unused)]
|
|
|
|
pub fn is_member(self, ws: &CargoWorkspace) -> bool {
|
|
|
|
ws.packages[self].is_member
|
|
|
|
}
|
|
|
|
pub fn dependencies<'a>(
|
|
|
|
self,
|
|
|
|
ws: &'a CargoWorkspace,
|
|
|
|
) -> impl Iterator<Item = &'a PackageDependency> + 'a {
|
|
|
|
ws.packages[self].dependencies.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Target {
|
|
|
|
pub fn package(self, ws: &CargoWorkspace) -> Package {
|
|
|
|
ws.targets[self].pkg
|
|
|
|
}
|
|
|
|
pub fn name(self, ws: &CargoWorkspace) -> &str {
|
|
|
|
ws.targets[self].name.as_str()
|
|
|
|
}
|
|
|
|
pub fn root(self, ws: &CargoWorkspace) -> &Path {
|
|
|
|
ws.targets[self].root.as_path()
|
|
|
|
}
|
|
|
|
pub fn kind(self, ws: &CargoWorkspace) -> TargetKind {
|
|
|
|
ws.targets[self].kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CargoWorkspace {
|
|
|
|
pub fn from_cargo_metadata(cargo_toml: &Path) -> Result<CargoWorkspace> {
|
2019-01-26 20:16:15 +00:00
|
|
|
let mut meta = MetadataCommand::new();
|
2019-02-08 11:49:43 +00:00
|
|
|
meta.manifest_path(cargo_toml).features(CargoOpt::AllFeatures);
|
2019-01-26 20:16:15 +00:00
|
|
|
if let Some(parent) = cargo_toml.parent() {
|
|
|
|
meta.current_dir(parent);
|
|
|
|
}
|
2019-02-08 11:49:43 +00:00
|
|
|
let meta = meta.exec().map_err(|e| format_err!("cargo metadata failed: {}", e))?;
|
2019-01-10 19:21:14 +00:00
|
|
|
let mut pkg_by_id = FxHashMap::default();
|
|
|
|
let mut packages = Arena::default();
|
|
|
|
let mut targets = Arena::default();
|
|
|
|
|
|
|
|
let ws_members = &meta.workspace_members;
|
|
|
|
|
|
|
|
for meta_pkg in meta.packages {
|
|
|
|
let is_member = ws_members.contains(&meta_pkg.id);
|
|
|
|
let pkg = packages.alloc(PackageData {
|
|
|
|
name: meta_pkg.name.into(),
|
|
|
|
manifest: meta_pkg.manifest_path.clone(),
|
|
|
|
targets: Vec::new(),
|
|
|
|
is_member,
|
2019-02-10 21:34:29 +00:00
|
|
|
edition: meta_pkg.edition,
|
2019-01-10 19:21:14 +00:00
|
|
|
dependencies: Vec::new(),
|
|
|
|
});
|
|
|
|
let pkg_data = &mut packages[pkg];
|
|
|
|
pkg_by_id.insert(meta_pkg.id.clone(), pkg);
|
|
|
|
for meta_tgt in meta_pkg.targets {
|
|
|
|
let tgt = targets.alloc(TargetData {
|
|
|
|
pkg,
|
|
|
|
name: meta_tgt.name.into(),
|
|
|
|
root: meta_tgt.src_path.clone(),
|
|
|
|
kind: TargetKind::new(meta_tgt.kind.as_slice()),
|
|
|
|
});
|
|
|
|
pkg_data.targets.push(tgt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let resolve = meta.resolve.expect("metadata executed with deps");
|
|
|
|
for node in resolve.nodes {
|
|
|
|
let source = pkg_by_id[&node.id];
|
|
|
|
for dep_node in node.deps {
|
2019-02-08 11:49:43 +00:00
|
|
|
let dep =
|
|
|
|
PackageDependency { name: dep_node.name.into(), pkg: pkg_by_id[&dep_node.pkg] };
|
2019-01-10 19:21:14 +00:00
|
|
|
packages[source].dependencies.push(dep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(CargoWorkspace { packages, targets })
|
|
|
|
}
|
2019-02-05 22:10:49 +00:00
|
|
|
|
2019-01-10 19:21:14 +00:00
|
|
|
pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + 'a {
|
|
|
|
self.packages.iter().map(|(id, _pkg)| id)
|
|
|
|
}
|
2019-02-05 22:10:49 +00:00
|
|
|
|
2019-01-10 19:21:14 +00:00
|
|
|
pub fn target_by_root(&self, root: &Path) -> Option<Target> {
|
2019-02-08 11:49:43 +00:00
|
|
|
self.packages().filter_map(|pkg| pkg.targets(self).find(|it| it.root(self) == root)).next()
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
}
|