diff options
author | Galilée 'Bill' Enguehard <[email protected]> | 2020-05-21 22:27:38 +0100 |
---|---|---|
committer | Galilée 'Bill' Enguehard <[email protected]> | 2020-05-21 22:27:38 +0100 |
commit | 7fece3bdd2450c0807f7dd742239cae95f0cc65e (patch) | |
tree | 866c4db826c959e79c63a6727bdb9f2c61e6fc4f /crates/ra_toolchain/src | |
parent | db926218b2082077750291f8426ddd28b284cd08 (diff) | |
parent | 59732df8d40dfadc6dcf5951265416576399712a (diff) |
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into modname_spacing
Diffstat (limited to 'crates/ra_toolchain/src')
-rw-r--r-- | crates/ra_toolchain/src/lib.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/crates/ra_toolchain/src/lib.rs b/crates/ra_toolchain/src/lib.rs new file mode 100644 index 000000000..3d2865e09 --- /dev/null +++ b/crates/ra_toolchain/src/lib.rs | |||
@@ -0,0 +1,63 @@ | |||
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. | ||
4 | use std::{env, iter, path::PathBuf}; | ||
5 | |||
6 | pub fn cargo() -> PathBuf { | ||
7 | get_path_for_executable("cargo") | ||
8 | } | ||
9 | |||
10 | pub fn rustc() -> PathBuf { | ||
11 | get_path_for_executable("rustc") | ||
12 | } | ||
13 | |||
14 | pub fn rustup() -> PathBuf { | ||
15 | get_path_for_executable("rustup") | ||
16 | } | ||
17 | |||
18 | /// Return a `PathBuf` to use for the given executable. | ||
19 | /// | ||
20 | /// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that | ||
21 | /// gives a valid Cargo executable; or it may return a full path to a valid | ||
22 | /// Cargo. | ||
23 | fn get_path_for_executable(executable_name: &'static str) -> PathBuf { | ||
24 | // The current implementation checks three places for an executable to use: | ||
25 | // 1) Appropriate environment variable (erroring if this is set but not a usable executable) | ||
26 | // example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc | ||
27 | // 2) `<executable_name>` | ||
28 | // example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH | ||
29 | // 3) `~/.cargo/bin/<executable_name>` | ||
30 | // example: for cargo, this tries ~/.cargo/bin/cargo | ||
31 | // It seems that this is a reasonable place to try for cargo, rustc, and rustup | ||
32 | let env_var = executable_name.to_ascii_uppercase(); | ||
33 | if let Some(path) = env::var_os(&env_var) { | ||
34 | return path.into(); | ||
35 | } | ||
36 | |||
37 | if lookup_in_path(executable_name) { | ||
38 | return executable_name.into(); | ||
39 | } | ||
40 | |||
41 | if let Some(mut path) = home::home_dir() { | ||
42 | path.push(".cargo"); | ||
43 | path.push("bin"); | ||
44 | path.push(executable_name); | ||
45 | if path.is_file() { | ||
46 | return path; | ||
47 | } | ||
48 | } | ||
49 | executable_name.into() | ||
50 | } | ||
51 | |||
52 | fn lookup_in_path(exec: &str) -> bool { | ||
53 | let paths = env::var_os("PATH").unwrap_or_default(); | ||
54 | let mut candidates = env::split_paths(&paths).flat_map(|path| { | ||
55 | let candidate = path.join(&exec); | ||
56 | let with_exe = match env::consts::EXE_EXTENSION { | ||
57 | "" => None, | ||
58 | it => Some(candidate.with_extension(it)), | ||
59 | }; | ||
60 | iter::once(candidate).chain(with_exe) | ||
61 | }); | ||
62 | candidates.any(|it| it.is_file()) | ||
63 | } | ||