Skip to content

Commit

Permalink
refactor: move code from Project to Environment (#630)
Browse files Browse the repository at this point in the history
This PR refactors the code to extract project information from
`Environment`s instead of directly from the project.

This initial PR tries to retain the same surface level API as before
where the accessors on the `Project` struct remains intact.

A next PR will remove this functions and force users to go through
`Environment`s instead of `Project`.

This PR currently refactos the `platforms`, `channels` and `tasks`
functions. I will create a follow-up PRs to also refactor the
`system-requirements`, `activation` and `dependency` functions.
  • Loading branch information
baszalmstra authored Jan 8, 2024
1 parent 2871062 commit 89356a7
Show file tree
Hide file tree
Showing 19 changed files with 656 additions and 198 deletions.
10 changes: 5 additions & 5 deletions src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};
use clap::Parser;
use indexmap::IndexMap;
use itertools::Itertools;
use itertools::{Either, Itertools};

use miette::{IntoDiagnostic, WrapErr};
use rattler_conda_types::{
Expand Down Expand Up @@ -255,11 +255,11 @@ pub async fn add_conda_specs_to_project(
let mut package_versions = HashMap::<PackageName, HashSet<Version>>::new();

let platforms = if specs_platforms.is_empty() {
project.platforms()
Either::Left(project.platforms().into_iter())
} else {
specs_platforms
}
.to_vec();
Either::Right(specs_platforms.iter().copied())
};

for platform in platforms {
// TODO: `build` and `host` has to be separated when we have separated environments for them.
// While we combine them on install we should also do that on getting the best version.
Expand Down
2 changes: 1 addition & 1 deletion src/cli/global/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
let platform = Platform::current();

// Fetch sparse repodata
let platform_sparse_repodata = fetch_sparse_repodata(&channels, &[platform]).await?;
let platform_sparse_repodata = fetch_sparse_repodata(&channels, [platform]).await?;

let available_packages = SparseRepoData::load_records_recursive(
platform_sparse_repodata.iter(),
Expand Down
2 changes: 1 addition & 1 deletion src/cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
package_count: dependency_count(&p).ok(),
environment_size,
last_updated: last_updated(p.lock_file_path()).ok(),
platforms: p.platforms().to_vec(),
platforms: p.platforms().into_iter().collect(),
});

let virtual_packages = VirtualPackage::current()
Expand Down
2 changes: 1 addition & 1 deletion src/cli/project/channel/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Args {
}

pub async fn execute(project: Project, args: Args) -> miette::Result<()> {
project.channels().iter().for_each(|channel| {
project.channels().into_iter().for_each(|channel| {
if args.urls {
// Print the channel's url
println!("{}", channel.base_url());
Expand Down
16 changes: 10 additions & 6 deletions src/cli/search.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::{cmp::Ordering, path::PathBuf};

use clap::Parser;
Expand Down Expand Up @@ -81,22 +82,25 @@ pub async fn execute(args: Args) -> miette::Result<()> {

let channel_config = ChannelConfig::default();

let channels = match (args.channel, project) {
let channels = match (args.channel, project.as_ref()) {
// if user passes channels through the channel flag
(Some(c), _) => c
.iter()
.map(|c| Channel::from_str(c, &channel_config))
.collect::<Result<Vec<Channel>, _>>()
.map_ok(Cow::Owned)
.collect::<Result<Vec<_>, _>>()
.into_diagnostic()?,
// if user doesn't pass channels and we are in a project
(None, Some(p)) => p.channels().to_owned(),
(None, Some(p)) => p.channels().into_iter().map(Cow::Borrowed).collect(),
// if user doesn't pass channels and we are not in project
(None, None) => vec![Channel::from_str("conda-forge", &channel_config).into_diagnostic()?],
(None, None) => vec![Cow::Owned(
Channel::from_str("conda-forge", &channel_config).into_diagnostic()?,
)],
};

let package_name_filter = args.package;
let platforms = [Platform::current()];
let repo_data = fetch_sparse_repodata(&channels, &platforms).await?;
let repo_data =
fetch_sparse_repodata(channels.iter().map(AsRef::as_ref), [Platform::current()]).await?;

// When package name filter contains * (wildcard), it will search and display a list of packages matching this filter
if package_name_filter.contains('*') {
Expand Down
34 changes: 20 additions & 14 deletions src/cli/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,22 @@ pub fn execute(args: Args) -> miette::Result<()> {
}

// Check if task has dependencies
let depends_on = project.task_names_depending_on(name);
if !depends_on.is_empty() && !args.names.contains(name) {
eprintln!(
"{}: {}",
console::style("Warning, the following task/s depend on this task")
.yellow(),
console::style(depends_on.iter().to_owned().join(", ")).bold()
);
eprintln!(
"{}",
console::style("Be sure to modify these after the removal\n").yellow()
);
}
// TODO: Make this properly work by inspecting which actual tasks depend on the task
// we just removed taking into account environments and features.
// let depends_on = project.task_names_depending_on(name);
// if !depends_on.is_empty() && !args.names.contains(name) {
// eprintln!(
// "{}: {}",
// console::style("Warning, the following task/s depend on this task")
// .yellow(),
// console::style(depends_on.iter().to_owned().join(", ")).bold()
// );
// eprintln!(
// "{}",
// console::style("Be sure to modify these after the removal\n").yellow()
// );
// }

// Safe to remove
to_remove.push((name, args.platform));
}
Expand Down Expand Up @@ -220,7 +223,10 @@ pub fn execute(args: Args) -> miette::Result<()> {
);
}
Operation::List(args) => {
let tasks = project.task_names(Some(Platform::current()));
let tasks = project
.tasks(Some(Platform::current()))
.into_keys()
.collect_vec();
if tasks.is_empty() {
eprintln!("No tasks found",);
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ pub const PIXI_DIR: &str = ".pixi";
pub const PREFIX_FILE_NAME: &str = "prefix";
pub const ENVIRONMENT_DIR: &str = "env";
pub const PYPI_DEPENDENCIES: &str = "pypi-dependencies";

pub const DEFAULT_ENVIRONMENT_NAME: &str = "default";
15 changes: 7 additions & 8 deletions src/lock_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ fn main_progress_bar(num_bars: u64, message: &'static str) -> ProgressBar {
top_level_progress
}

fn platform_solve_bars(platforms: &[Platform]) -> Vec<ProgressBar> {
fn platform_solve_bars(platforms: impl IntoIterator<Item = Platform>) -> Vec<ProgressBar> {
platforms
.iter()
.into_iter()
.map(|platform| {
let pb =
progress::global_multi_progress().add(ProgressBar::new(platforms.len() as u64));
let pb = progress::global_multi_progress().add(ProgressBar::new(0));
pb.set_style(
indicatif::ProgressStyle::with_template(&format!(
" {:<9} ..",
Expand Down Expand Up @@ -87,12 +86,12 @@ pub async fn update_lock_file_conda(
let _top_level_progress =
main_progress_bar(platforms.len() as u64, "resolving conda dependencies");
// Create progress bars for each platform
let solve_bars = platform_solve_bars(platforms);
let solve_bars = platform_solve_bars(platforms.iter().copied());

// Construct a conda lock file
let channels = project
.channels()
.iter()
.into_iter()
.map(|channel| rattler_lock::Channel::from(channel.base_url().to_string()));

let result: miette::Result<Vec<_>> =
Expand Down Expand Up @@ -162,7 +161,7 @@ pub async fn update_lock_file_for_pypi(
let platforms = project.platforms();
let _top_level_progress =
main_progress_bar(platforms.len() as u64, "resolving pypi dependencies");
let solve_bars = platform_solve_bars(platforms);
let solve_bars = platform_solve_bars(platforms.iter().copied());

let records = platforms
.iter()
Expand Down Expand Up @@ -216,7 +215,7 @@ pub async fn update_lock_file_for_pypi(

let channels = project
.channels()
.iter()
.into_iter()
.map(|channel| rattler_lock::Channel::from(channel.base_url().to_string()));
let mut builder = LockFileBuilder::new(channels, platforms.iter().cloned(), vec![]);
for locked_packages in result? {
Expand Down
2 changes: 1 addition & 1 deletion src/lock_file/satisfiability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn lock_file_satisfies_project(
// result.
let channels = project
.channels()
.iter()
.into_iter()
.map(|channel| rattler_lock::Channel::from(channel.base_url().to_string()))
.collect_vec();
if lock_file.metadata.channels.iter().ne(channels.iter()) {
Expand Down
Loading

0 comments on commit 89356a7

Please sign in to comment.