Skip to content

Commit

Permalink
refactor: Remove project level method that are per environment (#1412)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-lacroix authored May 19, 2024
1 parent 4c1cc5d commit 6e00256
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 108 deletions.
19 changes: 11 additions & 8 deletions src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,9 @@ pub async fn execute(args: Args) -> miette::Result<()> {
verify_prefix_location_unchanged(project.default_environment().dir().as_path()).await?;

// Add the platform if it is not already present
let platforms_to_add = spec_platforms
.iter()
.filter(|p| !project.platforms().contains(p))
.cloned()
.collect::<Vec<Platform>>();
project
.manifest
.add_platforms(platforms_to_add.iter(), &FeatureName::Default)?;
.add_platforms(spec_platforms.iter(), &FeatureName::Default)?;

let feature_name = args
.feature
Expand Down Expand Up @@ -376,7 +371,11 @@ async fn determine_latest_versions(
) -> miette::Result<Vec<Version>> {
// Get platforms to search for including NoArch
let platforms = if platforms.is_empty() {
let mut temp = project.platforms().into_iter().collect_vec();
let mut temp = project
.default_environment()
.platforms()
.into_iter()
.collect_vec();
temp.push(Platform::NoArch);
temp
} else {
Expand All @@ -389,7 +388,11 @@ async fn determine_latest_versions(
let records = project
.repodata_gateway()
.query(
project.channels().into_iter().cloned(),
project
.default_environment()
.channels()
.into_iter()
.cloned(),
platforms,
[name.clone()],
)
Expand Down
2 changes: 1 addition & 1 deletion src/cli/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
index_locations = environment.pypi_options().to_index_locations();
tags = get_pypi_tags(
platform,
&project.system_requirements(),
&environment.system_requirements(),
python_record.package_record(),
)?;
Some(RegistryWheelIndex::new(
Expand Down
8 changes: 7 additions & 1 deletion src/cli/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tokio::task::spawn_blocking;
use crate::config::Config;
use crate::util::default_channel_config;
use crate::utils::reqwest::build_reqwest_clients;
use crate::HasFeatures;
use crate::{progress::await_in_progress, repodata::fetch_sparse_repodata, Project};

/// Search a package, output will list the latest version of package
Expand Down Expand Up @@ -115,7 +116,12 @@ pub async fn execute(args: Args) -> miette::Result<()> {
}
// if user doesn't pass channels and we are in a project
(None, Some(p)) => {
let channels: Vec<_> = p.channels().into_iter().cloned().collect();
let channels: Vec<_> = p
.default_environment()
.channels()
.into_iter()
.cloned()
.collect();
eprintln!(
"Using channels from project ({}): {}",
p.name(),
Expand Down
121 changes: 32 additions & 89 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ mod solve_group;
pub mod virtual_packages;

use async_once_cell::OnceCell as AsyncCell;
use indexmap::{Equivalent, IndexSet};
use indexmap::Equivalent;
use miette::{IntoDiagnostic, NamedSource};

use rattler_conda_types::{Channel, Platform, Version};
use rattler_conda_types::Version;
use reqwest_middleware::ClientWithMiddleware;
use std::hash::Hash;

use rattler_repodata_gateway::Gateway;
use rattler_virtual_packages::VirtualPackage;
use std::sync::OnceLock;
use std::{
collections::{HashMap, HashSet},
Expand All @@ -29,20 +28,13 @@ use std::{

use crate::activation::{get_environment_variables, run_activation};
use crate::config::Config;
use crate::consts::{self, PROJECT_MANIFEST, PYPROJECT_MANIFEST};
use crate::project::grouped_environment::GroupedEnvironment;
use crate::pypi_mapping::MappingSource;
use crate::task::TaskName;
use crate::utils::reqwest::build_reqwest_clients;
use crate::{
consts::{self, PROJECT_MANIFEST, PYPROJECT_MANIFEST},
task::Task,
};
use manifest::{EnvironmentName, Manifest, SystemRequirements};
use manifest::{EnvironmentName, Manifest};

use self::{
has_features::HasFeatures,
manifest::{pyproject::PyProjectToml, Environments},
};
use self::manifest::{pyproject::PyProjectToml, Environments};
pub use dependencies::{CondaDependencies, PyPiDependencies};
pub use environment::Environment;
pub use solve_group::SolveGroup;
Expand Down Expand Up @@ -394,76 +386,6 @@ impl Project {
environments.into_iter().collect()
}

/// Returns the channels used by this project.
///
/// TODO: Remove this function and use the channels from the default environment instead.
pub fn channels(&self) -> IndexSet<&Channel> {
self.default_environment().channels()
}

/// Returns the platforms this project targets
///
/// TODO: Remove this function and use the platforms from the default environment instead.
pub fn platforms(&self) -> HashSet<Platform> {
self.default_environment().platforms()
}

/// Get the tasks of this project
///
/// TODO: Remove this function and use the tasks from the default environment instead.
pub fn tasks(&self, platform: Option<Platform>) -> HashMap<&TaskName, &Task> {
self.default_environment()
.tasks(platform)
.unwrap_or_default()
}

/// Get the task with the specified `name` or `None` if no such task exists. If `platform` is
/// specified then the task will first be looked up in the target specific tasks for the given
/// platform.
///
/// TODO: Remove this function and use the `task` function from the default environment instead.
pub fn task_opt(&self, name: &TaskName, platform: Option<Platform>) -> Option<&Task> {
self.default_environment().task(name, platform).ok()
}

/// TODO: Remove this method and use the one from Environment instead.
pub fn virtual_packages(&self, platform: Platform) -> Vec<VirtualPackage> {
self.default_environment().virtual_packages(platform)
}

/// Get the system requirements defined under the `system-requirements` section of the project manifest.
/// They will act as the description of a reference machine which is minimally needed for this package to be run.
///
/// TODO: Remove this function and use the `system_requirements` function from the default environment instead.
pub fn system_requirements(&self) -> SystemRequirements {
self.default_environment().system_requirements()
}

/// Returns the dependencies of the project.
///
/// TODO: Remove this function and use the `dependencies` function from the default environment instead.
pub fn dependencies(
&self,
kind: Option<SpecType>,
platform: Option<Platform>,
) -> CondaDependencies {
self.default_environment().dependencies(kind, platform)
}

/// Returns the PyPi dependencies of the project
///
/// TODO: Remove this function and use the `dependencies` function from the default environment instead.
pub fn pypi_dependencies(&self, platform: Option<Platform>) -> PyPiDependencies {
self.default_environment().pypi_dependencies(platform)
}

/// Returns the all specified activation scripts that are used in the current platform.
///
/// TODO: Remove this function and use the `activation_scripts function from the default environment instead.
pub fn activation_scripts(&self, platform: Option<Platform>) -> Vec<String> {
self.default_environment().activation_scripts(platform)
}

/// Returns true if the project contains any reference pypi dependencies. Even if just
/// `[pypi-dependencies]` is specified without any requirements this will return true.
pub fn has_pypi_dependencies(&self) -> bool {
Expand Down Expand Up @@ -550,10 +472,12 @@ pub fn find_project_manifest() -> Option<PathBuf> {

#[cfg(test)]
mod tests {
use self::has_features::HasFeatures;
use super::*;
use crate::project::manifest::FeatureName;
use insta::{assert_debug_snapshot, assert_snapshot};
use itertools::Itertools;
use rattler_conda_types::Platform;
use rattler_virtual_packages::{LibC, VirtualPackage};
use std::str::FromStr;

Expand Down Expand Up @@ -597,7 +521,10 @@ mod tests {
version: Version::from_str("2.12").unwrap(),
})];

let virtual_packages = project.system_requirements().virtual_packages();
let virtual_packages = project
.default_environment()
.system_requirements()
.virtual_packages();

assert_eq!(virtual_packages, expected_result);
}
Expand Down Expand Up @@ -630,7 +557,9 @@ mod tests {
let project = Project::from_manifest(manifest);

assert_snapshot!(format_dependencies(
project.dependencies(None, Some(Platform::Linux64))
project
.default_environment()
.dependencies(None, Some(Platform::Linux64))
));
}

Expand Down Expand Up @@ -663,7 +592,9 @@ mod tests {
let project = Project::from_manifest(manifest);

assert_snapshot!(format_dependencies(
project.dependencies(None, Some(Platform::Linux64))
project
.default_environment()
.dependencies(None, Some(Platform::Linux64))
));
}

Expand Down Expand Up @@ -693,9 +624,21 @@ mod tests {

assert_snapshot!(format!(
"= Linux64\n{}\n\n= Win64\n{}\n\n= OsxArm64\n{}",
fmt_activation_scripts(project.activation_scripts(Some(Platform::Linux64))),
fmt_activation_scripts(project.activation_scripts(Some(Platform::Win64))),
fmt_activation_scripts(project.activation_scripts(Some(Platform::OsxArm64)))
fmt_activation_scripts(
project
.default_environment()
.activation_scripts(Some(Platform::Linux64))
),
fmt_activation_scripts(
project
.default_environment()
.activation_scripts(Some(Platform::Win64))
),
fmt_activation_scripts(
project
.default_environment()
.activation_scripts(Some(Platform::OsxArm64))
)
));
}

Expand Down
14 changes: 10 additions & 4 deletions tests/add_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::common::package_database::{Package, PackageDatabase};
use crate::common::LockFileExt;
use crate::common::PixiControl;
use pixi::consts::DEFAULT_ENVIRONMENT_NAME;
use pixi::{DependencyType, SpecType};
use pixi::{DependencyType, HasFeatures, SpecType};
use rattler_conda_types::{PackageName, Platform};
use serial_test::serial;
use std::str::FromStr;
Expand Down Expand Up @@ -100,13 +100,19 @@ async fn add_functionality_union() {
let project = pixi.project().unwrap();

// Should contain all added dependencies
let dependencies = project.dependencies(Some(SpecType::Run), Some(Platform::current()));
let dependencies = project
.default_environment()
.dependencies(Some(SpecType::Run), Some(Platform::current()));
let (name, _) = dependencies.into_specs().next().unwrap();
assert_eq!(name, PackageName::try_from("rattler").unwrap());
let host_deps = project.dependencies(Some(SpecType::Host), Some(Platform::current()));
let host_deps = project
.default_environment()
.dependencies(Some(SpecType::Host), Some(Platform::current()));
let (name, _) = host_deps.into_specs().next().unwrap();
assert_eq!(name, PackageName::try_from("libcomputer").unwrap());
let build_deps = project.dependencies(Some(SpecType::Build), Some(Platform::current()));
let build_deps = project
.default_environment()
.dependencies(Some(SpecType::Build), Some(Platform::current()));
let (name, _) = build_deps.into_specs().next().unwrap();
assert_eq!(name, PackageName::try_from("libidk").unwrap());

Expand Down
6 changes: 3 additions & 3 deletions tests/init_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod common;

use crate::common::PixiControl;
use pixi::util::default_channel_config;
use pixi::{util::default_channel_config, HasFeatures};
use rattler_conda_types::{Channel, Version};
use std::str::FromStr;

Expand Down Expand Up @@ -43,7 +43,7 @@ async fn specific_channel() {
let project = pixi.project().unwrap();

// The only channel should be the "random" channel
let channels = Vec::from_iter(project.channels());
let channels = Vec::from_iter(project.default_environment().channels());
assert_eq!(
channels,
[
Expand All @@ -65,7 +65,7 @@ async fn default_channel() {
let project = pixi.project().unwrap();

// The only channel should be the "conda-forge" channel
let channels = Vec::from_iter(project.channels());
let channels = Vec::from_iter(project.default_environment().channels());
assert_eq!(
channels,
[&Channel::from_str("conda-forge", &default_channel_config()).unwrap()]
Expand Down
8 changes: 6 additions & 2 deletions tests/project_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;

use crate::{common::package_database::PackageDatabase, common::PixiControl};
use insta::assert_debug_snapshot;
use pixi::{util::default_channel_config, Project};
use pixi::{util::default_channel_config, HasFeatures, Project};
use rattler_conda_types::{Channel, Platform};
use tempfile::TempDir;
use url::Url;
Expand Down Expand Up @@ -47,13 +47,17 @@ async fn add_channel() {
&default_channel_config(),
)
.unwrap();
assert!(project.channels().contains(&local_channel));
assert!(project
.default_environment()
.channels()
.contains(&local_channel));
}

#[tokio::test]
async fn parse_project() {
fn dependency_names(project: &Project, platform: Platform) -> Vec<String> {
project
.default_environment()
.dependencies(None, Some(platform))
.iter()
.map(|dep| dep.0.as_normalized().to_string())
Expand Down

0 comments on commit 6e00256

Please sign in to comment.