aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-symfony-process/src/executable_finder.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-09 11:14:42 +0900
committernsfisis <nsfisis@gmail.com>2026-08-09 11:14:42 +0900
commit446f719f6c34453f027d5ccdbf81b16e63f4c982 (patch)
tree9c0443adbb99da5891a81d0131fc414d9a04c8ef /crates/shirabe-symfony-process/src/executable_finder.rs
parent880ba0fd1d05faf98588cc326b3d9fbe625ebf2b (diff)
downloadphp-shirabe-446f719f6c34453f027d5ccdbf81b16e63f4c982.tar.gz
php-shirabe-446f719f6c34453f027d5ccdbf81b16e63f4c982.tar.zst
php-shirabe-446f719f6c34453f027d5ccdbf81b16e63f4c982.zip
refactor(symfony-process): extract symfony/process into the shirabe-symfony-process crate
Move `Symfony\Component\Process` out of shirabe-external-packages and into its own crate, so the path is `shirabe_symfony_process::Process` instead of `shirabe_external_packages::symfony::process::Process`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-symfony-process/src/executable_finder.rs')
-rw-r--r--crates/shirabe-symfony-process/src/executable_finder.rs116
1 files changed, 116 insertions, 0 deletions
diff --git a/crates/shirabe-symfony-process/src/executable_finder.rs b/crates/shirabe-symfony-process/src/executable_finder.rs
new file mode 100644
index 00000000..5b1c0994
--- /dev/null
+++ b/crates/shirabe-symfony-process/src/executable_finder.rs
@@ -0,0 +1,116 @@
+//! ref: composer/vendor/symfony/process/ExecutableFinder.php
+
+const CMD_BUILTINS: &[&str] = &[
+ "assoc", "break", "call", "cd", "chdir", "cls", "color", "copy", "date", "del", "dir", "echo",
+ "endlocal", "erase", "exit", "for", "ftype", "goto", "help", "if", "label", "md", "mkdir",
+ "mklink", "move", "path", "pause", "popd", "prompt", "pushd", "rd", "rem", "ren", "rename",
+ "rmdir", "set", "setlocal", "shift", "start", "time", "title", "type", "ver", "vol",
+];
+
+#[derive(Debug)]
+pub struct ExecutableFinder {
+ suffixes: Vec<String>,
+}
+
+impl Default for ExecutableFinder {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl ExecutableFinder {
+ pub fn new() -> Self {
+ Self { suffixes: vec![] }
+ }
+
+ pub fn find(&self, name: &str, default: Option<&str>, extra_dirs: &[String]) -> Option<String> {
+ // windows built-in commands that are present in cmd.exe should not be resolved using PATH as they do not exist as exes
+ if cfg!(windows) && CMD_BUILTINS.contains(&shirabe_php_shim::strtolower(name).as_str()) {
+ return Some(name.to_string());
+ }
+
+ let path = shirabe_php_shim::getenv("PATH")
+ .or_else(|| shirabe_php_shim::getenv("Path"))
+ .map(|v| v.to_string_lossy().into_owned())
+ .unwrap_or_default();
+ let mut dirs: Vec<String> = std::env::split_paths(&path)
+ .map(|dir| dir.into_os_string().into_string().unwrap())
+ .collect();
+ dirs.extend_from_slice(extra_dirs);
+
+ let mut suffixes: Vec<String> = vec![];
+ if cfg!(windows) {
+ let path_ext =
+ shirabe_php_shim::getenv("PATHEXT").map(|v| v.to_string_lossy().into_owned());
+ suffixes = self.suffixes.clone();
+ let exts = match path_ext {
+ Some(ref ext) if !ext.is_empty() => std::env::split_paths(ext)
+ .map(|e| e.into_os_string().into_string().unwrap())
+ .collect(),
+ _ => vec![
+ ".exe".to_string(),
+ ".bat".to_string(),
+ ".cmd".to_string(),
+ ".com".to_string(),
+ ],
+ };
+ suffixes.extend(exts);
+ }
+ suffixes =
+ if !shirabe_php_shim::pathinfo(name, shirabe_php_shim::PATHINFO_EXTENSION).is_empty() {
+ let mut s = vec![String::new()];
+ s.extend(suffixes);
+ s
+ } else {
+ suffixes.push(String::new());
+ suffixes
+ };
+ for suffix in &suffixes {
+ for dir in &dirs {
+ let dir = if dir.is_empty() { "." } else { dir.as_str() };
+ let file = std::path::Path::new(dir)
+ .join(format!("{name}{suffix}"))
+ .into_os_string()
+ .into_string()
+ .unwrap();
+ if shirabe_php_shim::is_file(&file)
+ && (cfg!(windows) || shirabe_php_shim::is_executable(&file))
+ {
+ return Some(file);
+ }
+
+ if !shirabe_php_shim::is_dir(dir)
+ && shirabe_php_shim::basename(dir) == format!("{name}{suffix}")
+ && shirabe_php_shim::is_executable(dir)
+ {
+ return Some(dir.to_string());
+ }
+ }
+ }
+
+ if cfg!(windows)
+ || name.len()
+ != shirabe_php_shim::strcspn(name, &format!("/{}", std::path::MAIN_SEPARATOR))
+ {
+ return default.map(ToString::to_string);
+ }
+
+ let exec_result = shirabe_php_shim::exec(
+ &format!("command -v -- {}", shirabe_php_shim::escapeshellarg(name)),
+ None,
+ None,
+ )
+ .unwrap_or_default();
+
+ let executable_path = shirabe_php_shim::substr(
+ &exec_result,
+ 0,
+ shirabe_php_shim::strpos(&exec_result, shirabe_php_shim::PHP_EOL).map(|i| i as i64),
+ );
+ if !executable_path.is_empty() && shirabe_php_shim::is_executable(&executable_path) {
+ return Some(executable_path);
+ }
+
+ default.map(ToString::to_string)
+ }
+}