Skip to content

Commit

Permalink
feat: extend info and list with more global info
Browse files Browse the repository at this point in the history
  • Loading branch information
ruben-arts committed Oct 9, 2024
1 parent 0d17051 commit fbb5348
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 11 deletions.
10 changes: 2 additions & 8 deletions src/cli/global/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use rattler_conda_types::{PackageName, PackageRecord, PrefixRecord, Version};
use serde::Serialize;
use std::io::{stdout, Write};
use std::str::FromStr;
use thiserror::__private::AsDisplay;

/// Lists all packages previously installed into a globally accessible location via `pixi global install`.
///
Expand Down Expand Up @@ -299,13 +298,8 @@ async fn list_global_environments(project: Project) -> miette::Result<()> {
println!("No global environments found.");
} else {
println!(
"Global environments at {}:\n{}",
project
.env_root
.path()
.parent()
.unwrap_or(project.env_root.path())
.as_display(),
"Global environments as specified in '{}'\n{}",
console::style(project.manifest.path.display()).bold(),
message
);
}
Expand Down
62 changes: 59 additions & 3 deletions src/cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tokio::task::spawn_blocking;

use crate::cli::cli_config::ProjectConfig;

use crate::{task::TaskName, Project};
use crate::{global, task::TaskName, Project};
use fancy_display::FancyDisplay;

static WIDTH: usize = 18;
Expand Down Expand Up @@ -158,6 +158,38 @@ impl Display for EnvironmentInfo {
}
}

/// Information about `pixi global`
#[derive(Serialize)]
struct GlobalInfo {
bin_dir: PathBuf,
env_dir: PathBuf,
manifest: PathBuf,
}
impl Display for GlobalInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let bold = console::Style::new().bold();
writeln!(
f,
"{:>WIDTH$}: {}",
bold.apply_to("Bin dir"),
self.bin_dir.to_string_lossy()
)?;
writeln!(
f,
"{:>WIDTH$}: {}",
bold.apply_to("Environment dir"),
self.env_dir.to_string_lossy()
)?;
writeln!(
f,
"{:>WIDTH$}: {}",
bold.apply_to("Manifest dir"),
self.manifest.to_string_lossy()
)?;
Ok(())
}
}

#[serde_as]
#[derive(Serialize)]
pub struct Info {
Expand All @@ -168,6 +200,7 @@ pub struct Info {
cache_dir: Option<PathBuf>,
cache_size: Option<String>,
auth_dir: PathBuf,
global_info: Option<GlobalInfo>,
project_info: Option<ProjectInfo>,
environments_info: Vec<EnvironmentInfo>,
config_locations: Vec<PathBuf>,
Expand All @@ -180,6 +213,7 @@ impl Display for Info {
None => "None".to_string(),
};

writeln!(f, "{}", bold.apply_to("System\n------------").cyan())?;
writeln!(
f,
"{:>WIDTH$}: {}",
Expand Down Expand Up @@ -230,8 +264,15 @@ impl Display for Info {
}
)?;

// Pixi global information
if let Some(gi) = self.global_info.as_ref() {
writeln!(f, "\n{}", bold.apply_to("Global\n------------").cyan())?;
write!(f, "{}", gi)?;
}

// Project information
if let Some(pi) = self.project_info.as_ref() {
writeln!(f, "\n{}", bold.apply_to("Project\n------------"))?;
writeln!(f, "\n{}", bold.apply_to("Project\n------------").cyan())?;
writeln!(f, "{:>WIDTH$}: {}", bold.apply_to("Name"), pi.name)?;
if let Some(version) = pi.version.clone() {
writeln!(f, "{:>WIDTH$}: {}", bold.apply_to("Version"), version)?;
Expand All @@ -254,7 +295,11 @@ impl Display for Info {
}

if !self.environments_info.is_empty() {
writeln!(f, "\n{}", bold.apply_to("Environments\n------------"))?;
writeln!(
f,
"\n{}",
bold.apply_to("Environments\n------------").cyan()
)?;
for e in &self.environments_info {
writeln!(f, "{}", e)?;
}
Expand Down Expand Up @@ -360,6 +405,16 @@ pub async fn execute(args: Args) -> miette::Result<()> {
})
.unwrap_or_default();

let global_info = if let Ok(global_project) = global::Project::discover().await {
Some(GlobalInfo {
bin_dir: global_project.bin_dir.path().to_path_buf(),
env_dir: global_project.env_root.path().to_path_buf(),
manifest: global_project.manifest.path,
})
} else {
None
};

let virtual_packages = VirtualPackage::detect(&VirtualPackageOverrides::from_env())
.into_diagnostic()?
.iter()
Expand Down Expand Up @@ -389,6 +444,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
auth_dir: auth_file,
project_info,
environments_info,
global_info,
config_locations: config.loaded_from.clone(),
};

Expand Down
17 changes: 17 additions & 0 deletions src/global/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,23 @@ impl Project {
Self::from_path(&manifest_path, env_root, bin_dir)
}

/// Discovers the project manifest file in path at
/// `~/.pixi/manifests/pixi-global.toml`. If the manifest doesn't exist
/// yet, it will return an error.
pub(crate) async fn discover() -> miette::Result<Self> {
let manifest_dir = Self::manifest_dir()?;
let manifest_path = manifest_dir.join(MANIFEST_DEFAULT_NAME);

let bin_dir = BinDir::from_env().await?;
let env_root = EnvRoot::from_env().await?;

if !manifest_path.exists() {
miette::bail!("Global manifest not found at {}", manifest_path.display());
}

Self::from_path(&manifest_path, env_root, bin_dir)
}

async fn try_from_existing_installation(
manifest_path: &Path,
env_root: EnvRoot,
Expand Down

0 comments on commit fbb5348

Please sign in to comment.