diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-09 06:25:03 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-09 06:25:03 +0900 |
| commit | 6dd09abb33d86e1aa737667f9e15a18ed41d52e1 (patch) | |
| tree | 83f961fb8657c3b5c8440caecaf61a52fdc3a2de | |
| parent | 880a5eaad9dcdfd31563385f11ba1f63d38cfd14 (diff) | |
| download | php-shirabe-6dd09abb33d86e1aa737667f9e15a18ed41d52e1.tar.gz php-shirabe-6dd09abb33d86e1aa737667f9e15a18ed41d52e1.tar.zst php-shirabe-6dd09abb33d86e1aa737667f9e15a18ed41d52e1.zip | |
refactor(external-packages): test for Windows with cfg!(windows)
MAIN_SEPARATOR is the path separator; PHP's `'\\' === DIRECTORY_SEPARATOR`
uses it as an OS test only because PHP has no dedicated one. cfg!(windows)
says what the branch actually selects on, and leaves MAIN_SEPARATOR to the
sites that really join or split paths.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
9 files changed, 25 insertions, 43 deletions
diff --git a/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs b/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs index f5242d9a..8ec8d15c 100644 --- a/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs +++ b/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs @@ -685,7 +685,7 @@ impl QuestionHelper { input_stream: &shirabe_php_shim::PhpResource, trimmable: bool, ) -> anyhow::Result<Result<String, RuntimeException>> { - if std::path::MAIN_SEPARATOR == '\\' { + if cfg!(windows) { let mut exe = format!( "{}/../Resources/bin/hiddeninput.exe", shirabe_php_shim::dir() diff --git a/crates/shirabe-external-packages/src/symfony/console/output/stream_output.rs b/crates/shirabe-external-packages/src/symfony/console/output/stream_output.rs index 8ee2f186..8be98a57 100644 --- a/crates/shirabe-external-packages/src/symfony/console/output/stream_output.rs +++ b/crates/shirabe-external-packages/src/symfony/console/output/stream_output.rs @@ -98,8 +98,7 @@ impl StreamOutput { return false; } - if std::path::MAIN_SEPARATOR == '\\' && shirabe_php_shim::sapi_windows_vt100_support(stream) - { + if cfg!(windows) && shirabe_php_shim::sapi_windows_vt100_support(stream) { return true; } diff --git a/crates/shirabe-external-packages/src/symfony/console/style/symfony_style.rs b/crates/shirabe-external-packages/src/symfony/console/style/symfony_style.rs index e0994e99..8ae4d885 100644 --- a/crates/shirabe-external-packages/src/symfony/console/style/symfony_style.rs +++ b/crates/shirabe-external-packages/src/symfony/console/style/symfony_style.rs @@ -56,11 +56,7 @@ impl SymfonyStyle { output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, ) -> Self { let buffered_output = TrimmedBufferOutput::new( - if std::path::MAIN_SEPARATOR == '\\' { - 4 - } else { - 2 - }, + if cfg!(windows) { 4 } else { 2 }, Some(output.borrow().get_verbosity()), false, // TODO(plugin): clone of the formatter; PHP `clone $output->getFormatter()`. @@ -72,10 +68,7 @@ impl SymfonyStyle { let w = Terminal::new().get_width(); if w != 0 { w } else { MAX_LINE_LENGTH } }; - let line_length = std::cmp::min( - width - (std::path::MAIN_SEPARATOR == '\\') as i64, - MAX_LINE_LENGTH, - ); + let line_length = std::cmp::min(width - cfg!(windows) as i64, MAX_LINE_LENGTH); let inner = OutputStyle::new(output.clone()); @@ -279,7 +272,7 @@ impl SymfonyStyle { pub fn create_progress_bar(&self, max: i64) -> ProgressBar { let mut progress_bar = self.inner.create_progress_bar(max); - if std::path::MAIN_SEPARATOR != '\\' + if !cfg!(windows) || shirabe_php_shim::getenv("TERM_PROGRAM").as_deref() == Some(std::ffi::OsStr::new("Hyper")) { diff --git a/crates/shirabe-external-packages/src/symfony/console/terminal.rs b/crates/shirabe-external-packages/src/symfony/console/terminal.rs index 353dffcb..9af712ad 100644 --- a/crates/shirabe-external-packages/src/symfony/console/terminal.rs +++ b/crates/shirabe-external-packages/src/symfony/console/terminal.rs @@ -69,11 +69,7 @@ impl Terminal { let result = shirabe_php_shim::shell_exec(&format!( "stty 2> {}", - if std::path::MAIN_SEPARATOR == '\\' { - "NUL" - } else { - "/dev/null" - } + if cfg!(windows) { "NUL" } else { "/dev/null" } )) .is_some(); STTY.with(|s| s.set(Some(result))); @@ -81,7 +77,7 @@ impl Terminal { } fn init_dimensions() { - if std::path::MAIN_SEPARATOR == '\\' { + if cfg!(windows) { let ansicon = shirabe_php_shim::getenv("ANSICON"); let mut matches: Vec<Option<String>> = Vec::new(); if let Some(ansicon) = &ansicon diff --git a/crates/shirabe-external-packages/src/symfony/filesystem/filesystem.rs b/crates/shirabe-external-packages/src/symfony/filesystem/filesystem.rs index 0aba97fb..295a7b86 100644 --- a/crates/shirabe-external-packages/src/symfony/filesystem/filesystem.rs +++ b/crates/shirabe-external-packages/src/symfony/filesystem/filesystem.rs @@ -201,7 +201,7 @@ impl Filesystem { if shirabe_php_shim::is_link(&file) { // See https://bugs.php.net/52176 if !(shirabe_php_shim::unlink(&file) - || std::path::MAIN_SEPARATOR != '\\' + || !cfg!(windows) || shirabe_php_shim::rmdir(&file)) && shirabe_php_shim::file_exists(&file) { @@ -272,7 +272,7 @@ impl Filesystem { let mut origin_dir = origin_dir.to_string(); let mut target_dir = target_dir.to_string(); - if std::path::MAIN_SEPARATOR == '\\' { + if cfg!(windows) { origin_dir = shirabe_php_shim::strtr(&origin_dir, "/", "\\"); target_dir = shirabe_php_shim::strtr(&target_dir, "/", "\\"); diff --git a/crates/shirabe-external-packages/src/symfony/process/executable_finder.rs b/crates/shirabe-external-packages/src/symfony/process/executable_finder.rs index a00a302c..cd7d378f 100644 --- a/crates/shirabe-external-packages/src/symfony/process/executable_finder.rs +++ b/crates/shirabe-external-packages/src/symfony/process/executable_finder.rs @@ -25,9 +25,7 @@ impl ExecutableFinder { 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 std::path::MAIN_SEPARATOR == '\\' - && CMD_BUILTINS.contains(&shirabe_php_shim::strtolower(name).as_str()) - { + if cfg!(windows) && CMD_BUILTINS.contains(&shirabe_php_shim::strtolower(name).as_str()) { return Some(name.to_string()); } @@ -39,7 +37,7 @@ impl ExecutableFinder { dirs.extend_from_slice(extra_dirs); let mut suffixes: Vec<String> = vec![]; - if std::path::MAIN_SEPARATOR == '\\' { + if cfg!(windows) { let path_ext = shirabe_php_shim::getenv("PATHEXT").map(|v| v.to_string_lossy().into_owned()); suffixes = self.suffixes.clone(); @@ -70,7 +68,7 @@ impl ExecutableFinder { let dir = if dir.is_empty() { "." } else { dir.as_str() }; let file = format!("{dir}{}{name}{suffix}", std::path::MAIN_SEPARATOR); if shirabe_php_shim::is_file(&file) - && (std::path::MAIN_SEPARATOR == '\\' || shirabe_php_shim::is_executable(&file)) + && (cfg!(windows) || shirabe_php_shim::is_executable(&file)) { return Some(file); } @@ -84,7 +82,7 @@ impl ExecutableFinder { } } - if std::path::MAIN_SEPARATOR == '\\' + if cfg!(windows) || name.len() != shirabe_php_shim::strcspn(name, &format!("/{}", std::path::MAIN_SEPARATOR)) { diff --git a/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs b/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs index c4236743..cf4f3414 100644 --- a/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs +++ b/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs @@ -17,7 +17,7 @@ pub struct WindowsPipes { impl WindowsPipes { pub fn new(_input: PhpMixed) -> Self { - // Windows-only path: never constructed on POSIX (MAIN_SEPARATOR is '/'). + // Windows-only path: never constructed on non-Windows targets. todo!() } } diff --git a/crates/shirabe-external-packages/src/symfony/process/process.rs b/crates/shirabe-external-packages/src/symfony/process/process.rs index 64ed3a84..3fb3fab2 100644 --- a/crates/shirabe-external-packages/src/symfony/process/process.rs +++ b/crates/shirabe-external-packages/src/symfony/process/process.rs @@ -245,7 +245,7 @@ impl Process { this.set_input(input)?; this.set_timeout(timeout)?; - this.use_file_handles = std::path::MAIN_SEPARATOR == '\\'; + this.use_file_handles = cfg!(windows); Ok(this) } @@ -309,7 +309,7 @@ impl Process { .collect::<Vec<_>>() .join(" "); - if std::path::MAIN_SEPARATOR != '\\' { + if !cfg!(windows) { // exec is mandatory to deal with sending a signal to the process cmd = format!("exec {}", cmd); } @@ -318,7 +318,7 @@ impl Process { CommandLine::String(s) => self.replace_placeholders(s, &env)?, }; - if std::path::MAIN_SEPARATOR == '\\' { + if cfg!(windows) { commandline = self.prepare_windows_command_line(&commandline, &mut env)?; } else if !self.use_file_handles && self.is_sigchild_enabled() { // last exit code is output on the fourth pipe and caught to work around --enable-sigchild @@ -414,9 +414,8 @@ impl Process { loop { self.check_timeout()?; let running = self.is_running() - && (std::path::MAIN_SEPARATOR == '\\' - || self.process_pipes.as_ref().unwrap().are_open()); - self.read_pipes(running, std::path::MAIN_SEPARATOR != '\\' || !running); + && (cfg!(windows) || self.process_pipes.as_ref().unwrap().are_open()); + self.read_pipes(running, !cfg!(windows) || !running); if !running { break; } @@ -641,7 +640,7 @@ impl Process { /// Enables or disables the TTY mode. pub fn set_tty(&mut self, tty: bool) -> anyhow::Result<&mut Self> { - if std::path::MAIN_SEPARATOR == '\\' && tty { + if cfg!(windows) && tty { return Err(RuntimeException::new( "TTY mode is not supported on Windows platform.".to_string(), ) @@ -740,7 +739,7 @@ impl Process { /// Creates the descriptors needed by the proc_open. fn get_descriptors(&mut self) -> Vec<Descriptor> { // TODO(plugin): $this->input instanceof \Iterator -> rewind() is not modeled. - if std::path::MAIN_SEPARATOR == '\\' { + if cfg!(windows) { self.process_pipes = Some(Box::new(WindowsPipes::new(self.input.clone()))); } else { self.process_pipes = Some(Box::new(UnixPipes::new( @@ -813,10 +812,7 @@ impl Process { } } - self.read_pipes( - running && blocking, - std::path::MAIN_SEPARATOR != '\\' || !running, - ); + self.read_pipes(running && blocking, !cfg!(windows) || !running); if !self.fallback_status.is_empty() && self.is_sigchild_enabled() { // processInformation = fallbackStatus + processInformation (fallback keys win) @@ -996,7 +992,7 @@ impl Process { Some(pid) => pid, }; - if std::path::MAIN_SEPARATOR == '\\' { + if cfg!(windows) { let mut output: Vec<String> = Vec::new(); let mut exit_code: i64 = 0; shirabe_php_shim::exec( @@ -1189,7 +1185,7 @@ impl Process { None | Some("") => return "\"\"".to_string(), Some(a) => a, }; - if std::path::MAIN_SEPARATOR != '\\' { + if !cfg!(windows) { return format!("'{}'", argument.replace('\'', "'\\''")); } let mut argument = argument.to_string(); diff --git a/crates/shirabe/tests/util/platform_test.rs b/crates/shirabe/tests/util/platform_test.rs index 97ab0bbe..6c941ab2 100644 --- a/crates/shirabe/tests/util/platform_test.rs +++ b/crates/shirabe/tests/util/platform_test.rs @@ -31,6 +31,6 @@ fn test_expand_path() { #[test] fn test_is_windows() { // Compare 2 common tests for Windows to the built-in Windows test - assert_eq!(std::path::MAIN_SEPARATOR == '\\', Platform::is_windows()); + assert_eq!(cfg!(windows), Platform::is_windows()); assert_eq!(defined("PHP_WINDOWS_VERSION_MAJOR"), Platform::is_windows()); } |
