Skip to content

Commit

Permalink
refactor: everything uses get_up_to_date_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
tdejager committed Jan 1, 2024
1 parent e7b8626 commit 018bb34
Showing 1 changed file with 16 additions and 59 deletions.
75 changes: 16 additions & 59 deletions src/cli/add.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use crate::environment::{update_prefix_conda, verify_prefix_location_unchanged};
use crate::lock_file::update_lock_file_for_pypi;
use crate::prefix::Prefix;
use crate::environment::{get_up_to_date_prefix, verify_prefix_location_unchanged, LockFileUsage};

use crate::project::{DependencyType, SpecType};
use crate::{
consts,
lock_file::{load_lock_file, update_lock_file_conda},
project::python::PyPiRequirement,
project::Project,
consts, project::python::PyPiRequirement, project::Project,
virtual_packages::get_minimal_virtual_packages,
};
use clap::Parser;
use indexmap::IndexMap;
use itertools::Itertools;

use miette::{IntoDiagnostic, WrapErr};
use rattler_conda_types::version_spec::{LogicalOperator, RangeOperator};
use rattler_conda_types::{
Expand Down Expand Up @@ -219,8 +216,13 @@ pub async fn add_pypi_specs_to_project(
}
}
}
let lock_file_usage = if no_update_lockfile {
LockFileUsage::Frozen
} else {
LockFileUsage::Update
};

update_environment(project, None, no_install, no_update_lockfile).await?;
get_up_to_date_prefix(project, lock_file_usage, no_install).await?;

project.save()?;

Expand Down Expand Up @@ -318,62 +320,17 @@ pub async fn add_conda_specs_to_project(
}
}
}
project.save()?;

update_environment(
project,
Some(sparse_repo_data),
no_install,
no_update_lockfile,
)
.await?;

Ok(())
}

/// Updates the lock file and potentially the prefix to get an up-to-date environment.
///
/// We are using this function instead of [`crate::environment::get_up_to_date_prefix`] because we want to be able to
/// specify if we do not want to update the prefix. Also we know the lock file needs to be updated so `--frozen` and `--locked`
/// make no sense in this scenario.
///
/// Essentially, other than that it does almost the same thing
async fn update_environment(
project: &Project,
sparse_repo_data: Option<Vec<SparseRepoData>>,
no_install: bool,
no_update_lockfile: bool,
) -> miette::Result<()> {
// Update the lock file
let lock_file = if !no_update_lockfile {
let lock =
update_lock_file_conda(project, load_lock_file(project).await?, sparse_repo_data)
.await?;

if project.has_pypi_dependencies() {
update_lock_file_for_pypi(project, lock).await?.into()
} else {
lock.into()
}
let lock_file_usage = if no_update_lockfile {
LockFileUsage::Frozen
} else {
None
LockFileUsage::Update
};
get_up_to_date_prefix(project, lock_file_usage, no_install).await?;
project.save()?;

if let Some(lock_file) = lock_file {
if !no_install {
crate::environment::sanity_check_project(project)?;

// Get the currently installed packages
let prefix = Prefix::new(project.environment_dir())?;
let installed_packages = prefix.find_installed_packages(None).await?;

// Update the prefix
update_prefix_conda(&prefix, installed_packages, &lock_file, Platform::current())
.await?;
}
}
Ok(())
}

/// Given several specs determines the highest installable version for them.
pub fn determine_best_version(
new_specs: &HashMap<PackageName, NamelessMatchSpec>,
Expand Down

0 comments on commit 018bb34

Please sign in to comment.