forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmetadata.rs
More file actions
155 lines (133 loc) 路 4.57 KB
/
Copy pathmetadata.rs
File metadata and controls
155 lines (133 loc) 路 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use anyhow::{Context, Result};
use cargo_metadata::{
Metadata as CargoMetadata, Package as MetadataPackage, PackageId as MetadataId,
};
use std::{
collections::{btree_map, BTreeMap, BTreeSet},
path::{Path, PathBuf},
str::FromStr,
};
/// The minimal amount of package information we care about
///
/// The package's `name` is used to `cargo clean -p` specific crates while the `src_paths` are
/// are used to trigger recompiles of packages within the workspace
#[derive(Debug)]
pub struct Package {
name: String,
src_paths: Vec<PathBuf>,
}
impl Package {
pub fn name(&self) -> &str {
&self.name
}
pub fn src_paths(&self) -> &[PathBuf] {
&self.src_paths
}
}
impl From<&MetadataPackage> for Package {
fn from(package: &MetadataPackage) -> Self {
let name = package.name.to_string();
let src_paths = package
.targets
.iter()
.map(|target| target.src_path.clone().into_std_path_buf())
.collect();
Self { name, src_paths }
}
}
/// Contains metadata for the current project
pub struct Metadata {
/// Maps packages metadata id to the package
///
/// Currently `MetadataId` is used over `PkgId` because pkgid is not a UUID
packages: BTreeMap<MetadataId, Package>,
/// All of the crates in the current workspace
workspace_members: Vec<MetadataId>,
/// Maps each dependency to its set of dependents
reverse_deps: BTreeMap<MetadataId, BTreeSet<MetadataId>>,
/// The target directory of the project
///
/// Typically `target` at the workspace root, but can be overridden
target_directory: PathBuf,
/// Crate in the current working directory, empty if run from a
/// virtual workspace root.
current_package: Option<Package>,
}
impl Metadata {
pub fn package(&self, id: &MetadataId) -> Option<&Package> {
self.packages.get(id)
}
pub fn entries<'this>(&'this self) -> btree_map::Iter<'this, MetadataId, Package> {
self.packages.iter()
}
pub fn workspace_members(&self) -> &[MetadataId] {
&self.workspace_members
}
pub fn target_directory(&self) -> &Path {
&self.target_directory
}
pub fn current_package(&self) -> Option<&Package> {
self.current_package.as_ref()
}
/// Gets all dependents (direct and transitive) of `id`
pub fn all_dependents_of(&self, id: &MetadataId) -> BTreeSet<&MetadataId> {
let mut dependents = BTreeSet::new();
self.all_dependents_of_helper(id, &mut dependents);
dependents
}
fn all_dependents_of_helper<'this>(
&'this self,
id: &MetadataId,
dependents: &mut BTreeSet<&'this MetadataId>,
) {
if let Some(immediate_dependents) = self.reverse_deps.get(id) {
for immediate_dependent in immediate_dependents {
if dependents.insert(immediate_dependent) {
self.all_dependents_of_helper(&immediate_dependent, dependents);
}
}
}
}
}
impl FromStr for Metadata {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let cargo_metadata: CargoMetadata = serde_json::from_str(s)?;
// Extract the package in the current working directory, empty if run from a
// virtual workspace root.
let current_package: Option<Package> = cargo_metadata.root_package().map(Package::from);
let CargoMetadata {
packages: metadata_packages,
workspace_members,
resolve,
target_directory,
..
} = cargo_metadata;
let mut packages = BTreeMap::new();
for metadata_package in metadata_packages {
let package = Package::from(&metadata_package);
packages.insert(metadata_package.id, package);
}
let mut reverse_deps: BTreeMap<_, BTreeSet<_>> = BTreeMap::new();
let resolve =
resolve.context("Resolving the dependency graph failed (old version of cargo)")?;
for node in resolve.nodes {
for dep in node.deps {
let dependent = node.id.clone();
let dependency = dep.pkg;
reverse_deps
.entry(dependency)
.or_default()
.insert(dependent);
}
}
let target_directory = target_directory.into_std_path_buf();
Ok(Self {
packages,
workspace_members,
reverse_deps,
target_directory,
current_package,
})
}
}