Re-export which from helix-stdx::env

We use `which::which` in many crates, so `which` was a separate
dependency across all of them. We can centralize `which` into the
stdx crate so it's easy for all crates to depend on it.

I also moved the rest of `helix-view/src/env.rs` into helix-stdx's
`env` module since it only contained a thin wrapper around `which`
and `std::env`.
This commit is contained in:
Michael Davis 2024-01-23 09:29:07 -05:00 committed by Blaž Hrastnik
parent 6bfe1ddc53
commit 6d724a8f33
17 changed files with 38 additions and 39 deletions

23
Cargo.lock generated
View File

@ -358,9 +358,9 @@ checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b"
[[package]] [[package]]
name = "either" name = "either"
version = "1.8.1" version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
@ -1095,12 +1095,12 @@ dependencies = [
"anyhow", "anyhow",
"fern", "fern",
"helix-core", "helix-core",
"helix-stdx",
"log", "log",
"serde", "serde",
"serde_json", "serde_json",
"thiserror", "thiserror",
"tokio", "tokio",
"which",
] ]
[[package]] [[package]]
@ -1134,7 +1134,6 @@ dependencies = [
"threadpool", "threadpool",
"toml", "toml",
"tree-sitter", "tree-sitter",
"which",
] ]
[[package]] [[package]]
@ -1157,7 +1156,6 @@ dependencies = [
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"which",
] ]
[[package]] [[package]]
@ -1172,6 +1170,7 @@ dependencies = [
"etcetera", "etcetera",
"ropey", "ropey",
"tempfile", "tempfile",
"which",
] ]
[[package]] [[package]]
@ -1214,7 +1213,6 @@ dependencies = [
"tokio-stream", "tokio-stream",
"toml", "toml",
"url", "url",
"which",
] ]
[[package]] [[package]]
@ -1280,7 +1278,6 @@ dependencies = [
"tokio-stream", "tokio-stream",
"toml", "toml",
"url", "url",
"which",
] ]
[[package]] [[package]]
@ -1294,11 +1291,11 @@ dependencies = [
[[package]] [[package]]
name = "home" name = "home"
version = "0.5.5" version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5"
dependencies = [ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -2359,15 +2356,15 @@ checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
[[package]] [[package]]
name = "which" name = "which"
version = "5.0.0" version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bf3ea8596f3a0dd5980b46430f2058dfe2c36a27ccfbb1845d6fbfcd9ba6e14" checksum = "7fa5e0c10bf77f44aac573e498d1a82d5fbd5e91f6fc0a99e7be4b38e85e101c"
dependencies = [ dependencies = [
"either", "either",
"home", "home",
"once_cell", "once_cell",
"rustix", "rustix",
"windows-sys 0.48.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]

View File

@ -13,6 +13,7 @@ homepage.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
helix-stdx = { path = "../helix-stdx" }
helix-core = { path = "../helix-core" } helix-core = { path = "../helix-core" }
anyhow = "1.0" anyhow = "1.0"
@ -21,7 +22,6 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
thiserror = "1.0" thiserror = "1.0"
tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "net", "sync"] } tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "net", "sync"] }
which = "5.0.0"
[dev-dependencies] [dev-dependencies]
fern = "0.6" fern = "0.6"

View File

@ -113,7 +113,7 @@ pub fn stdio(
id: usize, id: usize,
) -> Result<(Self, UnboundedReceiver<Payload>)> { ) -> Result<(Self, UnboundedReceiver<Payload>)> {
// Resolve path to the binary // Resolve path to the binary
let cmd = which::which(cmd).map_err(|err| anyhow::anyhow!(err))?; let cmd = helix_stdx::env::which(cmd).map_err(|err| anyhow::anyhow!(err))?;
let process = Command::new(cmd) let process = Command::new(cmd)
.args(args) .args(args)

View File

@ -24,7 +24,6 @@ etcetera = "0.8"
tree-sitter.workspace = true tree-sitter.workspace = true
once_cell = "1.19" once_cell = "1.19"
log = "0.4" log = "0.4"
which = "5.0.0"
# TODO: these two should be on !wasm32 only # TODO: these two should be on !wasm32 only

View File

@ -86,7 +86,7 @@ pub fn get_language(name: &str) -> Result<Language> {
} }
fn ensure_git_is_available() -> Result<()> { fn ensure_git_is_available() -> Result<()> {
match which::which("git") { match helix_stdx::env::which("git") {
Ok(_cmd) => Ok(()), Ok(_cmd) => Ok(()),
Err(err) => Err(anyhow::anyhow!("'git' could not be found ({err})")), Err(err) => Err(anyhow::anyhow!("'git' could not be found ({err})")),
} }

View File

@ -29,5 +29,4 @@ serde_json = "1.0"
thiserror = "1.0" thiserror = "1.0"
tokio = { version = "1.35", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "sync"] } tokio = { version = "1.35", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "sync"] }
tokio-stream = "0.1.14" tokio-stream = "0.1.14"
which = "5.0.0"
parking_lot = "0.12.1" parking_lot = "0.12.1"

View File

@ -183,7 +183,7 @@ pub fn start(
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
) -> Result<(Self, UnboundedReceiver<(usize, Call)>, Arc<Notify>)> { ) -> Result<(Self, UnboundedReceiver<(usize, Call)>, Arc<Notify>)> {
// Resolve path to the binary // Resolve path to the binary
let cmd = which::which(cmd).map_err(|err| anyhow::anyhow!(err))?; let cmd = helix_stdx::env::which(cmd).map_err(|err| anyhow::anyhow!(err))?;
let process = Command::new(cmd) let process = Command::new(cmd)
.envs(server_environment) .envs(server_environment)

View File

@ -15,6 +15,7 @@ homepage.workspace = true
dunce = "1.0" dunce = "1.0"
etcetera = "0.8" etcetera = "0.8"
ropey = { version = "1.6.1", default-features = false } ropey = { version = "1.6.1", default-features = false }
which = "6.0"
[dev-dependencies] [dev-dependencies]
tempfile = "3.9" tempfile = "3.9"

View File

@ -1,3 +1,5 @@
pub use which::which;
use std::{ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::RwLock, sync::RwLock,
@ -30,6 +32,14 @@ pub fn set_current_working_dir(path: impl AsRef<Path>) -> std::io::Result<()> {
Ok(()) Ok(())
} }
pub fn env_var_is_set(env_var_name: &str) -> bool {
std::env::var_os(env_var_name).is_some()
}
pub fn binary_exists(binary_name: &str) -> bool {
which::which(binary_name).is_ok()
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{current_working_dir, set_current_working_dir}; use super::{current_working_dir, set_current_working_dir};

View File

@ -35,8 +35,6 @@ helix-loader = { path = "../helix-loader" }
anyhow = "1" anyhow = "1"
once_cell = "1.19" once_cell = "1.19"
which = "5.0.0"
tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot"] } tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot"] }
tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"] } tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"] }
crossterm = { version = "0.27", features = ["event-stream"] } crossterm = { version = "0.27", features = ["event-stream"] }

View File

@ -182,7 +182,7 @@ pub fn languages_all() -> std::io::Result<()> {
.sort_unstable_by_key(|l| l.language_id.clone()); .sort_unstable_by_key(|l| l.language_id.clone());
let check_binary = |cmd: Option<&str>| match cmd { let check_binary = |cmd: Option<&str>| match cmd {
Some(cmd) => match which::which(cmd) { Some(cmd) => match helix_stdx::env::which(cmd) {
Ok(_) => column(&format!("{}", cmd), Color::Green), Ok(_) => column(&format!("{}", cmd), Color::Green),
Err(_) => column(&format!("{}", cmd), Color::Red), Err(_) => column(&format!("{}", cmd), Color::Red),
}, },
@ -322,7 +322,7 @@ fn probe_protocols<'a, I: Iterator<Item = &'a str> + 'a>(
writeln!(stdout)?; writeln!(stdout)?;
for cmd in server_cmds { for cmd in server_cmds {
let (path, icon) = match which::which(cmd) { let (path, icon) = match helix_stdx::env::which(cmd) {
Ok(path) => (path.display().to_string().green(), "".green()), Ok(path) => (path.display().to_string().green(), "".green()),
Err(_) => (format!("'{}' not found in $PATH", cmd).red(), "".red()), Err(_) => (format!("'{}' not found in $PATH", cmd).red(), "".red()),
}; };
@ -344,7 +344,7 @@ fn probe_protocol(protocol_name: &str, server_cmd: Option<String>) -> std::io::R
writeln!(stdout, "Configured {}: {}", protocol_name, cmd_name)?; writeln!(stdout, "Configured {}: {}", protocol_name, cmd_name)?;
if let Some(cmd) = server_cmd { if let Some(cmd) = server_cmd {
let path = match which::which(&cmd) { let path = match helix_stdx::env::which(&cmd) {
Ok(path) => path.display().to_string().green(), Ok(path) => path.display().to_string().green(),
Err(_) => format!("'{}' not found in $PATH", cmd).red(), Err(_) => format!("'{}' not found in $PATH", cmd).red(),
}; };

View File

@ -46,7 +46,6 @@ serde_json = "1.0"
toml = "0.7" toml = "0.7"
log = "~0.4" log = "~0.4"
which = "5.0.0"
parking_lot = "0.12.1" parking_lot = "0.12.1"

View File

@ -73,7 +73,7 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> { pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
use crate::env::{binary_exists, env_var_is_set}; use helix_stdx::env::{binary_exists, env_var_is_set};
if env_var_is_set("TMUX") && binary_exists("tmux") { if env_var_is_set("TMUX") && binary_exists("tmux") {
command_provider! { command_provider! {
@ -98,7 +98,7 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
#[cfg(not(any(windows, target_os = "wasm32", target_os = "macos")))] #[cfg(not(any(windows, target_os = "wasm32", target_os = "macos")))]
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> { pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
use crate::env::{binary_exists, env_var_is_set}; use helix_stdx::env::{binary_exists, env_var_is_set};
use provider::command::is_exit_success; use provider::command::is_exit_success;
// TODO: support for user-defined provider, probably when we have plugin support by setting a // TODO: support for user-defined provider, probably when we have plugin support by setting a
// variable? // variable?

View File

@ -726,7 +726,12 @@ pub fn format(&self) -> Option<BoxFuture<'static, Result<Transaction, FormatterE
if let Some((fmt_cmd, fmt_args)) = self if let Some((fmt_cmd, fmt_args)) = self
.language_config() .language_config()
.and_then(|c| c.formatter.as_ref()) .and_then(|c| c.formatter.as_ref())
.and_then(|formatter| Some((which::which(&formatter.command).ok()?, &formatter.args))) .and_then(|formatter| {
Some((
helix_stdx::env::which(&formatter.command).ok()?,
&formatter.args,
))
})
{ {
use std::process::Stdio; use std::process::Stdio;
let text = self.text().clone(); let text = self.text().clone();

View File

@ -329,7 +329,7 @@ pub struct TerminalConfig {
#[cfg(windows)] #[cfg(windows)]
pub fn get_terminal_provider() -> Option<TerminalConfig> { pub fn get_terminal_provider() -> Option<TerminalConfig> {
use crate::env::binary_exists; use helix_stdx::env::binary_exists;
if binary_exists("wt") { if binary_exists("wt") {
return Some(TerminalConfig { return Some(TerminalConfig {
@ -352,7 +352,7 @@ pub fn get_terminal_provider() -> Option<TerminalConfig> {
#[cfg(not(any(windows, target_os = "wasm32")))] #[cfg(not(any(windows, target_os = "wasm32")))]
pub fn get_terminal_provider() -> Option<TerminalConfig> { pub fn get_terminal_provider() -> Option<TerminalConfig> {
use crate::env::{binary_exists, env_var_is_set}; use helix_stdx::env::{binary_exists, env_var_is_set};
if env_var_is_set("TMUX") && binary_exists("tmux") { if env_var_is_set("TMUX") && binary_exists("tmux") {
return Some(TerminalConfig { return Some(TerminalConfig {

View File

@ -1,8 +0,0 @@
pub fn binary_exists(binary_name: &str) -> bool {
which::which(binary_name).is_ok()
}
#[cfg(not(windows))]
pub fn env_var_is_set(env_var_name: &str) -> bool {
std::env::var_os(env_var_name).is_some()
}

View File

@ -5,7 +5,6 @@
pub mod clipboard; pub mod clipboard;
pub mod document; pub mod document;
pub mod editor; pub mod editor;
pub mod env;
pub mod events; pub mod events;
pub mod graphics; pub mod graphics;
pub mod gutter; pub mod gutter;