Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --pin argument to rye add #341

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. #341

- 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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also fixes a bug that sneaked in.

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