|
| 1 | +use std::{path::PathBuf, process::Stdio}; |
| 2 | + |
| 3 | +use clap::Parser; |
| 4 | +use miette::{IntoDiagnostic, Report}; |
| 5 | +use pixi_config::Config; |
| 6 | +use rattler_conda_types::Platform; |
| 7 | +use rattler_shell::shell::ShellEnum; |
| 8 | + |
| 9 | +use crate::{registry::Registry, EnvironmentName}; |
| 10 | + |
| 11 | +/// Run an executable in a conda environment. |
| 12 | +#[derive(Parser, Debug)] |
| 13 | +#[clap(trailing_var_arg = true, disable_help_flag = true)] |
| 14 | +pub struct Args { |
| 15 | + #[clap(num_args = 1.., allow_hyphen_values = true, required = true)] |
| 16 | + args: Vec<String>, |
| 17 | + |
| 18 | + /// Print help |
| 19 | + #[clap(long, short, action = clap::ArgAction::Help)] |
| 20 | + help: Option<bool>, |
| 21 | + |
| 22 | + /// Name of environment. |
| 23 | + #[clap( |
| 24 | + long, |
| 25 | + short, |
| 26 | + help_heading = "Target Environment Specification", |
| 27 | + conflicts_with = "prefix", |
| 28 | + required = true |
| 29 | + )] |
| 30 | + name: Option<EnvironmentName>, |
| 31 | + |
| 32 | + /// Path to environment location (i.e. prefix). |
| 33 | + #[clap(long, short, help_heading = "Target Environment Specification")] |
| 34 | + prefix: Option<PathBuf>, |
| 35 | +} |
| 36 | + |
| 37 | +pub async fn execute(_config: Config, mut args: Args) -> miette::Result<()> { |
| 38 | + // Determine the prefix to use |
| 39 | + let prefix = if let Some(name) = &args.name { |
| 40 | + &Registry::from_env().root().join(name.as_ref()) |
| 41 | + } else if let Some(prefix) = &args.prefix { |
| 42 | + prefix |
| 43 | + } else { |
| 44 | + unreachable!("Either a name or a prefix must be provided") |
| 45 | + }; |
| 46 | + |
| 47 | + // Make sure it exists |
| 48 | + if !prefix.is_dir() || !prefix.join("conda-meta").is_dir() { |
| 49 | + let prefix_or_name = if let Some(name) = &args.name { |
| 50 | + format!("--name {name}") |
| 51 | + } else if let Some(prefix) = &args.prefix { |
| 52 | + format!("--prefix {}", prefix.display()) |
| 53 | + } else { |
| 54 | + unreachable!("Either a name or a prefix must be provided") |
| 55 | + }; |
| 56 | + miette::bail!( |
| 57 | + help = format!( |
| 58 | + "You can create an environment with:\n\n\tpixi-conda create {prefix_or_name} ..." |
| 59 | + ), |
| 60 | + "The environment at '{}' does not appear to be a valid conda environment", |
| 61 | + prefix.display() |
| 62 | + ); |
| 63 | + }; |
| 64 | + |
| 65 | + // Collect environment variables for the prefix. |
| 66 | + let activation_variables = rattler_shell::activation::Activator::from_path( |
| 67 | + prefix, |
| 68 | + ShellEnum::default(), |
| 69 | + Platform::current(), |
| 70 | + ) |
| 71 | + .into_diagnostic()? |
| 72 | + .run_activation( |
| 73 | + rattler_shell::activation::ActivationVariables::from_env().into_diagnostic()?, |
| 74 | + None, |
| 75 | + ) |
| 76 | + .into_diagnostic()?; |
| 77 | + |
| 78 | + // Spawn the process |
| 79 | + let executable = args.args.remove(0); |
| 80 | + let mut command = std::process::Command::new(&executable); |
| 81 | + |
| 82 | + // Set the environment variables |
| 83 | + command.envs(activation_variables); |
| 84 | + |
| 85 | + // Add the arguments |
| 86 | + command.args(args.args); |
| 87 | + |
| 88 | + // Inherit stdin, stdout, and stderr |
| 89 | + command |
| 90 | + .stdin(Stdio::inherit()) |
| 91 | + .stdout(Stdio::inherit()) |
| 92 | + .stderr(Stdio::inherit()); |
| 93 | + |
| 94 | + // Spawn the child process |
| 95 | + #[cfg(target_family = "unix")] |
| 96 | + command.exec(); |
| 97 | + |
| 98 | + #[cfg(target_os = "windows")] |
| 99 | + { |
| 100 | + let mut child = match command.spawn() { |
| 101 | + Ok(child) => child, |
| 102 | + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { |
| 103 | + miette::bail!("The executable '{}' could not be found", executable); |
| 104 | + } |
| 105 | + Err(e) => return Err(Report::from_err(e)), |
| 106 | + }; |
| 107 | + |
| 108 | + // Wait for the child process to complete |
| 109 | + let status = child.wait().into_diagnostic()?; |
| 110 | + |
| 111 | + // Exit with the same status code as the child process |
| 112 | + std::process::exit(status.code().unwrap_or(1)); |
| 113 | + } |
| 114 | +} |
0 commit comments