Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
67 changes: 63 additions & 4 deletions crates/pixi_api/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::collections::HashMap;

use pixi_core::{Workspace, environment::LockFileUsage};
use pixi_manifest::{EnvironmentName, FeatureName, Task, TaskName};
use rattler_conda_types::Platform;

use crate::interface::Interface;
use crate::workspace::{init::InitOptions, reinstall::ReinstallOptions};
use crate::workspace::{InitOptions, ReinstallOptions};

pub struct WorkspaceContext<I: Interface> {
interface: I,
Expand All @@ -16,16 +20,71 @@ impl<I: Interface> WorkspaceContext<I> {
}
}

pub fn workspace(&self) -> &Workspace {
&self.workspace
}

pub async fn init(interface: I, options: InitOptions) -> miette::Result<Workspace> {
crate::workspace::init::init(&interface, options).await
}

pub async fn name(&self) -> String {
crate::workspace::workspace::name::get(self.workspace.clone()).await
crate::workspace::workspace::name::get(&self.workspace).await
}

pub async fn set_name(&self, name: &str) -> miette::Result<()> {
crate::workspace::workspace::name::set(&self.interface, self.workspace.clone(), name).await
crate::workspace::workspace::name::set(&self.interface, &self.workspace, name).await
}

pub async fn list_tasks(
&self,
environment: Option<EnvironmentName>,
) -> miette::Result<HashMap<EnvironmentName, HashMap<TaskName, Task>>> {
crate::workspace::task::list_tasks(&self.interface, &self.workspace, environment).await
}

pub async fn add_task(
&self,
name: TaskName,
task: Task,
feature: FeatureName,
platform: Option<Platform>,
) -> miette::Result<()> {
crate::workspace::task::add_task(
&self.interface,
&self.workspace,
name,
task,
feature,
platform,
)
.await
}

pub async fn alias_task(
&self,
name: TaskName,
task: Task,
platform: Option<Platform>,
) -> miette::Result<()> {
crate::workspace::task::alias_task(&self.interface, &self.workspace, name, task, platform)
.await
}

pub async fn remove_task(
&self,
names: Vec<TaskName>,
platform: Option<Platform>,
feature: FeatureName,
) -> miette::Result<()> {
crate::workspace::task::remove_tasks(
&self.interface,
&self.workspace,
names,
platform,
feature,
)
.await
}

pub async fn reinstall(
Expand All @@ -35,8 +94,8 @@ impl<I: Interface> WorkspaceContext<I> {
) -> miette::Result<()> {
crate::workspace::reinstall::reinstall(
&self.interface,
&self.workspace,
options,
self.workspace.clone(),
lock_file_usage,
)
.await
Expand Down
9 changes: 7 additions & 2 deletions crates/pixi_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
pub mod workspace;

pub mod context;
pub mod interface;
mod context;
pub use context::WorkspaceContext;

mod interface;
pub use interface::Interface;

// Reexport for pixi_api consumers
pub use pixi_core as core;
pub use pixi_manifest as manifest;
pub use rattler_conda_types;
5 changes: 1 addition & 4 deletions crates/pixi_api/src/workspace/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ mod template;

pub use options::{GitAttributes, InitOptions, ManifestFormat};

pub(crate) async fn init<I: Interface>(
interface: &I,
options: InitOptions,
) -> miette::Result<Workspace> {
pub async fn init<I: Interface>(interface: &I, options: InitOptions) -> miette::Result<Workspace> {
let env = Environment::new();
// Fail silently if the directory already exists or cannot be created.
fs_err::create_dir_all(&options.path).into_diagnostic()?;
Expand Down
12 changes: 9 additions & 3 deletions crates/pixi_api/src/workspace/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
pub mod init;
pub mod reinstall;
pub(crate) mod init;
pub use init::{GitAttributes, InitOptions, ManifestFormat};

pub(crate) mod reinstall;
pub use reinstall::ReinstallOptions;

pub(crate) mod task;

#[allow(clippy::module_inception)]
pub mod workspace;
pub(crate) mod workspace;
4 changes: 2 additions & 2 deletions crates/pixi_api/src/workspace/reinstall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ mod options;

pub use options::ReinstallOptions;

pub(crate) async fn reinstall<I: Interface>(
pub async fn reinstall<I: Interface>(
interface: &I,
workspace: &Workspace,
options: ReinstallOptions,
workspace: Workspace,
lock_file_usage: LockFileUsage,
) -> miette::Result<()> {
// Install either:
Expand Down
184 changes: 184 additions & 0 deletions crates/pixi_api/src/workspace/task/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
use std::collections::{HashMap, HashSet};

use fancy_display::FancyDisplay;
use miette::IntoDiagnostic;
use pixi_core::{
Workspace,
workspace::{Environment, virtual_packages::verify_current_platform_can_run_environment},
};
use pixi_manifest::{EnvironmentName, FeatureName, Task, TaskName};
use rattler_conda_types::Platform;

use crate::interface::Interface;

pub async fn list_tasks<I: Interface>(
_interface: &I,
workspace: &Workspace,
environment: Option<EnvironmentName>,
) -> miette::Result<HashMap<EnvironmentName, HashMap<TaskName, Task>>> {
let explicit_environment = environment
.map(|n| {
workspace
.environment(&n)
.ok_or_else(|| miette::miette!("unknown environment '{n}'"))
})
.transpose()?;

let lockfile = workspace.load_lock_file().await.ok();

let env_task_map: HashMap<Environment, HashSet<TaskName>> =
if let Some(explicit_environment) = explicit_environment {
HashMap::from([(
explicit_environment.clone(),
explicit_environment.get_filtered_tasks(),
)])
} else {
workspace
.environments()
.iter()
.filter_map(|env| {
if verify_current_platform_can_run_environment(env, lockfile.as_ref()).is_ok() {
Some((env.clone(), env.get_filtered_tasks()))
} else {
None
}
})
.collect()
};

Ok(env_task_map
.into_iter()
.map(|(env, task_names)| {
let env_name = env.name().clone();
let best_platform = env.best_platform();
let task_map = task_names
.into_iter()
.flat_map(|task_name| {
env.task(&task_name, Some(best_platform))
.ok()
.map(|task| (task_name, task.clone()))
})
.collect();
(env_name, task_map)
})
.collect())
}

pub async fn add_task<I: Interface>(
interface: &I,
workspace: &Workspace,
name: TaskName,
task: Task,
feature: FeatureName,
platform: Option<Platform>,
) -> miette::Result<()> {
let mut workspace = workspace.clone().modify()?;

workspace
.manifest()
.add_task(name.clone(), task.clone(), platform, &feature)?;
workspace.save().await.into_diagnostic()?;

interface
.success(&format!(
"Added task `{}`: {}",
name.fancy_display().bold(),
task,
))
.await;

Ok(())
}

pub async fn alias_task<I: Interface>(
interface: &I,
workspace: &Workspace,
name: TaskName,
task: Task,
platform: Option<Platform>,
) -> miette::Result<()> {
let mut workspace = workspace.clone().modify()?;

workspace
.manifest()
.add_task(name.clone(), task.clone(), platform, &FeatureName::DEFAULT)?;
workspace.save().await.into_diagnostic()?;

interface
.success(&format!(
"Added alias `{}`: {}",
name.fancy_display().bold(),
task,
))
.await;

Ok(())
}

pub async fn remove_tasks<I: Interface>(
interface: &I,
workspace: &Workspace,
names: Vec<TaskName>,
platform: Option<Platform>,
feature: FeatureName,
) -> miette::Result<()> {
let mut workspace = workspace.clone().modify()?;
let mut to_remove = Vec::new();

for name in names.iter() {
if let Some(platform) = platform {
if !workspace
.workspace()
.workspace
.value
.tasks(Some(platform), &feature)?
.contains_key(name)
{
interface
.error(&format!(
"Task '{}' does not exist on {}",
name.fancy_display().bold(),
console::style(platform.as_str()).bold(),
))
.await;
continue;
}
} else if !workspace
.workspace()
.workspace
.value
.tasks(None, &feature)?
.contains_key(name)
{
interface
.error(&format!(
"Task `{}` does not exist for the `{}` feature",
name.fancy_display().bold(),
console::style(&feature).bold(),
))
.await;
continue;
}

// Safe to remove
to_remove.push((name, platform));
}

let mut removed = Vec::with_capacity(to_remove.len());
for (name, platform) in to_remove {
workspace
.manifest()
.remove_task(name.clone(), platform, &feature)?;
removed.push(name);
}

workspace.save().await.into_diagnostic()?;

for name in removed {
interface
.success(&format!("Removed task `{}` ", name.fancy_display().bold()))
.await;
}

Ok(())
}
8 changes: 4 additions & 4 deletions crates/pixi_api/src/workspace/workspace/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use pixi_core::Workspace;

use crate::interface::Interface;

pub(crate) async fn get(workspace: Workspace) -> String {
pub async fn get(workspace: &Workspace) -> String {
workspace.display_name().to_string()
}

pub(crate) async fn set<I: Interface>(
pub async fn set<I: Interface>(
interface: &I,
workspace: Workspace,
workspace: &Workspace,
name: &str,
) -> miette::Result<()> {
let mut workspace = workspace.modify()?;
let mut workspace = workspace.clone().modify()?;

// Set the new workspace name
workspace.manifest().set_name(name)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/pixi_cli/src/cli_interface.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use miette::IntoDiagnostic;
use pixi_api::interface::Interface;
use pixi_api::Interface;

#[derive(Default)]
pub struct CliInterface {}
Expand Down
Loading
Loading