|
|
@ -8,19 +8,24 @@ use std::path::Path; |
|
|
|
pub struct PowerShell; |
|
|
|
pub struct PowerShell; |
|
|
|
|
|
|
|
|
|
|
|
impl Shell for PowerShell { |
|
|
|
impl Shell for PowerShell { |
|
|
|
fn path(&self, path: &Path) -> String { |
|
|
|
fn path(&self, path: &Path) -> anyhow::Result<String> { |
|
|
|
let current_path = std::env::var_os("PATH").expect("Can't read PATH env var"); |
|
|
|
let current_path = |
|
|
|
|
|
|
|
std::env::var_os("PATH").ok_or_else(|| anyhow::anyhow!("Can't read PATH env var"))?; |
|
|
|
let mut split_paths: Vec<_> = std::env::split_paths(¤t_path).collect(); |
|
|
|
let mut split_paths: Vec<_> = std::env::split_paths(¤t_path).collect(); |
|
|
|
split_paths.insert(0, path.to_path_buf()); |
|
|
|
split_paths.insert(0, path.to_path_buf()); |
|
|
|
let new_path = std::env::join_paths(split_paths).expect("Can't join paths"); |
|
|
|
let new_path = std::env::join_paths(split_paths) |
|
|
|
self.set_env_var("PATH", new_path.to_str().expect("Can't read PATH")) |
|
|
|
.map_err(|source| anyhow::anyhow!("Can't join paths: {}", source))?; |
|
|
|
|
|
|
|
let new_path = new_path |
|
|
|
|
|
|
|
.to_str() |
|
|
|
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("Can't read PATH"))?; |
|
|
|
|
|
|
|
Ok(self.set_env_var("PATH", new_path)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn set_env_var(&self, name: &str, value: &str) -> String { |
|
|
|
fn set_env_var(&self, name: &str, value: &str) -> String { |
|
|
|
format!(r#"$env:{} = "{}""#, name, value) |
|
|
|
format!(r#"$env:{} = "{}""#, name, value) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn use_on_cd(&self, config: &crate::config::FnmConfig) -> String { |
|
|
|
fn use_on_cd(&self, config: &crate::config::FnmConfig) -> anyhow::Result<String> { |
|
|
|
let autoload_hook = match config.version_file_strategy() { |
|
|
|
let autoload_hook = match config.version_file_strategy() { |
|
|
|
VersionFileStrategy::Local => indoc!( |
|
|
|
VersionFileStrategy::Local => indoc!( |
|
|
|
r#" |
|
|
|
r#" |
|
|
@ -29,7 +34,7 @@ impl Shell for PowerShell { |
|
|
|
), |
|
|
|
), |
|
|
|
VersionFileStrategy::Recursive => r#"fnm use --silent-if-unchanged"#, |
|
|
|
VersionFileStrategy::Recursive => r#"fnm use --silent-if-unchanged"#, |
|
|
|
}; |
|
|
|
}; |
|
|
|
formatdoc!( |
|
|
|
Ok(formatdoc!( |
|
|
|
r#" |
|
|
|
r#" |
|
|
|
function Set-FnmOnLoad {{ {autoload_hook} }} |
|
|
|
function Set-FnmOnLoad {{ {autoload_hook} }} |
|
|
|
function Set-LocationWithFnm {{ param($path); Set-Location $path; Set-FnmOnLoad }} |
|
|
|
function Set-LocationWithFnm {{ param($path); Set-Location $path; Set-FnmOnLoad }} |
|
|
@ -39,7 +44,7 @@ impl Shell for PowerShell { |
|
|
|
Set-FnmOnLoad |
|
|
|
Set-FnmOnLoad |
|
|
|
"#, |
|
|
|
"#, |
|
|
|
autoload_hook = autoload_hook |
|
|
|
autoload_hook = autoload_hook |
|
|
|
) |
|
|
|
)) |
|
|
|
} |
|
|
|
} |
|
|
|
fn to_clap_shell(&self) -> clap_complete::Shell { |
|
|
|
fn to_clap_shell(&self) -> clap_complete::Shell { |
|
|
|
clap_complete::Shell::PowerShell |
|
|
|
clap_complete::Shell::PowerShell |
|
|
|