Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add shell-hook command #672

Merged
merged 8 commits into from
Jan 17, 2024
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
4 changes: 4 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod remove;
pub mod run;
pub mod search;
pub mod shell;
pub mod shell_hook;
pub mod task;
pub mod upload;

Expand Down Expand Up @@ -58,6 +59,8 @@ pub enum Command {
Run(run::Args),
#[clap(alias = "s")]
Shell(shell::Args),
#[clap(hide = true)]
ShellHook(shell_hook::Args),
#[clap(alias = "g")]
Global(global::Args),
Auth(auth::Args),
Expand Down Expand Up @@ -161,6 +164,7 @@ pub async fn execute_command(command: Command) -> miette::Result<()> {
Command::Auth(cmd) => auth::execute(cmd).await,
Command::Install(cmd) => install::execute(cmd).await,
Command::Shell(cmd) => shell::execute(cmd).await,
Command::ShellHook(cmd) => shell_hook::execute(cmd),
Command::Task(cmd) => task::execute(cmd),
Command::Info(cmd) => info::execute(cmd).await,
Command::Upload(cmd) => upload::execute(cmd).await,
Expand Down
66 changes: 66 additions & 0 deletions src/cli/shell_hook.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::path::PathBuf;

use clap::Parser;
use miette::IntoDiagnostic;
use rattler_conda_types::Platform;
use rattler_shell::{
activation::{ActivationVariables, Activator},
shell::ShellEnum,
};

use crate::prefix::Prefix;

/// Print the activation script
#[derive(Parser, Debug)]
pub struct Args {
/// Sets the shell
#[arg(short, long)]
shell: Option<ShellEnum>,
}

/// Generates the activation script.
fn generate_activation_script(shell: Option<ShellEnum>) -> miette::Result<String> {
let prefix = Prefix::new(PathBuf::new())?;
let shell = shell.unwrap_or_default();
let platform = Platform::current();
let activator = Activator::from_path(prefix.root(), shell, platform).into_diagnostic()?;
let path = std::env::var("PATH")
.ok()
.map(|p| std::env::split_paths(&p).collect::<Vec<_>>());

// If we are in a conda environment, we need to deactivate it before activating the host / build prefix
let conda_prefix = std::env::var("CONDA_PREFIX").ok().map(|p| p.into());
let result = activator
.activation(ActivationVariables {
conda_prefix,
path,
path_modification_behavior: Default::default(),
})
.into_diagnostic()?;

Ok(result.script.clone())
}

/// Prints the activation script to the stdout.
pub fn execute(args: Args) -> miette::Result<()> {
let script = generate_activation_script(args.shell)?;
println!("{script}");
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
pub fn test_shell_hook() {
let script = generate_activation_script(None).unwrap();
if cfg!(unix) {
assert!(script.contains("export PATH="));
assert!(script.contains("export CONDA_PREFIX="));
} else {
assert!(script.contains("@SET \"PATH="));
assert!(script.contains("@SET \"CONDA_PREFIX="));
}
}
}
Loading