diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-05-10 15:29:19 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-05-10 15:29:19 +0900 |
| commit | 46845eff8d1398f35099a0ef914f77bcaf473287 (patch) | |
| tree | 12c4850f1d2f438d0ba6c363fdc0e5036cd4601d /crates/mozart/src/commands/self_update.rs | |
| parent | 212506c364b2342dd9e5fa789e8cff38835dfe52 (diff) | |
| download | php-mozart-46845eff8d1398f35099a0ef914f77bcaf473287.tar.gz php-mozart-46845eff8d1398f35099a0ef914f77bcaf473287.tar.zst php-mozart-46845eff8d1398f35099a0ef914f77bcaf473287.zip | |
refactor(io): introduce IoInterface trait mirroring Composer IOInterface
Add an `IoInterface` trait in mozart-core::console that mirrors
`\Composer\IO\IOInterface`, implement it for `Console`, and switch
commands, the auditor, and the suggested-packages reporter to accept
the abstracted IO (typically `Arc<Mutex<Box<dyn IoInterface>>>` at the
command boundary, `&dyn IoInterface` deeper down) instead of
`&Console`. The console_writeln\!/write\! macros now go through
`IoInterface::verbosity()` via the lock so any implementor works.
Diffstat (limited to 'crates/mozart/src/commands/self_update.rs')
| -rw-r--r-- | crates/mozart/src/commands/self_update.rs | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/crates/mozart/src/commands/self_update.rs b/crates/mozart/src/commands/self_update.rs index a326914..4b6c27a 100644 --- a/crates/mozart/src/commands/self_update.rs +++ b/crates/mozart/src/commands/self_update.rs @@ -1,5 +1,6 @@ use clap::Args; use mozart_core::MOZART_VERSION; +use mozart_core::console::IoInterface; use mozart_core::console_writeln; use std::io::Write; use std::path::{Path, PathBuf}; @@ -47,7 +48,7 @@ const BACKUP_EXTENSION: &str = ".old"; pub async fn execute( args: &SelfUpdateArgs, _cli: &super::Cli, - console: &mozart_core::console::Console, + io: std::sync::Arc<std::sync::Mutex<Box<dyn IoInterface>>>, ) -> anyhow::Result<()> { let current_exe = std::env::current_exe() .map_err(|e| anyhow::anyhow!("Could not determine current executable path: {e}"))?; @@ -61,9 +62,9 @@ pub async fn execute( })?; if args.rollback { - rollback(¤t_exe, &data_dir, console) + rollback(¤t_exe, &data_dir, io.clone()) } else { - update(args, ¤t_exe, &data_dir, console).await + update(args, ¤t_exe, &data_dir, &io).await } } @@ -203,7 +204,7 @@ async fn download_asset( asset: &GitHubAsset, dest: &Path, show_progress: bool, - console: &mozart_core::console::Console, + io: std::sync::Arc<std::sync::Mutex<Box<dyn IoInterface>>>, ) -> anyhow::Result<()> { let client = mozart_core::http::client_builder() .timeout(std::time::Duration::from_secs(300)) @@ -248,7 +249,7 @@ async fn download_asset( } if show_progress && total_bytes > 0 { - console.info(""); // newline after progress + io.lock().unwrap().info(""); // newline after progress } Ok(()) @@ -258,7 +259,7 @@ async fn update( args: &SelfUpdateArgs, current_exe: &Path, data_dir: &Path, - console: &mozart_core::console::Console, + io: &std::sync::Arc<std::sync::Mutex<Box<dyn IoInterface>>>, ) -> anyhow::Result<()> { let current_version = MOZART_VERSION; let channel = effective_channel(args.preview); @@ -278,7 +279,7 @@ async fn update( // If no explicit version was requested and we're already up-to-date, bail early if args.version.is_none() && target_version == current_version { console_writeln!( - console, + io, "<info>You are already using the latest available Mozart version {current_version} ({channel} channel).</info>" ); @@ -286,13 +287,13 @@ async fn update( // Preserve the most recent backup let latest = find_latest_backup(data_dir).ok(); clean_backups(data_dir, latest.as_deref())?; - console_writeln!(console, "<comment>Old backups removed.</comment>"); + console_writeln!(io, "<comment>Old backups removed.</comment>"); } return Ok(()); } - console.info(&format!( + io.lock().unwrap().info(&format!( "Upgrading to version {target_version} ({channel} channel)." )); @@ -300,7 +301,7 @@ async fn update( let asset_name = platform_asset_name()?; let asset = find_asset(target_release, &asset_name)?; - console.info(&format!( + io.lock().unwrap().info(&format!( "Downloading {} ({} bytes)...", asset.name, asset.size )); @@ -312,7 +313,7 @@ async fn update( .map_err(|e| anyhow::anyhow!("Could not create temporary file: {e}"))?; let tmp_path = tmp.path().to_path_buf(); - download_asset(asset, &tmp_path, !args.no_progress, console).await?; + download_asset(asset, &tmp_path, !args.no_progress, io.clone()).await?; // Set executable permission on Unix #[cfg(unix)] @@ -342,16 +343,16 @@ async fn update( drop(tmp); console_writeln!( - console, + io, "<info>Mozart updated successfully from {current_version} to {target_version}</info>" ); - console.info(&format!( + io.lock().unwrap().info(&format!( "Use `mozart self-update --rollback` to return to version {current_version}" )); if args.clean_backups { clean_backups(data_dir, Some(&backup_path))?; - console_writeln!(console, "<comment>Old backups removed.</comment>"); + console_writeln!(io, "<comment>Old backups removed.</comment>"); } Ok(()) @@ -360,12 +361,14 @@ async fn update( fn rollback( current_exe: &Path, data_dir: &Path, - console: &mozart_core::console::Console, + io: std::sync::Arc<std::sync::Mutex<Box<dyn IoInterface>>>, ) -> anyhow::Result<()> { let backup = find_latest_backup(data_dir)?; let backup_version = version_from_backup(&backup); - console.info(&format!("Rolling back to version {backup_version}...")); + io.lock() + .unwrap() + .info(&format!("Rolling back to version {backup_version}...")); // Set executable permission on Unix before replacing #[cfg(unix)] @@ -381,7 +384,7 @@ fn rollback( .map_err(|e| anyhow::anyhow!("Could not restore backup: {e}"))?; console_writeln!( - console, + io, "<info>Rollback successful. Restored version {backup_version}</info>", ); |
