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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::pkg;
use anyhow::{anyhow, Result};
use forc_util::{println_green, println_red};
use petgraph::{visit::EdgeRef, Direction};
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeSet, HashMap},
fs,
path::Path,
str::FromStr,
};
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct Lock {
pub(crate) package: BTreeSet<PkgLock>,
}
pub struct Diff<'a> {
pub removed: BTreeSet<&'a PkgLock>,
pub added: BTreeSet<&'a PkgLock>,
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
pub struct PkgLock {
pub(crate) name: String,
version: Option<semver::Version>,
source: Option<String>,
dependencies: Vec<String>,
}
pub fn source_to_string(source: &pkg::SourcePinned) -> Option<String> {
match source {
pkg::SourcePinned::Path => None,
pkg::SourcePinned::Git(git) => Some(git.to_string()),
pkg::SourcePinned::Registry(_reg) => unimplemented!("pkg registries not yet implemented"),
}
}
pub fn source_from_str(s: &str) -> Result<pkg::SourcePinned> {
if let Ok(src) = pkg::SourceGitPinned::from_str(s) {
return Ok(pkg::SourcePinned::Git(src));
}
Err(anyhow!(
"Unable to parse valid pinned source from given string {}",
s
))
}
impl PkgLock {
pub fn from_node(graph: &pkg::Graph, node: pkg::NodeIx) -> Self {
let pinned = &graph[node];
let name = pinned.name.clone();
let version = match &pinned.source {
pkg::SourcePinned::Registry(reg) => Some(reg.source.version.clone()),
_ => None,
};
let source = source_to_string(&pinned.source);
let mut dependencies: Vec<String> = graph
.edges_directed(node, Direction::Outgoing)
.map(|edge| {
let dep_node = edge.target();
let dep = &graph[dep_node];
let source_string = source_to_string(&dep.source);
pkg_unique_string(&dep.name, source_string.as_deref())
})
.collect();
dependencies.sort();
Self {
name,
version,
source,
dependencies,
}
}
pub fn unique_string(&self) -> String {
pkg_unique_string(&self.name, self.source.as_deref())
}
}
impl Lock {
pub fn from_path(path: &Path) -> Result<Self> {
let string = fs::read_to_string(&path)
.map_err(|e| anyhow!("failed to read {}: {}", path.display(), e))?;
toml::de::from_str(&string).map_err(|e| anyhow!("failed to parse lock file: {}", e))
}
pub fn from_graph(graph: &pkg::Graph) -> Self {
let package: BTreeSet<_> = graph
.node_indices()
.map(|node| PkgLock::from_node(graph, node))
.collect();
Self { package }
}
pub fn to_graph(&self) -> Result<pkg::Graph> {
let mut graph = pkg::Graph::new();
let mut pkg_to_node: HashMap<String, pkg::NodeIx> = HashMap::new();
for pkg in &self.package {
let key = pkg.unique_string();
let name = pkg.name.clone();
let pkg_source_string = pkg.source.clone();
let source = match &pkg_source_string {
None => pkg::SourcePinned::Path,
Some(s) => source_from_str(s).map_err(|e| {
anyhow!("invalid 'source' entry for package {} lock: {}", name, e)
})?,
};
let pkg = pkg::Pinned { name, source };
let node = graph.add_node(pkg);
pkg_to_node.insert(key, node);
}
for pkg in &self.package {
let key = pkg.unique_string();
let node = pkg_to_node[&key];
for dep_key in &pkg.dependencies {
let dep_node = pkg_to_node
.get(&dep_key[..])
.cloned()
.ok_or_else(|| anyhow!("found dep {} without node entry in graph", dep_key))?;
graph.add_edge(node, dep_node, ());
}
}
Ok(graph)
}
pub fn diff<'a>(&'a self, old: &'a Self) -> Diff<'a> {
let added = self.package.difference(&old.package).collect();
let removed = old.package.difference(&self.package).collect();
Diff { added, removed }
}
}
fn pkg_unique_string(name: &str, source: Option<&str>) -> String {
match source {
None => name.to_string(),
Some(s) => format!("{} {}", name, s),
}
}
pub fn print_diff(proj_name: &str, diff: &Diff) {
print_removed_pkgs(proj_name, diff.removed.iter().cloned());
print_added_pkgs(proj_name, diff.added.iter().cloned());
}
pub fn print_removed_pkgs<'a, I>(proj_name: &str, removed: I)
where
I: IntoIterator<Item = &'a PkgLock>,
{
for pkg in removed {
if pkg.name != proj_name {
let _ = println_red(&format!(" Removing {}", pkg.unique_string()));
}
}
}
pub fn print_added_pkgs<'a, I>(proj_name: &str, removed: I)
where
I: IntoIterator<Item = &'a PkgLock>,
{
for pkg in removed {
if pkg.name != proj_name {
let _ = println_green(&format!(" Adding {}", pkg.unique_string()));
}
}
}