aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_toolchain/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_toolchain/src/lib.rs')
-rw-r--r--crates/ra_toolchain/src/lib.rs68
1 files changed, 0 insertions, 68 deletions
diff --git a/crates/ra_toolchain/src/lib.rs b/crates/ra_toolchain/src/lib.rs
deleted file mode 100644
index 9916e52c4..000000000
--- a/crates/ra_toolchain/src/lib.rs
+++ /dev/null
@@ -1,68 +0,0 @@
1//! This crate contains a single public function
2//! [`get_path_for_executable`](fn.get_path_for_executable.html).
3//! See docs there for more information.
4use std::{env, iter, path::PathBuf};
5
6pub fn cargo() -> PathBuf {
7 get_path_for_executable("cargo")
8}
9
10pub fn rustc() -> PathBuf {
11 get_path_for_executable("rustc")
12}
13
14pub fn rustup() -> PathBuf {
15 get_path_for_executable("rustup")
16}
17
18pub fn rustfmt() -> PathBuf {
19 get_path_for_executable("rustfmt")
20}
21
22/// Return a `PathBuf` to use for the given executable.
23///
24/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
25/// gives a valid Cargo executable; or it may return a full path to a valid
26/// Cargo.
27fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
28 // The current implementation checks three places for an executable to use:
29 // 1) Appropriate environment variable (erroring if this is set but not a usable executable)
30 // example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
31 // 2) `<executable_name>`
32 // example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH
33 // 3) `~/.cargo/bin/<executable_name>`
34 // example: for cargo, this tries ~/.cargo/bin/cargo
35 // It seems that this is a reasonable place to try for cargo, rustc, and rustup
36 let env_var = executable_name.to_ascii_uppercase();
37 if let Some(path) = env::var_os(&env_var) {
38 return path.into();
39 }
40
41 if lookup_in_path(executable_name) {
42 return executable_name.into();
43 }
44
45 if let Some(mut path) = home::home_dir() {
46 path.push(".cargo");
47 path.push("bin");
48 path.push(executable_name);
49 if let Some(path) = probe(path) {
50 return path;
51 }
52 }
53
54 executable_name.into()
55}
56
57fn lookup_in_path(exec: &str) -> bool {
58 let paths = env::var_os("PATH").unwrap_or_default();
59 env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some()
60}
61
62fn probe(path: PathBuf) -> Option<PathBuf> {
63 let with_extension = match env::consts::EXE_EXTENSION {
64 "" => None,
65 it => Some(path.with_extension(it)),
66 };
67 iter::once(path).chain(with_extension).find(|it| it.is_file())
68}