Skip to content

Commit

Permalink
Remove deps with --project
Browse files Browse the repository at this point in the history
  • Loading branch information
ischaojie committed Jun 17, 2023
1 parent 685c2ff commit a058771
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
32 changes: 9 additions & 23 deletions rye/src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use url::Url;

use crate::bootstrap::ensure_self_venv;
use crate::consts::VENV_BIN;
use crate::pyproject::{BuildSystem, DependencyKind, ExpandedSources, PyProject};
use crate::pyproject::{
find_project_by_name, BuildSystem, DependencyKind, ExpandedSources, PyProject,
};
use crate::utils::{format_requirement, set_proxy_variables, CommandOutput};

const PACKAGE_FINDER_SCRIPT: &str = r#"
Expand Down Expand Up @@ -126,18 +128,10 @@ impl ReqExtras {
)),
};
} else if let Some(ref path) = self.path {
let mut pyproject_toml = PyProject::discover()?;
let project_dir = match &self.project {
Some(name) => {
let mut pyproject_toml = PyProject::discover()?;
if let Some(workspace) = pyproject_toml.workspace() {
if let Some(project) = workspace.get_project(&name)? {
pyproject_toml = project;
} else {
bail!("project {} not found", name);
}
} else {
bail!("no workspace found");
}
pyproject_toml = find_project_by_name(&name)?;
pyproject_toml.root_path().as_ref().to_owned()
}
None => env::current_dir()?,
Expand All @@ -146,7 +140,7 @@ impl ReqExtras {
// but this not supported by pip-tools,
// and use ${PROJECT_ROOT} will cause error in hatchling, so force absolute path.
let is_hatchling =
PyProject::discover()?.build_backend().unwrap() == BuildSystem::Hatchling;
pyproject_toml.build_backend().unwrap() == BuildSystem::Hatchling;
let file_url = if self.absolute || is_hatchling {
Url::from_file_path(project_dir.join(path))
.map_err(|_| anyhow!("unable to interpret '{}' as path", path.display()))?
Expand Down Expand Up @@ -211,17 +205,9 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
let mut added = Vec::new();
python_path.push(VENV_BIN);
python_path.push("python");
let mut pyproject_toml = PyProject::discover()?;
if let Some(ref name) = cmd.req_extras.project {
if let Some(workspace) = pyproject_toml.workspace() {
if let Some(project) = workspace.get_project(&name)? {
pyproject_toml = project;
} else {
bail!("project {} not found", name);
}
} else {
bail!("no workspace found");
}
let mut pyproject_toml = match cmd.req_extras.project {
Some(ref name) => find_project_by_name(&name)?,
None => PyProject::discover()?,
};
let py_ver = match pyproject_toml.target_python_version() {
Some(ver) => ver.format_simple(),
Expand Down
10 changes: 8 additions & 2 deletions rye/src/cli/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Error;
use clap::Parser;
use pep508_rs::Requirement;

use crate::pyproject::{DependencyKind, PyProject};
use crate::pyproject::{find_project_by_name, DependencyKind, PyProject};
use crate::utils::{format_requirement, CommandOutput};

/// Removes a package from this project.
Expand All @@ -18,6 +18,9 @@ pub struct Args {
/// Remove this from an optional dependency group.
#[arg(long, conflicts_with = "dev")]
optional: Option<String>,
/// Remove from a specific project.
#[arg(long)]
project: Option<String>,
/// Enables verbose diagnostics.
#[arg(short, long)]
verbose: bool,
Expand All @@ -30,7 +33,10 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
let output = CommandOutput::from_quiet_and_verbose(cmd.quiet, cmd.verbose);
let mut removed_packages = Vec::new();

let mut pyproject_toml = PyProject::discover()?;
let mut pyproject_toml = match cmd.project {
Some(name) => find_project_by_name(&name)?,
None => PyProject::discover()?,
};
for str_requirement in cmd.requirements {
let requirement = Requirement::from_str(&str_requirement)?;
if let Some(removed) = pyproject_toml.remove_dependency(
Expand Down
14 changes: 14 additions & 0 deletions rye/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,20 @@ fn is_rye_managed(doc: &Document) -> bool {
.unwrap_or(false)
}

pub fn find_project_by_name(name: &str) -> Result<PyProject, Error> {
let mut pyproject_toml = PyProject::discover()?;
if let Some(workspace) = pyproject_toml.workspace() {
if let Some(project) = workspace.get_project(&name)? {
pyproject_toml = project;
} else {
bail!("project {} not found", name);
}
} else {
bail!("no workspace found");
}
Ok(pyproject_toml)
}

/// Represents expanded sources.
#[derive(Debug, Clone, Serialize)]
pub struct ExpandedSources {
Expand Down

0 comments on commit a058771

Please sign in to comment.