diff --git a/src/main.rs b/src/main.rs index d537c4df..ed9234b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -380,10 +380,7 @@ fn run() -> Result<()> { #[cfg(windows)] { if config.should_run(Step::System) { - runner.execute("Windows update", || { - powershell::Powershell::windows_powershell() - .windows_update(run_type, config.accept_all_windows_updates()) - })?; + runner.execute("Windows update", || windows::windows_update(&ctx))?; } } diff --git a/src/steps/os/windows.rs b/src/steps/os/windows.rs index aaf4b5f1..265bf273 100644 --- a/src/steps/os/windows.rs +++ b/src/steps/os/windows.rs @@ -1,5 +1,7 @@ use crate::error::SkipStep; +use crate::execution_context::ExecutionContext; use crate::executor::{CommandExt, RunType}; +use crate::powershell; use crate::terminal::print_separator; use crate::utils::require; use anyhow::Result; @@ -41,6 +43,22 @@ pub fn run_wsl_topgrade(run_type: RunType) -> Result<()> { .check_run() } +pub fn windows_update(ctx: &ExecutionContext) -> Result<()> { + let powershell = powershell::Powershell::windows_powershell(); + + if powershell.supports_windows_update() { + print_separator("Windows Update"); + return powershell.windows_update(ctx); + } + + let usoclient = require("UsoClient")?; + + print_separator("Windows Update"); + println!("Running Windows Update. Check the control panel for progress."); + ctx.run_type().execute(&usoclient).arg("ScanInstallWait").check_run()?; + ctx.run_type().execute(&usoclient).arg("StartInstall").check_run() +} + pub fn reboot() { Command::new("shutdown").args(&["/R", "/T", "0"]).spawn().ok(); } diff --git a/src/steps/powershell.rs b/src/steps/powershell.rs index bfea48b4..c6e1b903 100644 --- a/src/steps/powershell.rs +++ b/src/steps/powershell.rs @@ -1,5 +1,5 @@ #[cfg(windows)] -use crate::error::SkipStep; +use crate::execution_context::ExecutionContext; use crate::executor::{CommandExt, RunType}; use crate::terminal::{is_dumb, print_separator}; use crate::utils::{require_option, which, PathExt}; @@ -65,21 +65,30 @@ impl Powershell { } #[cfg(windows)] - pub fn windows_update(&self, run_type: RunType, accept_all_updates: bool) -> Result<()> { + pub fn supports_windows_update(&self) -> bool { + self.path + .as_ref() + .map(|p| Self::has_module(&p, "PSWindowsUpdate")) + .unwrap_or(false) + } + + #[cfg(windows)] + pub fn windows_update(&self, ctx: &ExecutionContext) -> Result<()> { let powershell = require_option(self.path.as_ref())?; - if !Self::has_module(&powershell, "PSWindowsUpdate") { - return Err(SkipStep.into()); - } - print_separator("Windows Update"); + debug_assert!(self.supports_windows_update()); - run_type + ctx.run_type() .execute(&powershell) .args(&[ "-Command", &format!( "Import-Module PSWindowsUpdate; Install-WindowsUpdate -MicrosoftUpdate {} -Verbose", - if accept_all_updates { "-AcceptAll" } else { "" } + if ctx.config().accept_all_windows_updates() { + "-AcceptAll" + } else { + "" + } ), ]) .check_run()