Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
68 changes: 67 additions & 1 deletion 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,6 +20,10 @@ impl<I: Interface> WorkspaceContext<I> {
}
}

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

pub async fn init(interface: I, options: InitOptions) -> miette::Result<Workspace> {
crate::workspace::init::init(&interface, options).await
}
Expand All @@ -28,6 +36,64 @@ impl<I: Interface> WorkspaceContext<I> {
crate::workspace::workspace::name::set(&self.interface, self.workspace.clone(), 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.clone(), 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.clone(),
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.clone(),
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.clone(),
names,
platform,
feature,
)
.await
}

pub async fn reinstall(
&self,
options: ReinstallOptions,
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;
2 changes: 1 addition & 1 deletion crates/pixi_api/src/workspace/reinstall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod options;

pub use options::ReinstallOptions;

pub(crate) async fn reinstall<I: Interface>(
pub async fn reinstall<I: Interface>(
interface: &I,
options: ReinstallOptions,
workspace: Workspace,
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.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.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.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(())
}
4 changes: 2 additions & 2 deletions crates/pixi_api/src/workspace/workspace/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ 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,
name: &str,
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
23 changes: 12 additions & 11 deletions crates/pixi_cli/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cmp::PartialEq, path::PathBuf};

use clap::{Parser, ValueEnum};
use pixi_api::{context::WorkspaceContext, interface::Interface, workspace::init::InitOptions};
use pixi_api::{WorkspaceContext, workspace::InitOptions};
use rattler_conda_types::NamedChannelOrUrl;

use crate::cli_interface::CliInterface;
Expand Down Expand Up @@ -68,15 +68,15 @@ pub enum GitAttributes {
impl From<Args> for InitOptions {
fn from(args: Args) -> Self {
let format = args.format.map(|f| match f {
ManifestFormat::Pixi => pixi_api::workspace::init::ManifestFormat::Pixi,
ManifestFormat::Pyproject => pixi_api::workspace::init::ManifestFormat::Pyproject,
ManifestFormat::Mojoproject => pixi_api::workspace::init::ManifestFormat::Mojoproject,
ManifestFormat::Pixi => pixi_api::workspace::ManifestFormat::Pixi,
ManifestFormat::Pyproject => pixi_api::workspace::ManifestFormat::Pyproject,
ManifestFormat::Mojoproject => pixi_api::workspace::ManifestFormat::Mojoproject,
});

let scm = args.scm.map(|s| match s {
GitAttributes::Github => pixi_api::workspace::init::GitAttributes::Github,
GitAttributes::Gitlab => pixi_api::workspace::init::GitAttributes::Gitlab,
GitAttributes::Codeberg => pixi_api::workspace::init::GitAttributes::Codeberg,
GitAttributes::Github => pixi_api::workspace::GitAttributes::Github,
GitAttributes::Gitlab => pixi_api::workspace::GitAttributes::Gitlab,
GitAttributes::Codeberg => pixi_api::workspace::GitAttributes::Codeberg,
});

InitOptions {
Expand All @@ -96,12 +96,13 @@ pub async fn execute(args: Args) -> miette::Result<()> {

// Deprecation warning for the `pyproject` option
if uses_deprecated_pyproject_flag {
CliInterface::default().warning(&format!(
"The '{}' option is deprecated and will be removed in the future.\nUse '{}' instead.",
eprintln!(
"{}The '{}' option is deprecated and will be removed in the future.\nUse '{}' instead.",
console::style(console::Emoji("⚠️ ", "")).yellow(),
console::style("--pyproject").bold().red(),
console::style("--format pyproject").bold().green(),
)).await;
options.format = Some(pixi_api::workspace::init::ManifestFormat::Pyproject);
);
options.format = Some(pixi_api::workspace::ManifestFormat::Pyproject);
}

WorkspaceContext::init(CliInterface {}, options).await?;
Expand Down
Loading
Loading