aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_toolchain
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_toolchain')
-rw-r--r--crates/ra_toolchain/Cargo.toml4
-rw-r--r--crates/ra_toolchain/src/lib.rs25
2 files changed, 19 insertions, 10 deletions
diff --git a/crates/ra_toolchain/Cargo.toml b/crates/ra_toolchain/Cargo.toml
index 1873fbe16..84b748c0a 100644
--- a/crates/ra_toolchain/Cargo.toml
+++ b/crates/ra_toolchain/Cargo.toml
@@ -3,6 +3,10 @@ edition = "2018"
3name = "ra_toolchain" 3name = "ra_toolchain"
4version = "0.1.0" 4version = "0.1.0"
5authors = ["rust-analyzer developers"] 5authors = ["rust-analyzer developers"]
6license = "MIT OR Apache-2.0"
7
8[lib]
9doctest = false
6 10
7[dependencies] 11[dependencies]
8home = "0.5.3" 12home = "0.5.3"
diff --git a/crates/ra_toolchain/src/lib.rs b/crates/ra_toolchain/src/lib.rs
index 3d2865e09..9916e52c4 100644
--- a/crates/ra_toolchain/src/lib.rs
+++ b/crates/ra_toolchain/src/lib.rs
@@ -15,6 +15,10 @@ pub fn rustup() -> PathBuf {
15 get_path_for_executable("rustup") 15 get_path_for_executable("rustup")
16} 16}
17 17
18pub fn rustfmt() -> PathBuf {
19 get_path_for_executable("rustfmt")
20}
21
18/// Return a `PathBuf` to use for the given executable. 22/// Return a `PathBuf` to use for the given executable.
19/// 23///
20/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that 24/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
@@ -42,22 +46,23 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
42 path.push(".cargo"); 46 path.push(".cargo");
43 path.push("bin"); 47 path.push("bin");
44 path.push(executable_name); 48 path.push(executable_name);
45 if path.is_file() { 49 if let Some(path) = probe(path) {
46 return path; 50 return path;
47 } 51 }
48 } 52 }
53
49 executable_name.into() 54 executable_name.into()
50} 55}
51 56
52fn lookup_in_path(exec: &str) -> bool { 57fn lookup_in_path(exec: &str) -> bool {
53 let paths = env::var_os("PATH").unwrap_or_default(); 58 let paths = env::var_os("PATH").unwrap_or_default();
54 let mut candidates = env::split_paths(&paths).flat_map(|path| { 59 env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some()
55 let candidate = path.join(&exec); 60}
56 let with_exe = match env::consts::EXE_EXTENSION { 61
57 "" => None, 62fn probe(path: PathBuf) -> Option<PathBuf> {
58 it => Some(candidate.with_extension(it)), 63 let with_extension = match env::consts::EXE_EXTENSION {
59 }; 64 "" => None,
60 iter::once(candidate).chain(with_exe) 65 it => Some(path.with_extension(it)),
61 }); 66 };
62 candidates.any(|it| it.is_file()) 67 iter::once(path).chain(with_extension).find(|it| it.is_file())
63} 68}