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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use std::{borrow::Cow, sync::Arc};
use uv_normalize::{DEV_DEPENDENCIES, DefaultGroups, GroupName};
/// Manager of all dependency-group decisions and settings history.
///
/// This is an Arc mostly just to avoid size bloat on things that contain these.
#[derive(Debug, Default, Clone)]
pub struct DependencyGroups(Arc<DependencyGroupsInner>);
/// Manager of all dependency-group decisions and settings history.
#[derive(Debug, Default, Clone)]
pub struct DependencyGroupsInner {
/// Groups to include.
include: IncludeGroups,
/// Groups to exclude (always wins over include).
exclude: Vec<GroupName>,
/// Whether an `--only` flag was passed.
///
/// If true, users of this API should refrain from looking at packages
/// that *aren't* specified by the dependency-groups. This is exposed
/// via [`DependencyGroupsInner::prod`][].
only_groups: bool,
/// The "raw" flags/settings we were passed for diagnostics.
history: DependencyGroupsHistory,
}
impl DependencyGroups {
/// Create from history.
///
/// This is the "real" constructor, it's basically taking raw CLI flags but in
/// a way that's a bit nicer for other constructors to use.
fn from_history(history: DependencyGroupsHistory) -> Self {
let DependencyGroupsHistory {
dev_mode,
mut group,
mut only_group,
mut no_group,
all_groups,
no_default_groups,
mut defaults,
} = history.clone();
// First desugar --dev flags
match dev_mode {
Some(DevMode::Include) => group.push(DEV_DEPENDENCIES.clone()),
Some(DevMode::Only) => only_group.push(DEV_DEPENDENCIES.clone()),
Some(DevMode::Exclude) => no_group.push(DEV_DEPENDENCIES.clone()),
None => {}
}
// `group` and `only_group` actually have the same meanings: packages to include.
// But if `only_group` is non-empty then *other* packages should be excluded.
// So we just record whether it was and then treat the two lists as equivalent.
let only_groups = !only_group.is_empty();
// --only flags imply --no-default-groups
let default_groups = !no_default_groups && !only_groups;
let include = if all_groups {
// If this is set we can ignore group/only_group/defaults as irrelevant
// (`--all-groups --only-*` is rejected at the CLI level, don't worry about it).
IncludeGroups::All
} else {
// Merge all these lists, they're equivalent now
group.append(&mut only_group);
// Resolve default groups potentially also setting All
if default_groups {
match &mut defaults {
DefaultGroups::All => IncludeGroups::All,
DefaultGroups::List(defaults) => {
group.append(defaults);
IncludeGroups::Some(group)
}
}
} else {
IncludeGroups::Some(group)
}
};
Self(Arc::new(DependencyGroupsInner {
include,
exclude: no_group,
only_groups,
history,
}))
}
/// Create from raw CLI args
#[allow(clippy::fn_params_excessive_bools)]
pub fn from_args(
dev: bool,
no_dev: bool,
only_dev: bool,
group: Vec<GroupName>,
no_group: Vec<GroupName>,
no_default_groups: bool,
only_group: Vec<GroupName>,
all_groups: bool,
) -> Self {
// Lower the --dev flags into a single dev mode.
//
// In theory only one of these 3 flags should be set (enforced by CLI),
// but we explicitly allow `--dev` and `--only-dev` to both be set,
// and "saturate" that to `--only-dev`.
let dev_mode = if only_dev {
Some(DevMode::Only)
} else if no_dev {
Some(DevMode::Exclude)
} else if dev {
Some(DevMode::Include)
} else {
None
};
Self::from_history(DependencyGroupsHistory {
dev_mode,
group,
only_group,
no_group,
all_groups,
no_default_groups,
// This is unknown at CLI-time, use `.with_defaults(...)` to apply this later!
defaults: DefaultGroups::default(),
})
}
/// Helper to make a spec from just a --dev flag
pub fn from_dev_mode(dev_mode: DevMode) -> Self {
Self::from_history(DependencyGroupsHistory {
dev_mode: Some(dev_mode),
..Default::default()
})
}
/// Helper to make a spec from just a --group
pub fn from_group(group: GroupName) -> Self {
Self::from_history(DependencyGroupsHistory {
group: vec![group],
..Default::default()
})
}
/// Apply defaults to a base [`DependencyGroups`].
///
/// This is appropriate in projects, where the `dev` group is synced by default.
pub fn with_defaults(&self, defaults: DefaultGroups) -> DependencyGroupsWithDefaults {
// Explicitly clone the inner history and set the defaults, then remake the result.
let mut history = self.0.history.clone();
history.defaults = defaults;
DependencyGroupsWithDefaults {
cur: Self::from_history(history),
prev: self.clone(),
}
}
}
impl std::ops::Deref for DependencyGroups {
type Target = DependencyGroupsInner;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DependencyGroupsInner {
/// Returns `true` if packages other than the ones referenced by these
/// dependency-groups should be considered.
///
/// That is, if I tell you to install a project and this is false,
/// you should ignore the project itself and all its dependencies,
/// and instead just install the dependency-groups.
///
/// (This is really just asking if an --only flag was passed.)
pub fn prod(&self) -> bool {
!self.only_groups
}
/// Returns `true` if the specification includes the given group.
pub fn contains(&self, group: &GroupName) -> bool {
// exclude always trumps include
!self.exclude.contains(group) && self.include.contains(group)
}
/// Iterate over all groups that we think should exist.
pub fn desugarred_names(&self) -> impl Iterator<Item = &GroupName> {
self.include.names().chain(&self.exclude)
}
/// Returns an iterator over all groups that are included in the specification,
/// assuming `all_names` is an iterator over all groups.
pub fn group_names<'a, Names>(
&'a self,
all_names: Names,
) -> impl Iterator<Item = &'a GroupName> + 'a
where
Names: Iterator<Item = &'a GroupName> + 'a,
{
all_names.filter(move |name| self.contains(name))
}
/// Iterate over all groups the user explicitly asked for on the CLI
pub fn explicit_names(&self) -> impl Iterator<Item = &GroupName> {
let DependencyGroupsHistory {
// Strictly speaking this is an explicit reference to "dev"
// but we're currently tolerant of dev not existing when referenced with
// these flags, since it kinda implicitly always exists even if
// it's not properly defined in a config file.
dev_mode: _,
group,
only_group,
no_group,
// These reference no groups explicitly
all_groups: _,
no_default_groups: _,
// This doesn't include defaults because the `dev` group may not be defined
// but gets implicitly added as a default sometimes!
defaults: _,
} = self.history();
group.iter().chain(no_group).chain(only_group)
}
/// Returns `true` if the specification will have no effect.
pub fn is_empty(&self) -> bool {
self.prod() && self.exclude.is_empty() && self.include.is_empty()
}
/// Get the raw history for diagnostics
pub fn history(&self) -> &DependencyGroupsHistory {
&self.history
}
}
/// Context about a [`DependencyGroups`][] that we've preserved for diagnostics
#[derive(Debug, Default, Clone)]
pub struct DependencyGroupsHistory {
pub dev_mode: Option<DevMode>,
pub group: Vec<GroupName>,
pub only_group: Vec<GroupName>,
pub no_group: Vec<GroupName>,
pub all_groups: bool,
pub no_default_groups: bool,
pub defaults: DefaultGroups,
}
impl DependencyGroupsHistory {
/// Returns all the CLI flags that this represents.
///
/// If a flag was provided multiple times (e.g. `--group A --group B`) this will
/// elide the arguments and just show the flag once (e.g. just yield "--group").
///
/// Conceptually this being an empty list should be equivalent to
/// [`DependencyGroups::is_empty`][] when there aren't any defaults set.
/// When there are defaults the two will disagree, and rightfully so!
pub fn as_flags_pretty(&self) -> Vec<Cow<'_, str>> {
let Self {
dev_mode,
group,
only_group,
no_group,
all_groups,
no_default_groups,
// defaults aren't CLI flags!
defaults: _,
} = self;
let mut flags = vec![];
if *all_groups {
flags.push(Cow::Borrowed("--all-groups"));
}
if *no_default_groups {
flags.push(Cow::Borrowed("--no-default-groups"));
}
if let Some(dev_mode) = dev_mode {
flags.push(Cow::Borrowed(dev_mode.as_flag()));
}
match &**group {
[] => {}
[group] => flags.push(Cow::Owned(format!("--group {group}"))),
[..] => flags.push(Cow::Borrowed("--group")),
}
match &**only_group {
[] => {}
[group] => flags.push(Cow::Owned(format!("--only-group {group}"))),
[..] => flags.push(Cow::Borrowed("--only-group")),
}
match &**no_group {
[] => {}
[group] => flags.push(Cow::Owned(format!("--no-group {group}"))),
[..] => flags.push(Cow::Borrowed("--no-group")),
}
flags
}
}
/// A trivial newtype wrapped around [`DependencyGroups`][] that signifies "defaults applied"
///
/// It includes a copy of the previous semantics to provide info on if
/// the group being a default actually affected it being enabled, because it's obviously "correct".
/// (These are Arcs so it's ~free to hold onto the previous semantics)
#[derive(Debug, Clone)]
pub struct DependencyGroupsWithDefaults {
/// The active semantics
cur: DependencyGroups,
/// The semantics before defaults were applied
prev: DependencyGroups,
}
impl DependencyGroupsWithDefaults {
/// Do not enable any groups
///
/// Many places in the code need to know what dependency-groups are active,
/// but various commands or subsystems never enable any dependency-groups,
/// in which case they want this.
pub fn none() -> Self {
DependencyGroups::default().with_defaults(DefaultGroups::default())
}
/// Returns `true` if the specification was enabled, and *only* because it was a default
pub fn contains_because_default(&self, group: &GroupName) -> bool {
self.cur.contains(group) && !self.prev.contains(group)
}
}
impl std::ops::Deref for DependencyGroupsWithDefaults {
type Target = DependencyGroups;
fn deref(&self) -> &Self::Target {
&self.cur
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum DevMode {
/// Include development dependencies.
#[default]
Include,
/// Exclude development dependencies.
Exclude,
/// Only include development dependencies, excluding all other dependencies.
Only,
}
impl DevMode {
/// Returns the flag that was used to request development dependencies.
pub fn as_flag(&self) -> &'static str {
match self {
Self::Exclude => "--no-dev",
Self::Include => "--dev",
Self::Only => "--only-dev",
}
}
}
#[derive(Debug, Clone)]
pub enum IncludeGroups {
/// Include dependencies from the specified groups.
Some(Vec<GroupName>),
/// A marker indicates including dependencies from all groups.
All,
}
impl IncludeGroups {
/// Returns `true` if the specification includes the given group.
pub fn contains(&self, group: &GroupName) -> bool {
match self {
Self::Some(groups) => groups.contains(group),
Self::All => true,
}
}
/// Returns `true` if the specification will have no effect.
pub fn is_empty(&self) -> bool {
match self {
Self::Some(groups) => groups.is_empty(),
// Although technically this is a noop if they have no groups,
// conceptually they're *trying* to have an effect, so treat it as one.
Self::All => false,
}
}
/// Iterate over all groups referenced in the [`IncludeGroups`].
pub fn names(&self) -> std::slice::Iter<'_, GroupName> {
match self {
Self::Some(groups) => groups.iter(),
Self::All => [].iter(),
}
}
}
impl Default for IncludeGroups {
fn default() -> Self {
Self::Some(Vec::new())
}
}