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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
use super::pool::{Pool, PoolLink, PoolPackageInput};
use indexmap::IndexSet;
use std::collections::VecDeque;
/// Builder for constructing a Pool from package metadata.
///
/// The builder accepts package inputs and recursively discovers
/// transitive dependencies. This is done by the registry layer
/// before solving.
pub struct PoolBuilder {
/// Packages to add to the pool.
inputs: Vec<PoolPackageInput>,
/// Names already added (to avoid duplicates).
added: IndexSet<String>,
/// Queue of package names that need to be explored.
pending_names: VecDeque<String>,
/// Package names that have already been explored (returned by next_pending).
explored_names: IndexSet<String>,
/// Specific platform packages to ignore (from `--ignore-platform-req=name`).
ignore_platform_reqs: IndexSet<String>,
/// When true, ignore every platform package (php, ext-*, lib-*, composer-*).
/// Mirrors `--ignore-platform-reqs` (no value).
ignore_all_platform_reqs: bool,
}
impl PoolBuilder {
pub fn new() -> Self {
PoolBuilder {
inputs: Vec::new(),
added: IndexSet::new(),
pending_names: VecDeque::new(),
explored_names: IndexSet::new(),
ignore_platform_reqs: IndexSet::new(),
ignore_all_platform_reqs: false,
}
}
/// Set platform requirements to ignore during exploration.
pub fn set_ignore_platform_reqs(&mut self, names: IndexSet<String>) {
self.ignore_platform_reqs = names;
}
/// When set, every platform package is skipped during exploration.
pub fn set_ignore_all_platform_reqs(&mut self, ignore_all: bool) {
self.ignore_all_platform_reqs = ignore_all;
}
fn is_ignored_platform_dep(&self, name: &str) -> bool {
if self
.ignore_platform_reqs
.iter()
.any(|p| crate::matches_wildcard(name, p))
{
return true;
}
self.ignore_all_platform_reqs && crate::platform::is_platform_package(name)
}
/// Add a package version to the builder. Returns true if it's new.
pub fn add_package(&mut self, input: PoolPackageInput) -> bool {
let key = format!("{}@{}", input.name, input.version);
if self.added.contains(&key) {
return false;
}
self.added.insert(key);
// Queue dependency names for exploration
for link in &input.requires {
if !self.is_ignored_platform_dep(&link.target) {
self.pending_names.push_back(link.target.clone());
}
}
self.inputs.push(input);
true
}
/// Get the next package name that needs to be explored.
/// The caller should fetch available versions for this package
/// and add them via `add_package`.
pub fn next_pending(&mut self) -> Option<String> {
while let Some(name) = self.pending_names.pop_front() {
// Skip if already explored or already has versions in inputs
if self.explored_names.contains(&name) {
continue;
}
if self.inputs.iter().any(|p| p.name == name) {
continue;
}
self.explored_names.insert(name.clone());
return Some(name);
}
None
}
/// Check if there are more names to explore.
pub fn has_pending(&self) -> bool {
!self.pending_names.is_empty()
}
/// Build the final Pool.
pub fn build(self) -> Pool {
Pool::new(self.inputs, vec![])
}
/// Get the number of packages added so far.
pub fn len(&self) -> usize {
self.inputs.len()
}
/// Read-only access to package inputs collected so far. Used by the
/// registry layer to materialize root aliases (`require: "X as Y"`) once
/// every base + branch-alias entry is in place: a second pass scans for
/// matching `(name, version)` and pushes the alias entry on top.
pub fn inputs(&self) -> &[PoolPackageInput] {
&self.inputs
}
/// Whether the builder has no packages.
pub fn is_empty(&self) -> bool {
self.inputs.is_empty()
}
}
impl Default for PoolBuilder {
fn default() -> Self {
Self::new()
}
}
/// Helper to convert (name, constraint) pairs from Packagist into PoolLinks.
///
/// `source_version` is the normalized version of the package declaring these
/// links; it replaces any `"self.version"` constraint, mirroring Composer's
/// `ArrayLoader::createLink` (and `AliasPackage::replaceSelfVersionDependencies`,
/// which feeds the alias's own version in for the same purpose).
pub fn make_pool_links(
source: &str,
source_version: &str,
deps: &[(String, String)],
) -> Vec<PoolLink> {
deps.iter()
.map(|(target, constraint)| PoolLink {
target: target.clone(),
constraint: if constraint.trim() == "self.version" {
source_version.to_string()
} else {
constraint.clone()
},
source: source.to_string(),
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pool_builder_basic() {
let mut builder = PoolBuilder::new();
builder.add_package(PoolPackageInput {
name: "a/a".to_string(),
version: "1.0.0.0".to_string(),
pretty_version: "1.0.0".to_string(),
requires: vec![PoolLink {
target: "b/b".to_string(),
constraint: "^1.0".to_string(),
source: "a/a".to_string(),
}],
replaces: vec![],
provides: vec![],
conflicts: vec![],
is_fixed: false,
is_alias_of: None,
});
// Should have b/b pending
let pending = builder.next_pending();
assert_eq!(pending, Some("b/b".to_string()));
builder.add_package(PoolPackageInput {
name: "b/b".to_string(),
version: "1.0.0.0".to_string(),
pretty_version: "1.0.0".to_string(),
requires: vec![],
replaces: vec![],
provides: vec![],
conflicts: vec![],
is_fixed: false,
is_alias_of: None,
});
// No more pending
assert!(builder.next_pending().is_none());
let pool = builder.build();
assert_eq!(pool.len(), 2);
}
#[test]
fn test_deduplication() {
let mut builder = PoolBuilder::new();
let input = PoolPackageInput {
name: "a/a".to_string(),
version: "1.0.0.0".to_string(),
pretty_version: "1.0.0".to_string(),
requires: vec![],
replaces: vec![],
provides: vec![],
conflicts: vec![],
is_fixed: false,
is_alias_of: None,
};
assert!(builder.add_package(input.clone()));
assert!(!builder.add_package(input));
assert_eq!(builder.len(), 1);
}
}
|