-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathcli_config.rs
467 lines (417 loc) · 14.5 KB
/
cli_config.rs
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use crate::cli::has_specs::HasSpecs;
use crate::environment::LockFileUsage;
use crate::lock_file::UpdateMode;
use crate::workspace::DiscoveryStart;
use crate::DependencyType;
use crate::Workspace;
use clap::Parser;
use indexmap::IndexMap;
use indexmap::IndexSet;
use itertools::Itertools;
use miette::IntoDiagnostic;
use pep508_rs::Requirement;
use pixi_config::{Config, ConfigCli};
use pixi_consts::consts;
use pixi_manifest::pypi::PyPiPackageName;
use pixi_manifest::FeaturesExt;
use pixi_manifest::{FeatureName, SpecType};
use pixi_spec::GitReference;
use rattler_conda_types::ChannelConfig;
use rattler_conda_types::{Channel, NamedChannelOrUrl, Platform};
use std::collections::HashMap;
use std::path::PathBuf;
use url::Url;
use pixi_git::GIT_URL_QUERY_REV_TYPE;
/// Workspace configuration
#[derive(Parser, Debug, Default, Clone)]
pub struct WorkspaceConfig {
/// The path to `pixi.toml`, `pyproject.toml`, or the workspace directory
#[arg(long, global = true, help_heading = consts::CLAP_GLOBAL_OPTIONS)]
pub manifest_path: Option<PathBuf>,
}
impl WorkspaceConfig {
/// Returns the start location when trying to discover a workspace.
pub fn workspace_locator_start(&self) -> DiscoveryStart {
match &self.manifest_path {
Some(path) => DiscoveryStart::ExplicitManifest(path.clone()),
None => DiscoveryStart::CurrentDir,
}
}
}
/// Channel configuration
#[derive(Parser, Debug, Default)]
pub struct ChannelsConfig {
/// The channels to consider as a name or a url.
/// Multiple channels can be specified by using this field multiple times.
///
/// When specifying a channel, it is common that the selected channel also
/// depends on the `conda-forge` channel.
///
/// By default, if no channel is provided, `conda-forge` is used.
#[clap(long = "channel", short = 'c', value_name = "CHANNEL")]
channels: Vec<NamedChannelOrUrl>,
}
impl ChannelsConfig {
/// Parses the channels, getting channel config and default channels from config
pub(crate) fn resolve_from_config(&self, config: &Config) -> miette::Result<IndexSet<Channel>> {
self.resolve(config.global_channel_config(), config.default_channels())
}
/// Parses the channels, getting channel config and default channels from project
pub(crate) fn resolve_from_project(
&self,
project: Option<&Workspace>,
) -> miette::Result<IndexSet<Channel>> {
match project {
Some(project) => {
let channels = project
.default_environment()
.channels()
.into_iter()
.cloned()
.collect_vec();
self.resolve(&project.channel_config(), channels)
}
None => self.resolve_from_config(&Config::load_global()),
}
}
/// Parses the channels from specified channel config and default channels
fn resolve(
&self,
channel_config: &ChannelConfig,
default_channels: Vec<NamedChannelOrUrl>,
) -> miette::Result<IndexSet<Channel>> {
let channels = if self.channels.is_empty() {
default_channels
} else {
self.channels.clone()
};
channels
.into_iter()
.map(|c| c.into_channel(channel_config))
.try_collect()
.into_diagnostic()
}
}
/// Configuration for how to update the prefix
#[derive(Parser, Debug, Default, Clone)]
pub struct PrefixUpdateConfig {
/// Don't update lockfile, implies the no-install as well.
#[clap(long, conflicts_with = "no_install")]
pub no_lockfile_update: bool,
/// Lock file usage from the CLI
#[clap(flatten)]
pub lock_file_usage: super::LockFileUsageArgs,
/// Don't modify the environment, only modify the lock-file.
#[arg(long)]
pub no_install: bool,
#[clap(flatten)]
pub config: ConfigCli,
/// Run the complete environment validation. This will reinstall a broken environment.
#[arg(long)]
pub revalidate: bool,
}
impl PrefixUpdateConfig {
pub fn lock_file_usage(&self) -> LockFileUsage {
if self.lock_file_usage.locked {
LockFileUsage::Locked
} else if self.lock_file_usage.frozen || self.no_lockfile_update {
LockFileUsage::Frozen
} else {
LockFileUsage::Update
}
}
/// Decide whether to install or not.
pub(crate) fn no_install(&self) -> bool {
self.no_install || self.no_lockfile_update
}
/// Which `[UpdateMode]` to use
pub(crate) fn update_mode(&self) -> UpdateMode {
if self.revalidate {
UpdateMode::Revalidate
} else {
UpdateMode::QuickValidate
}
}
}
#[derive(Parser, Debug, Default, Clone)]
pub struct GitRev {
/// The git branch
#[clap(long, requires = "git", conflicts_with_all = ["tag", "rev"], help_heading = consts::CLAP_GIT_OPTIONS)]
pub branch: Option<String>,
/// The git tag
#[clap(long, requires = "git", conflicts_with_all = ["branch", "rev"], help_heading = consts::CLAP_GIT_OPTIONS)]
pub tag: Option<String>,
/// The git revision
#[clap(long, requires = "git", conflicts_with_all = ["branch", "tag"], help_heading = consts::CLAP_GIT_OPTIONS)]
pub rev: Option<String>,
}
impl GitRev {
/// Create a new `GitRev`
pub fn new() -> Self {
Default::default()
}
/// Set the branch
pub fn with_branch(mut self, branch: String) -> GitRev {
self.branch = Some(branch);
self
}
/// Set the revision
pub fn with_rev(mut self, rev: String) -> GitRev {
self.rev = Some(rev);
self
}
/// Set the tag
pub fn with_tag(mut self, tag: String) -> GitRev {
self.tag = Some(tag);
self
}
/// Get the reference as a string
pub fn as_str(&self) -> Option<&str> {
if let Some(branch) = &self.branch {
Some(branch)
} else if let Some(tag) = &self.tag {
Some(tag)
} else if let Some(rev) = &self.rev {
Some(rev)
} else {
None
}
}
/// Get the type of the reference
pub fn reference_type(&self) -> Option<&str> {
if self.branch.is_some() {
Some("branch")
} else if self.tag.is_some() {
Some("tag")
} else if self.rev.is_some() {
Some("rev")
} else {
None
}
}
}
impl From<GitRev> for GitReference {
fn from(git_rev: GitRev) -> Self {
if let Some(branch) = git_rev.branch {
GitReference::Branch(branch)
} else if let Some(tag) = git_rev.tag {
GitReference::Tag(tag)
} else if let Some(rev) = git_rev.rev {
GitReference::Rev(rev)
} else {
GitReference::DefaultBranch
}
}
}
#[derive(Parser, Debug, Default)]
pub struct DependencyConfig {
/// The dependencies as names, conda MatchSpecs or PyPi requirements
#[arg(required = true)]
pub specs: Vec<String>,
/// The specified dependencies are host dependencies. Conflicts with `build`
/// and `pypi`
#[arg(long, conflicts_with_all = ["build", "pypi"], hide = true)]
pub host: bool,
/// The specified dependencies are build dependencies. Conflicts with `host`
/// and `pypi`
#[arg(long, conflicts_with_all = ["host", "pypi"], hide = true)]
pub build: bool,
/// The specified dependencies are pypi dependencies. Conflicts with `host`
/// and `build`
#[arg(long, conflicts_with_all = ["host", "build"])]
pub pypi: bool,
/// The platform(s) for which the dependency should be modified
#[arg(long = "platform", short)]
pub platforms: Vec<Platform>,
/// The feature for which the dependency should be modified
#[clap(long, short, default_value_t)]
pub feature: FeatureName,
/// The git url to use when adding a git dependency
#[clap(long, short, help_heading = consts::CLAP_GIT_OPTIONS)]
pub git: Option<Url>,
#[clap(flatten)]
/// The git revisions to use when adding a git dependency
pub rev: Option<GitRev>,
/// The subdirectory of the git repository to use
#[clap(long, short, requires = "git", help_heading = consts::CLAP_GIT_OPTIONS)]
pub subdir: Option<String>,
}
impl DependencyConfig {
pub(crate) fn dependency_type(&self) -> DependencyType {
if self.pypi {
DependencyType::PypiDependency
} else if self.host {
DependencyType::CondaDependency(SpecType::Host)
} else if self.build {
DependencyType::CondaDependency(SpecType::Build)
} else {
DependencyType::CondaDependency(SpecType::Run)
}
}
pub(crate) fn display_success(
&self,
operation: &str,
implicit_constraints: HashMap<String, String>,
) {
for package in self.specs.clone() {
eprintln!(
"{}{operation} {}{}",
console::style(console::Emoji("✔ ", "")).green(),
console::style(&package).bold(),
if let Some(constraint) = implicit_constraints.get(&package) {
format!(" {}", console::style(constraint).dim())
} else {
"".to_string()
}
);
}
// Print if it is something different from host and dep
let dependency_type = self.dependency_type();
if !matches!(
dependency_type,
DependencyType::CondaDependency(SpecType::Run)
) {
eprintln!(
"{operation} these as {}.",
console::style(dependency_type.name()).bold()
);
}
// Print something if we've modified for platforms
if !self.platforms.is_empty() {
eprintln!(
"{operation} these only for platform(s): {}",
console::style(self.platforms.iter().join(", ")).bold()
)
}
// Print something if we've modified for features
if let FeatureName::Named(feature) = &self.feature {
{
eprintln!(
"{operation} these only for feature: {}",
consts::FEATURE_STYLE.apply_to(feature)
)
}
}
}
pub fn vcs_pep508_requirements(
&self,
project: &Workspace,
) -> Option<miette::Result<IndexMap<PyPiPackageName, Requirement>>> {
match &self.git {
Some(git) => {
// pep 508 requirements with direct reference
// should be in this format
// name @ url@rev#subdirectory=subdir
// we need to construct it
let pep_reqs: miette::Result<IndexMap<PyPiPackageName, Requirement>> = self
.specs
.iter()
.map(|package_name| {
let vcs_req = build_vcs_requirement(
package_name,
git,
self.rev.as_ref(),
self.subdir.clone(),
);
let dep = Requirement::parse(&vcs_req, project.root()).into_diagnostic()?;
let name = PyPiPackageName::from_normalized(dep.clone().name);
Ok((name, dep))
})
.collect();
Some(pep_reqs)
}
None => None,
}
}
}
impl HasSpecs for DependencyConfig {
fn packages(&self) -> Vec<&str> {
self.specs.iter().map(AsRef::as_ref).collect()
}
}
/// Builds a PEP 508 compliant VCS requirement string.
/// Main difference between a simple VCS requirement is that it encode
/// in a separate query parameter the reference type.
/// This is used to differentiate between a branch, a tag or a revision
/// which is lost in the simple VCS requirement.
/// Return a string in the format `name @ git+url@rev?rev_type=type#subdirectory=subdir`
/// where `rev_type` is added only if reference is present.
fn build_vcs_requirement(
package_name: &str,
git: &Url,
rev: Option<&GitRev>,
subdir: Option<String>,
) -> String {
let scheme = if git.scheme().starts_with("git+") {
""
} else {
"git+"
};
let mut vcs_req = format!("{} @ {}{}", package_name, scheme, git);
if let Some(revision) = rev {
if let Some(rev_str) = revision.as_str().map(|s| s.to_string()) {
vcs_req.push_str(&format!("@{}", rev_str));
if let Some(rev_type) = revision.reference_type() {
vcs_req.push_str(&format!("?{GIT_URL_QUERY_REV_TYPE}={}", rev_type));
}
}
}
if let Some(subdir) = subdir {
vcs_req.push_str(&format!("#subdirectory={}", subdir));
}
vcs_req
}
#[cfg(test)]
mod tests {
use url::Url;
use crate::cli::cli_config::{build_vcs_requirement, GitRev};
#[test]
fn test_build_vcs_requirement_with_all_fields() {
let result = build_vcs_requirement(
"mypackage",
&Url::parse("https://github.com/user/repo").unwrap(),
Some(&GitRev::new().with_tag("v1.0.0".to_string())),
Some("subdir".to_string()),
);
assert_eq!(
result,
"mypackage @ git+https://github.com/user/repo@v1.0.0?rev_type=tag#subdirectory=subdir"
);
}
#[test]
fn test_build_vcs_requirement_with_no_rev() {
let result = build_vcs_requirement(
"mypackage",
&Url::parse("https://github.com/user/repo").unwrap(),
None,
Some("subdir".to_string()),
);
assert_eq!(
result,
"mypackage @ git+https://github.com/user/repo#subdirectory=subdir"
);
}
#[test]
fn test_build_vcs_requirement_with_no_subdir() {
let result = build_vcs_requirement(
"mypackage",
&Url::parse("https://github.com/user/repo").unwrap(),
Some(&GitRev::new().with_tag("v1.0.0".to_string())),
None,
);
assert_eq!(
result,
"mypackage @ git+https://github.com/user/repo@v1.0.0?rev_type=tag"
);
}
#[test]
fn test_build_vcs_requirement_with_only_git() {
let result = build_vcs_requirement(
"mypackage",
&Url::parse("https://github.com/user/repo").unwrap(),
None,
None,
);
assert_eq!(result, "mypackage @ git+https://github.com/user/repo");
}
}