mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Simplify the command execution
This commit is contained in:
parent
67d30312d0
commit
e29dbdb139
1 changed files with 29 additions and 35 deletions
|
@ -4,7 +4,7 @@ use anyhow::{bail, Context, Result};
|
||||||
use std::{
|
use std::{
|
||||||
env,
|
env,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::Command,
|
process::{Command, Output},
|
||||||
};
|
};
|
||||||
|
|
||||||
use ra_arena::{impl_arena_id, Arena, RawId};
|
use ra_arena::{impl_arena_id, Arena, RawId};
|
||||||
|
@ -79,49 +79,43 @@ impl Sysroot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
|
fn create_command_text(program: &str, args: &[&str]) -> String {
|
||||||
fn try_install_rust_src(cargo_toml: &Path) -> Result<()> {
|
format!("{} {}", program, args.join(" "))
|
||||||
let rustup_output = Command::new("rustup")
|
}
|
||||||
.current_dir(cargo_toml.parent().unwrap())
|
|
||||||
.args(&["component", "add", "rust-src"])
|
|
||||||
.output()
|
|
||||||
.context("rustup component add rust-src failed")?;
|
|
||||||
if !rustup_output.status.success() {
|
|
||||||
match rustup_output.status.code() {
|
|
||||||
Some(code) => bail!(
|
|
||||||
"failed to install rust-src: rustup component add rust-src exited with code {}",
|
|
||||||
code
|
|
||||||
),
|
|
||||||
None => bail!(
|
|
||||||
"failed to install rust-src: rustup component add rust-src terminated by signal"
|
|
||||||
),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
|
fn run_command_in_cargo_dir(cargo_toml: &Path, program: &str, args: &[&str]) -> Result<Output> {
|
||||||
|
let output = Command::new(program)
|
||||||
|
.current_dir(cargo_toml.parent().unwrap())
|
||||||
|
.args(args)
|
||||||
|
.output()
|
||||||
|
.context(format!("{} failed", create_command_text(program, args)))?;
|
||||||
|
if !output.status.success() {
|
||||||
|
match output.status.code() {
|
||||||
|
Some(code) => bail!(
|
||||||
|
"failed to run the command: '{}' exited with code {}",
|
||||||
|
create_command_text(program, args),
|
||||||
|
code
|
||||||
|
),
|
||||||
|
None => bail!(
|
||||||
|
"failed to run the command: '{}' terminated by signal",
|
||||||
|
create_command_text(program, args)
|
||||||
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Ok(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
|
||||||
if let Ok(path) = env::var("RUST_SRC_PATH") {
|
if let Ok(path) = env::var("RUST_SRC_PATH") {
|
||||||
return Ok(path.into());
|
return Ok(path.into());
|
||||||
}
|
}
|
||||||
let rustc_output = Command::new("rustc")
|
let rustc_output = run_command_in_cargo_dir(cargo_toml, "rustc", &["--print", "sysroot"])?;
|
||||||
.current_dir(cargo_toml.parent().unwrap())
|
|
||||||
.args(&["--print", "sysroot"])
|
|
||||||
.output()
|
|
||||||
.context("rustc --print sysroot failed")?;
|
|
||||||
if !rustc_output.status.success() {
|
|
||||||
match rustc_output.status.code() {
|
|
||||||
Some(code) => {
|
|
||||||
bail!("failed to locate sysroot: rustc --print sysroot exited with code {}", code)
|
|
||||||
}
|
|
||||||
None => bail!("failed to locate sysroot: rustc --print sysroot terminated by signal"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
let stdout = String::from_utf8(rustc_output.stdout)?;
|
let stdout = String::from_utf8(rustc_output.stdout)?;
|
||||||
let sysroot_path = Path::new(stdout.trim());
|
let sysroot_path = Path::new(stdout.trim());
|
||||||
let src_path = sysroot_path.join("lib/rustlib/src/rust/src");
|
let src_path = sysroot_path.join("lib/rustlib/src/rust/src");
|
||||||
|
|
||||||
if !src_path.exists() {
|
if !src_path.exists() {
|
||||||
try_install_rust_src(cargo_toml)?
|
run_command_in_cargo_dir(cargo_toml, "rustup", &["component", "add", "rust-src"])?;
|
||||||
}
|
}
|
||||||
if !src_path.exists() {
|
if !src_path.exists() {
|
||||||
bail!(
|
bail!(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue