diff options
author | Kirill Bulatov <[email protected]> | 2020-02-17 22:38:01 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-02-17 22:38:01 +0000 |
commit | e29dbdb139fb3a35902b4b3d11a92d45861d210b (patch) | |
tree | 1f011539525108a10f36fdcde151d294c78db1c0 /crates/ra_project_model/src | |
parent | 67d30312d0741d3bcc45d794dc8c333f1a7d9ed8 (diff) |
Simplify the command execution
Diffstat (limited to 'crates/ra_project_model/src')
-rw-r--r-- | crates/ra_project_model/src/sysroot.rs | 62 |
1 files changed, 28 insertions, 34 deletions
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index b426fc107..db779a2d2 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs | |||
@@ -4,7 +4,7 @@ use anyhow::{bail, Context, Result}; | |||
4 | use std::{ | 4 | use std::{ |
5 | env, | 5 | env, |
6 | path::{Path, PathBuf}, | 6 | path::{Path, PathBuf}, |
7 | process::Command, | 7 | process::{Command, Output}, |
8 | }; | 8 | }; |
9 | 9 | ||
10 | use ra_arena::{impl_arena_id, Arena, RawId}; | 10 | use ra_arena::{impl_arena_id, Arena, RawId}; |
@@ -79,49 +79,43 @@ impl Sysroot { | |||
79 | } | 79 | } |
80 | } | 80 | } |
81 | 81 | ||
82 | fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> { | 82 | fn create_command_text(program: &str, args: &[&str]) -> String { |
83 | fn try_install_rust_src(cargo_toml: &Path) -> Result<()> { | 83 | format!("{} {}", program, args.join(" ")) |
84 | let rustup_output = Command::new("rustup") | 84 | } |
85 | .current_dir(cargo_toml.parent().unwrap()) | ||
86 | .args(&["component", "add", "rust-src"]) | ||
87 | .output() | ||
88 | .context("rustup component add rust-src failed")?; | ||
89 | if !rustup_output.status.success() { | ||
90 | match rustup_output.status.code() { | ||
91 | Some(code) => bail!( | ||
92 | "failed to install rust-src: rustup component add rust-src exited with code {}", | ||
93 | code | ||
94 | ), | ||
95 | None => bail!( | ||
96 | "failed to install rust-src: rustup component add rust-src terminated by signal" | ||
97 | ), | ||
98 | }; | ||
99 | } | ||
100 | Ok(()) | ||
101 | } | ||
102 | 85 | ||
103 | if let Ok(path) = env::var("RUST_SRC_PATH") { | 86 | fn run_command_in_cargo_dir(cargo_toml: &Path, program: &str, args: &[&str]) -> Result<Output> { |
104 | return Ok(path.into()); | 87 | let output = Command::new(program) |
105 | } | ||
106 | let rustc_output = Command::new("rustc") | ||
107 | .current_dir(cargo_toml.parent().unwrap()) | 88 | .current_dir(cargo_toml.parent().unwrap()) |
108 | .args(&["--print", "sysroot"]) | 89 | .args(args) |
109 | .output() | 90 | .output() |
110 | .context("rustc --print sysroot failed")?; | 91 | .context(format!("{} failed", create_command_text(program, args)))?; |
111 | if !rustc_output.status.success() { | 92 | if !output.status.success() { |
112 | match rustc_output.status.code() { | 93 | match output.status.code() { |
113 | Some(code) => { | 94 | Some(code) => bail!( |
114 | bail!("failed to locate sysroot: rustc --print sysroot exited with code {}", code) | 95 | "failed to run the command: '{}' exited with code {}", |
115 | } | 96 | create_command_text(program, args), |
116 | None => bail!("failed to locate sysroot: rustc --print sysroot terminated by signal"), | 97 | code |
98 | ), | ||
99 | None => bail!( | ||
100 | "failed to run the command: '{}' terminated by signal", | ||
101 | create_command_text(program, args) | ||
102 | ), | ||
117 | }; | 103 | }; |
118 | } | 104 | } |
105 | Ok(output) | ||
106 | } | ||
107 | |||
108 | fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> { | ||
109 | if let Ok(path) = env::var("RUST_SRC_PATH") { | ||
110 | return Ok(path.into()); | ||
111 | } | ||
112 | let rustc_output = run_command_in_cargo_dir(cargo_toml, "rustc", &["--print", "sysroot"])?; | ||
119 | let stdout = String::from_utf8(rustc_output.stdout)?; | 113 | let stdout = String::from_utf8(rustc_output.stdout)?; |
120 | let sysroot_path = Path::new(stdout.trim()); | 114 | let sysroot_path = Path::new(stdout.trim()); |
121 | let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); | 115 | let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); |
122 | 116 | ||
123 | if !src_path.exists() { | 117 | if !src_path.exists() { |
124 | try_install_rust_src(cargo_toml)? | 118 | run_command_in_cargo_dir(cargo_toml, "rustup", &["component", "add", "rust-src"])?; |
125 | } | 119 | } |
126 | if !src_path.exists() { | 120 | if !src_path.exists() { |
127 | bail!( | 121 | bail!( |