forked from astral-sh/rye
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically add to PATH on UNIX shells (astral-sh#580)
- Loading branch information
Showing
6 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::path::{Path, PathBuf}; | ||
use std::{env, fs}; | ||
|
||
use anyhow::{Context, Error}; | ||
|
||
pub(crate) fn add_to_path(rye_home: &Path) -> Result<(), Error> { | ||
// for regular shells just add the path to `.profile` | ||
add_source_line_to_profile( | ||
&home::home_dir() | ||
.context("could not find home dir")? | ||
.join(".profile"), | ||
&(format!( | ||
". \"{}\"", | ||
reverse_resolve_env_home(rye_home.join("env")).display() | ||
)), | ||
)?; | ||
Ok(()) | ||
} | ||
|
||
fn add_source_line_to_profile(profile_path: &Path, source_line: &str) -> Result<(), Error> { | ||
let mut profile = if profile_path.is_file() { | ||
fs::read_to_string(profile_path)? | ||
} else { | ||
String::new() | ||
}; | ||
|
||
if !profile.lines().any(|x| x.trim() == source_line) { | ||
profile.push_str(source_line); | ||
profile.push('\n'); | ||
fs::write(profile_path, profile).context("failed to write updated .profile")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn reverse_resolve_env_home(path: PathBuf) -> PathBuf { | ||
if let Some(env_home) = env::var_os("HOME").map(PathBuf::from) { | ||
if let Ok(rest) = path.strip_prefix(&env_home) { | ||
return Path::new("$HOME").join(rest); | ||
} | ||
} | ||
path | ||
} |