Skip to content

Commit

Permalink
Added --pin argument to rye add
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jun 20, 2023
1 parent f684905 commit 2a4d585
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ that were not yet released.

_Unreleased_

- `rye add` now accepts `--pin` to let one override the type of pin to use.

- Added `rye config` to read and manipulate the `config.toml` file. #339

- Added support for the new `behavior.global-python` flag which turns on global
Expand Down
37 changes: 31 additions & 6 deletions rye/src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::process::{Command, Stdio};
use std::str::FromStr;

use anyhow::{anyhow, bail, Context, Error};
use clap::Parser;
use clap::{Parser, ValueEnum};
use console::style;
use pep440_rs::{Operator, Version, VersionSpecifier, VersionSpecifiers};
use pep508_rs::{Requirement, VersionOrUrl};
Expand Down Expand Up @@ -88,6 +88,26 @@ pub struct ReqExtras {
features: Vec<String>,
}

#[derive(ValueEnum, Copy, Clone, Debug, PartialEq)]
enum Pin {
#[value(alias = "exact", alias = "==", alias = "eq")]
Equal,
#[value(alias = "tilde", alias = "compatible", alias = "~=")]
TildeEqual,
#[value(alias = ">=", alias = "ge", alias = "gte")]
GreaterThanEqual,
}

impl From<Pin> for Operator {
fn from(value: Pin) -> Self {
match value {
Pin::Equal => Operator::Equal,
Pin::TildeEqual => Operator::TildeEqual,
Pin::GreaterThanEqual => Operator::GreaterThanEqual,
}
}
}

impl ReqExtras {
pub fn force_absolute(&mut self) {
self.absolute = true;
Expand Down Expand Up @@ -178,6 +198,9 @@ pub struct Args {
/// Include pre-releases when finding a package version.
#[arg(long)]
pre: bool,
/// Overrides the pin operator
#[arg(long)]
pin: Option<Pin>,
/// Enables verbose diagnostics.
#[arg(short, long)]
verbose: bool,
Expand Down Expand Up @@ -207,7 +230,10 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
} else {
DependencyKind::Normal
};
let default_operator = Config::current().default_dependency_operator();
let default_operator = match cmd.pin {
Some(pin) => Operator::from(pin),
None => Config::current().default_dependency_operator(),
};

for str_requirement in cmd.requirements {
let mut requirement = Requirement::from_str(&str_requirement)?;
Expand Down Expand Up @@ -290,10 +316,9 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
// local versions or versions with only one component cannot
// use ~= but need to use ==.
match default_operator {
Operator::EqualStar
if version.is_local() || version.release.len() < 2 =>
{
Operator::TildeEqual
_ if version.is_local() => Operator::Equal,
Operator::TildeEqual if version.release.len() < 2 => {
Operator::GreaterThanEqual
}
ref other => other.clone(),
},
Expand Down

0 comments on commit 2a4d585

Please sign in to comment.