From 99a720c9689cafcec66a06f9b04f400a32edb9eb Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 12 Mar 2024 12:02:26 -0700 Subject: [PATCH] Remove dependency on which crate --- Cargo.lock | 17 +----------- Cargo.toml | 1 - cli/Cargo.toml | 1 - cli/loader/Cargo.toml | 1 - cli/loader/src/lib.rs | 64 +++++++++++++++---------------------------- 5 files changed, 23 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 180ce544..63a77610 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -133,7 +133,7 @@ dependencies = [ "rustc-hash", "shlex", "syn", - "which 4.4.2", + "which", ] [[package]] @@ -1421,7 +1421,6 @@ dependencies = [ "walkdir", "wasmparser 0.201.0", "webbrowser", - "which 6.0.0", ] [[package]] @@ -1461,7 +1460,6 @@ dependencies = [ "tree-sitter", "tree-sitter-highlight", "tree-sitter-tags", - "which 6.0.0", ] [[package]] @@ -1879,19 +1877,6 @@ dependencies = [ "rustix", ] -[[package]] -name = "which" -version = "6.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fa5e0c10bf77f44aac573e498d1a82d5fbd5e91f6fc0a99e7be4b38e85e101c" -dependencies = [ - "either", - "home", - "once_cell", - "rustix", - "windows-sys 0.52.0", -] - [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 62715b22..218fe846 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,6 @@ unindent = "0.2.3" walkdir = "2.5.0" wasmparser = "0.201.0" webbrowser = "0.8.13" -which = "6.0.0" tree-sitter = { version = "0.22.0", path = "./lib" } tree-sitter-loader = { version = "0.22.0", path = "./cli/loader" } diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 7d11bf4a..fd2136ae 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -53,7 +53,6 @@ tiny_http.workspace = true walkdir.workspace = true wasmparser.workspace = true webbrowser.workspace = true -which.workspace = true tree-sitter.workspace = true tree-sitter-config.workspace = true diff --git a/cli/loader/Cargo.toml b/cli/loader/Cargo.toml index c235e4c4..d93765b6 100644 --- a/cli/loader/Cargo.toml +++ b/cli/loader/Cargo.toml @@ -26,7 +26,6 @@ once_cell.workspace = true regex.workspace = true serde.workspace = true serde_json.workspace = true -which.workspace = true tree-sitter.workspace = true tree-sitter-highlight.workspace = true diff --git a/cli/loader/src/lib.rs b/cli/loader/src/lib.rs index a22ce1cf..9556bc99 100644 --- a/cli/loader/src/lib.rs +++ b/cli/loader/src/lib.rs @@ -20,7 +20,6 @@ use serde::{Deserialize, Deserializer, Serialize}; use tree_sitter::{Language, QueryError, QueryErrorKind}; use tree_sitter_highlight::HighlightConfiguration; use tree_sitter_tags::{Error as TagsError, TagsConfiguration}; -use which::which; pub const EMSCRIPTEN_TAG: &str = concat!("docker.io/emscripten/emsdk:", env!("EMSCRIPTEN_VERSION")); @@ -712,62 +711,43 @@ impl Loader { ) -> Result<(), Error> { #[derive(PartialEq, Eq)] enum EmccSource { - Native(PathBuf), + Native, Docker, Podman, } - fn path_of_bin( - name: &str, - test: impl Fn(&Path) -> std::io::Result, - ) -> Option { - let bin_path = which(name).ok()?; - if test(&bin_path).is_ok() { - Some(bin_path) - } else { - None - } - } + let emcc_name = if cfg!(windows) { "emcc.bat" } else { "emcc" }; // Order of preference: emscripten > docker > podman > error - let source = if force_docker { - None + let source = if !force_docker && Command::new(emcc_name).output().is_ok() { + EmccSource::Native + } else if Command::new("docker") + .arg("info") + .output() + .map_or(false, |out| out.status.success()) + { + EmccSource::Docker + } else if Command::new("podman") + .arg("--version") + .output() + .map_or(false, |out| out.status.success()) + { + EmccSource::Podman } else { - path_of_bin(if cfg!(windows) { "emcc.bat" } else { "emcc" }, |p| { - Command::new(p).output() - }) - .map(EmccSource::Native) - } - .or_else(|| { - path_of_bin("docker", |docker| { - // `docker info` should succeed iff the daemon is running - // see https://docs.docker.com/config/daemon/troubleshoot/#check-whether-docker-is-running - Command::new(docker).args(["info"]).output() - }) - .map(|_| EmccSource::Docker) - }) - .or_else(|| { - path_of_bin("podman", |podman| { - Command::new(podman).arg("--version").output() - }) - .map(|_| EmccSource::Podman) - }); - - let Some(cmd) = source else { return Err(anyhow!( - "You must have either emcc or docker on your PATH to run this command" + "You must have either emcc, docker, or podman on your PATH to run this command" )); }; - let mut command = match cmd { - EmccSource::Native(emcc_path) => { - let mut command = Command::new(emcc_path); + let mut command = match source { + EmccSource::Native => { + let mut command = Command::new(emcc_name); command.current_dir(src_path); command } EmccSource::Docker | EmccSource::Podman => { - let mut command = match cmd { + let mut command = match source { EmccSource::Docker => Command::new("docker"), EmccSource::Podman => Command::new("podman"), _ => unreachable!(), @@ -798,7 +778,7 @@ impl Loader { fn getuid() -> u32; } // don't need to set user for podman since PODMAN_USERNS=keep-id is already set - if cmd == EmccSource::Docker { + if source == EmccSource::Docker { let user_id = unsafe { getuid() }; command.args(["--user", &user_id.to_string()]); }