From b4d69e985f13064893f114339253e9185bb168eb Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Mon, 15 Dec 2025 11:08:24 -0800 Subject: [PATCH] update --- codex-rs/core/src/lib.rs | 1 + codex-rs/core/src/update_nudge.rs | 85 +------------------------ codex-rs/tui/src/lib.rs | 2 +- codex-rs/tui/src/update_action.rs | 101 ------------------------------ 4 files changed, 4 insertions(+), 185 deletions(-) delete mode 100644 codex-rs/tui/src/update_action.rs diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index da66ded50d..9c265b016c 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -95,6 +95,7 @@ pub use rollout::list::read_head_for_summary; mod function_tool; mod state; mod tasks; +pub mod update_action; mod update_nudge; mod user_notification; mod user_shell_command; diff --git a/codex-rs/core/src/update_nudge.rs b/codex-rs/core/src/update_nudge.rs index 7053c713a9..aee1380b22 100644 --- a/codex-rs/core/src/update_nudge.rs +++ b/codex-rs/core/src/update_nudge.rs @@ -1,40 +1,7 @@ -use std::path::Path; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum UpdateAction { - NpmGlobalLatest, - BunGlobalLatest, - BrewUpgrade, -} - -impl UpdateAction { - fn command_args(self) -> (&'static str, &'static [&'static str]) { - match self { - UpdateAction::NpmGlobalLatest => ("npm", &["install", "-g", "@openai/codex"]), - UpdateAction::BunGlobalLatest => ("bun", &["install", "-g", "@openai/codex"]), - UpdateAction::BrewUpgrade => ("brew", &["upgrade", "codex"]), - } - } - - fn command_str(self) -> String { - let (command, args) = self.command_args(); - shlex::try_join(std::iter::once(command).chain(args.iter().copied())) - .unwrap_or_else(|_| format!("{command} {}", args.join(" "))) - } -} +use crate::update_action::get_update_action; pub(crate) fn update_available_nudge() -> String { - let exe = std::env::current_exe().unwrap_or_default(); - let managed_by_npm = std::env::var_os("CODEX_MANAGED_BY_NPM").is_some(); - let managed_by_bun = std::env::var_os("CODEX_MANAGED_BY_BUN").is_some(); - let update_action = detect_update_action( - cfg!(target_os = "macos"), - &exe, - managed_by_npm, - managed_by_bun, - ); - - match update_action { + match get_update_action() { Some(action) => { let command = action.command_str(); format!("Update available. Run `{command}` to update.") @@ -43,51 +10,3 @@ pub(crate) fn update_available_nudge() -> String { .to_string(), } } - -fn detect_update_action( - is_macos: bool, - current_exe: &Path, - managed_by_npm: bool, - managed_by_bun: bool, -) -> Option { - if managed_by_npm { - Some(UpdateAction::NpmGlobalLatest) - } else if managed_by_bun { - Some(UpdateAction::BunGlobalLatest) - } else if is_macos - && (current_exe.starts_with("/opt/homebrew") || current_exe.starts_with("/usr/local")) - { - Some(UpdateAction::BrewUpgrade) - } else { - None - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn detects_update_action_without_env_mutation() { - assert_eq!( - detect_update_action(false, Path::new("/any/path"), false, false), - None - ); - assert_eq!( - detect_update_action(false, Path::new("/any/path"), true, false), - Some(UpdateAction::NpmGlobalLatest) - ); - assert_eq!( - detect_update_action(false, Path::new("/any/path"), false, true), - Some(UpdateAction::BunGlobalLatest) - ); - assert_eq!( - detect_update_action(true, Path::new("/opt/homebrew/bin/codex"), false, false), - Some(UpdateAction::BrewUpgrade) - ); - assert_eq!( - detect_update_action(true, Path::new("/usr/local/bin/codex"), false, false), - Some(UpdateAction::BrewUpgrade) - ); - } -} diff --git a/codex-rs/tui/src/lib.rs b/codex-rs/tui/src/lib.rs index 772eb19ee4..1c4a987d38 100644 --- a/codex-rs/tui/src/lib.rs +++ b/codex-rs/tui/src/lib.rs @@ -78,7 +78,7 @@ mod text_formatting; mod tooltips; mod tui; mod ui_consts; -pub mod update_action; +pub use codex_core::update_action; mod update_prompt; mod updates; mod version; diff --git a/codex-rs/tui/src/update_action.rs b/codex-rs/tui/src/update_action.rs deleted file mode 100644 index b5cf56a6b4..0000000000 --- a/codex-rs/tui/src/update_action.rs +++ /dev/null @@ -1,101 +0,0 @@ -/// Update action the CLI should perform after the TUI exits. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum UpdateAction { - /// Update via `npm install -g @openai/codex@latest`. - NpmGlobalLatest, - /// Update via `bun install -g @openai/codex@latest`. - BunGlobalLatest, - /// Update via `brew upgrade codex`. - BrewUpgrade, -} - -impl UpdateAction { - /// Returns the list of command-line arguments for invoking the update. - pub fn command_args(self) -> (&'static str, &'static [&'static str]) { - match self { - UpdateAction::NpmGlobalLatest => ("npm", &["install", "-g", "@openai/codex"]), - UpdateAction::BunGlobalLatest => ("bun", &["install", "-g", "@openai/codex"]), - UpdateAction::BrewUpgrade => ("brew", &["upgrade", "codex"]), - } - } - - /// Returns string representation of the command-line arguments for invoking the update. - pub fn command_str(self) -> String { - let (command, args) = self.command_args(); - shlex::try_join(std::iter::once(command).chain(args.iter().copied())) - .unwrap_or_else(|_| format!("{command} {}", args.join(" "))) - } -} - -#[cfg(not(debug_assertions))] -pub(crate) fn get_update_action() -> Option { - let exe = std::env::current_exe().unwrap_or_default(); - let managed_by_npm = std::env::var_os("CODEX_MANAGED_BY_NPM").is_some(); - let managed_by_bun = std::env::var_os("CODEX_MANAGED_BY_BUN").is_some(); - - detect_update_action( - cfg!(target_os = "macos"), - &exe, - managed_by_npm, - managed_by_bun, - ) -} - -#[cfg(any(not(debug_assertions), test))] -fn detect_update_action( - is_macos: bool, - current_exe: &std::path::Path, - managed_by_npm: bool, - managed_by_bun: bool, -) -> Option { - if managed_by_npm { - Some(UpdateAction::NpmGlobalLatest) - } else if managed_by_bun { - Some(UpdateAction::BunGlobalLatest) - } else if is_macos - && (current_exe.starts_with("/opt/homebrew") || current_exe.starts_with("/usr/local")) - { - Some(UpdateAction::BrewUpgrade) - } else { - None - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn detects_update_action_without_env_mutation() { - assert_eq!( - detect_update_action(false, std::path::Path::new("/any/path"), false, false), - None - ); - assert_eq!( - detect_update_action(false, std::path::Path::new("/any/path"), true, false), - Some(UpdateAction::NpmGlobalLatest) - ); - assert_eq!( - detect_update_action(false, std::path::Path::new("/any/path"), false, true), - Some(UpdateAction::BunGlobalLatest) - ); - assert_eq!( - detect_update_action( - true, - std::path::Path::new("/opt/homebrew/bin/codex"), - false, - false - ), - Some(UpdateAction::BrewUpgrade) - ); - assert_eq!( - detect_update_action( - true, - std::path::Path::new("/usr/local/bin/codex"), - false, - false - ), - Some(UpdateAction::BrewUpgrade) - ); - } -}