diff --git a/Cargo.lock b/Cargo.lock index daf69f5..b0b8538 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -63,6 +63,54 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "anstream" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "anyhow" version = "1.0.79" @@ -89,6 +137,7 @@ name = "bazel-lsp" version = "0.1.1" dependencies = [ "anyhow", + "clap", "either", "itertools 0.12.0", "lsp-types", @@ -150,6 +199,46 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.48", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + [[package]] name = "clipboard-win" version = "4.5.0" @@ -166,6 +255,12 @@ name = "cmp_any" version = "0.8.1" source = "git+https://github.com/facebookexperimental/starlark-rust.git#9d3444a588f9f93c11975741d73ef6ef907d4dcf" +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "convert_case" version = "0.4.0" @@ -415,6 +510,12 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.4" diff --git a/Cargo.toml b/Cargo.toml index 1f31fd7..41d5648 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] anyhow = "1.0.79" +clap = { version = "4.4.18", features = ["derive"] } either = "1.9.0" itertools = "0.12.0" lsp-types = "0.94.1" diff --git a/src/client.rs b/src/client.rs index ef3b67e..2324ba0 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,4 +1,8 @@ -use std::{collections::HashMap, process::Command}; +use std::{ + collections::HashMap, + path::{Path, PathBuf}, + process::Command, +}; use anyhow::anyhow; @@ -16,11 +20,21 @@ pub(crate) trait BazelClient { fn dump_repo_mapping(&self, repo: &str) -> anyhow::Result>; } -pub(crate) struct BazelCli; +pub(crate) struct BazelCli { + bazel: PathBuf, +} + +impl BazelCli { + pub fn new>(bazel: P) -> Self { + Self { + bazel: bazel.as_ref().to_owned(), + } + } +} impl BazelClient for BazelCli { fn info(&self) -> anyhow::Result { - let output = Command::new("bazel") + let output = Command::new(&self.bazel) .arg("info") .current_dir(std::env::current_dir()?) .output()?; @@ -49,16 +63,14 @@ impl BazelClient for BazelCli { } fn dump_repo_mapping(&self, repo: &str) -> anyhow::Result> { - let output = Command::new("bazel") + let output = Command::new(&self.bazel) .args(["mod", "dump_repo_mapping"]) .arg(repo) .current_dir(std::env::current_dir()?) .output()?; if !output.status.success() { - return Err(anyhow!( - "Command `bazel mod dump_repo_mapping` failed" - )); + return Err(anyhow!("Command `bazel mod dump_repo_mapping` failed")); } Ok(serde_json::from_slice(&output.stdout)?) diff --git a/src/main.rs b/src/main.rs index e8470d2..bbefa36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,12 +5,32 @@ mod label; #[cfg(test)] pub mod test_fixture; +use std::path::PathBuf; + use bazel::BazelContext; +use clap::Parser; use client::BazelCli; use eval::ContextMode; +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + /// Location of the bazel binary + #[arg(long, default_value = "bazel")] + bazel: PathBuf, +} + fn main() -> anyhow::Result<()> { - let ctx = BazelContext::new(BazelCli, ContextMode::Check, true, &[], true)?; + let args = Args::parse(); + + let ctx = BazelContext::new( + BazelCli::new(args.bazel), + ContextMode::Check, + true, + &[], + true, + )?; + starlark_lsp::server::stdio_server(ctx)?; Ok(())