Skip to content

Commit 7e1c943

Browse files
author
Bogdan Mircea
committed
implement BazelCommand trait
1 parent 7896110 commit 7e1c943

4 files changed

Lines changed: 51 additions & 28 deletions

File tree

tools/rust_analyzer/aquery.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use anyhow::Context;
88
use camino::{Utf8Path, Utf8PathBuf};
99
use serde::Deserialize;
1010

11+
use crate::command::BazelCommand;
12+
1113
#[derive(Debug, Deserialize)]
1214
struct AqueryOutput {
1315
artifacts: Vec<Artifact>,
@@ -95,12 +97,7 @@ pub fn get_crate_specs(
9597
log::debug!("Get crate specs with targets: {:?}", targets);
9698
let target_pattern = format!("deps({})", targets.join("+"));
9799

98-
let output = Command::new(bazel)
99-
.current_dir(workspace)
100-
.env_remove("BAZELISK_SKIP_WRAPPER")
101-
.env_remove("BUILD_WORKING_DIRECTORY")
102-
.env_remove("BUILD_WORKSPACE_DIRECTORY")
103-
.arg(format!("--output_base={output_base}"))
100+
let output = Command::new_bazel_command(bazel, Some(workspace), Some(output_base))
104101
.arg("aquery")
105102
.arg("--include_aspects")
106103
.arg("--include_artifacts")

tools/rust_analyzer/command.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::process::Command;
2+
3+
use camino::Utf8Path;
4+
5+
/// Trait used for extending [`Command`] when the purpose is to invoke `bazel`
6+
/// while preserving its builder pattern capabilities.
7+
pub trait BazelCommand {
8+
fn new_bazel_command(
9+
bazel: &Utf8Path,
10+
workspace: Option<&Utf8Path>,
11+
output_base: Option<&Utf8Path>,
12+
) -> Self;
13+
}
14+
15+
impl BazelCommand for Command {
16+
fn new_bazel_command(
17+
bazel: &Utf8Path,
18+
workspace: Option<&Utf8Path>,
19+
output_base: Option<&Utf8Path>,
20+
) -> Self {
21+
let mut cmd = Self::new(bazel);
22+
23+
cmd
24+
// Switch to the workspace directory if one was provided.
25+
.current_dir(workspace.unwrap_or(Utf8Path::new(".")))
26+
.env_remove("BAZELISK_SKIP_WRAPPER")
27+
.env_remove("BUILD_WORKING_DIRECTORY")
28+
.env_remove("BUILD_WORKSPACE_DIRECTORY")
29+
// Set the output_base if one was provided.
30+
.args(output_base.map(|s| format!("--output_base={s}")));
31+
32+
cmd
33+
}
34+
}

tools/rust_analyzer/lib.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
mod aquery;
2+
mod command;
23
mod rust_project;
34

45
use std::{collections::HashMap, process::Command};
56

67
use anyhow::bail;
78
use camino::Utf8Path;
9+
use command::BazelCommand;
810
use runfiles::Runfiles;
911
use rust_project::RustProject;
1012
pub use rust_project::{DiscoverProject, NormalizedProjectString, RustAnalyzerArg};
@@ -24,12 +26,7 @@ pub fn generate_crate_info(
2426
log::info!("running bazel build...");
2527
log::debug!("Building rust_analyzer_crate_spec files for {:?}", targets);
2628

27-
let output = Command::new(bazel)
28-
.current_dir(workspace)
29-
.env_remove("BAZELISK_SKIP_WRAPPER")
30-
.env_remove("BUILD_WORKING_DIRECTORY")
31-
.env_remove("BUILD_WORKSPACE_DIRECTORY")
32-
.arg(format!("--output_base={output_base}"))
29+
let output = Command::new_bazel_command(bazel, Some(workspace), Some(output_base))
3330
.arg("build")
3431
.arg("--norun_validations")
3532
.arg(format!(
@@ -40,11 +37,9 @@ pub fn generate_crate_info(
4037
.output()?;
4138

4239
if !output.status.success() {
43-
bail!(
44-
"bazel build failed:({})\n{}",
45-
output.status,
46-
String::from_utf8_lossy(&output.stderr)
47-
);
40+
let status = output.status;
41+
let stderr = String::from_utf8_lossy(&output.stderr);
42+
bail!("bazel build failed: ({status})\n{stderr}");
4843
}
4944

5045
log::info!("bazel build finished");
@@ -89,21 +84,14 @@ pub fn get_bazel_info(
8984
workspace: Option<&Utf8Path>,
9085
output_base: Option<&Utf8Path>,
9186
) -> anyhow::Result<HashMap<String, String>> {
92-
let output = Command::new(&bazel)
93-
// Switch to the workspace directory if one was provided.
94-
.current_dir(workspace.unwrap_or(Utf8Path::new(".")))
95-
.env_remove("BAZELISK_SKIP_WRAPPER")
96-
.env_remove("BUILD_WORKING_DIRECTORY")
97-
.env_remove("BUILD_WORKSPACE_DIRECTORY")
98-
// Set the output_base if one was provided.
99-
.args(output_base.map(|s| format!("--output_base={s}")))
87+
let output = Command::new_bazel_command(bazel, workspace, output_base)
10088
.arg("info")
10189
.output()?;
10290

10391
if !output.status.success() {
10492
let status = output.status;
10593
let stderr = String::from_utf8_lossy(&output.stderr);
106-
bail!("Failed to run `bazel info` ({status:?}): {stderr}");
94+
bail!("bazel info failed: ({status:?})\n{stderr}");
10795
}
10896

10997
// Extract and parse the output.

tools/rust_analyzer/rust_project.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
//! Library for generating rust_project.json files from a `Vec<CrateSpec>`
22
//! See official documentation of file format at https://rust-analyzer.github.io/manual.html
33
4-
use std::collections::{
5-
convert::TryFrom, fmt::Display, fs, str::FromStr, BTreeMap, BTreeSet, HashMap,
4+
use std::{
5+
collections::{BTreeMap, BTreeSet, HashMap},
6+
convert::TryFrom,
7+
fmt::Display,
8+
fs,
9+
str::FromStr,
610
};
711

812
use anyhow::{anyhow, bail, Context};

0 commit comments

Comments
 (0)