Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 30 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ barrier_cell = { path = "crates/barrier_cell" }
fancy_display = { path = "crates/fancy_display" }
pixi = { path = "crates/pixi" }
pixi_allocator = { path = "crates/pixi_allocator" }
pixi_api = { path = "crates/pixi_api" }
pixi_build_discovery = { path = "crates/pixi_build_discovery" }
pixi_build_frontend = { path = "crates/pixi_build_frontend" }
pixi_build_type_conversions = { path = "crates/pixi_build_type_conversions" }
Expand Down
30 changes: 30 additions & 0 deletions crates/pixi_api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
authors.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
name = "pixi_api"
readme.workspace = true
repository.workspace = true
version = "0.1.0"

[dependencies]
dunce = { workspace = true }
fancy_display = { workspace = true }
fs-err = { workspace = true }
itertools = { workspace = true }
miette = { workspace = true }
minijinja = { workspace = true }
pixi_config = { workspace = true }
pixi_consts = { workspace = true }
pixi_core = { workspace = true }
pixi_manifest = { workspace = true }
pixi_utils = { workspace = true }
rattler_conda_types = { workspace = true }
same-file = { workspace = true }
serde = { workspace = true, features = ["derive"] }
tempfile = { workspace = true }
tokio = { workspace = true, features = ["fs"] }
tracing = { workspace = true }
url = { workspace = true }
uv-normalize = { workspace = true }
44 changes: 44 additions & 0 deletions crates/pixi_api/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use pixi_core::{Workspace, environment::LockFileUsage};

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

pub struct WorkspaceContext<I: Interface> {
interface: I,
workspace: Workspace,
}

impl<I: Interface> WorkspaceContext<I> {
pub fn new(interface: I, workspace: Workspace) -> Self {
Self {
interface,
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::config::name::get(self.workspace.clone()).await
}

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

pub async fn reinstall(
&self,
options: ReinstallOptions,
lock_file_usage: LockFileUsage,
) -> miette::Result<()> {
crate::workspace::reinstall::reinstall(
&self.interface,
options,
self.workspace.clone(),
lock_file_usage,
)
.await
}
}
14 changes: 14 additions & 0 deletions crates/pixi_api/src/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use miette::Result;
use std::future::Future;

use crate::styled_text::StyledText;

pub trait Interface {
fn styled(&self, text: StyledText) -> String;
fn is_cli(&self) -> impl Future<Output = bool> + Send;
fn confirm(&self, msg: &str) -> impl Future<Output = Result<bool>> + Send;
fn info(&self, msg: &str) -> impl Future<Output = ()> + Send;
fn success(&self, msg: &str) -> impl Future<Output = ()> + Send;
fn warning(&self, msg: &str) -> impl Future<Output = ()> + Send;
fn error(&self, msg: &str) -> impl Future<Output = ()> + Send;
}
8 changes: 8 additions & 0 deletions crates/pixi_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod workspace;

pub mod context;
pub mod interface;
pub mod styled_text;

// Reexport for pixi_api consumers
pub use pixi_core as core;
39 changes: 39 additions & 0 deletions crates/pixi_api/src/styled_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pub struct StyledText {
pub text: String,
pub bold: bool,
pub green: bool,
}

impl StyledText {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
bold: false,
green: false,
}
}

pub fn bold(mut self) -> Self {
self.bold = true;
self
}

pub fn green(mut self) -> Self {
self.green = true;
self
}

pub fn text(&self) -> &str {
&self.text
}
}

pub trait StyleExt {
fn style(self) -> StyledText;
}

impl<T: Into<String>> StyleExt for T {
fn style(self) -> StyledText {
StyledText::new(self)
}
}
1 change: 1 addition & 0 deletions crates/pixi_api/src/workspace/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod name;
32 changes: 32 additions & 0 deletions crates/pixi_api/src/workspace/config/name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use miette::IntoDiagnostic;
use pixi_core::Workspace;

use crate::interface::Interface;

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

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

// Set the new workspace name
workspace.manifest().set_name(name)?;

// Save workspace
let workspace = workspace.save().await.into_diagnostic()?;

// Report back to the user
interface
.success(&format!(
"Updated workspace name to '{}'.",
workspace.display_name()
))
.await;

Ok(())
}
Loading
Loading