From ea2f3e07e96e219d5e68d104b9e8ee919666ff7c Mon Sep 17 00:00:00 2001 From: Andreas02-dev <69248785+Andreas02-dev@users.noreply.github.com> Date: Wed, 23 Oct 2024 02:15:46 +0200 Subject: [PATCH] feat(microsoft_store): Add Microsoft Store step for Windows (#963) * feat(microsoft_store): Add Microsoft Store step for Windows Add Microsoft Store Apps update step for Windows as Winget cannot update all Microsoft Store apps yet. Closes #912 * style(translation): modify `zh_TW` translation --- locales/app.yml | 18 +++++++++++++++++- src/config.rs | 1 + src/main.rs | 3 +++ src/steps/os/windows.rs | 8 ++++++++ src/steps/powershell.rs | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) diff --git a/locales/app.yml b/locales/app.yml index 265b9bd9..d4a58514 100644 --- a/locales/app.yml +++ b/locales/app.yml @@ -621,4 +621,20 @@ _version: 2 "Would upgrade OpenBSD packages": en: "Would upgrade OpenBSD packages" es: "Actualizaría los paquetes de OpenBSD" - zh_TW: "會升級 OpenBSD 套件" \ No newline at end of file + zh_TW: "會升級 OpenBSD 套件" +"Microsoft Store": + en: "Microsoft Store" + es: "Tienda de Microsoft" + zh_TW: "Microsoft Store" +"Scanning for updates...": + en: "Scanning for updates..." + es: "Buscando actualizaciones..." + zh_TW: "正在掃描更新..." +"Success, Microsoft Store apps are being updated in the background": + en: "Success, Microsoft Store apps are being updated in the background" + es: "Éxito, las aplicaciones de Microsoft Store se están actualizando en segundo plano" + zh_TW: "成功,Microsoft Store 應用程式正在後台更新" +"Unable to update Microsoft Store apps, manual intervention is required": + en: "Unable to update Microsoft Store apps, manual intervention is required" + es: "No se pueden actualizar las aplicaciones de Microsoft Store, se requiere intervención manual" + zh_TW: "無法更新 Microsoft Store 應用,需手動幹預" diff --git a/src/config.rs b/src/config.rs index 0991e284..6dbac9bf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -111,6 +111,7 @@ pub enum Step { Mas, Maza, Micro, + MicrosoftStore, Mise, Myrepos, Nix, diff --git a/src/main.rs b/src/main.rs index 1166615a..63551426 100644 --- a/src/main.rs +++ b/src/main.rs @@ -206,6 +206,9 @@ fn run() -> Result<()> { runner.execute(Step::Scoop, "Scoop", || windows::run_scoop(&ctx))?; runner.execute(Step::Winget, "Winget", || windows::run_winget(&ctx))?; runner.execute(Step::System, "Windows update", || windows::windows_update(&ctx))?; + runner.execute(Step::MicrosoftStore, "Microsoft Store", || { + windows::microsoft_store(&ctx) + })?; } #[cfg(target_os = "linux")] diff --git a/src/steps/os/windows.rs b/src/steps/os/windows.rs index c5ff220d..575ec7ca 100644 --- a/src/steps/os/windows.rs +++ b/src/steps/os/windows.rs @@ -221,6 +221,14 @@ pub fn windows_update(ctx: &ExecutionContext) -> Result<()> { } } +pub fn microsoft_store(ctx: &ExecutionContext) -> Result<()> { + let powershell = powershell::Powershell::windows_powershell(); + + print_separator(t!("Microsoft Store")); + + powershell.microsoft_store(ctx) +} + pub fn reboot() -> Result<()> { // If this works, it won't return, but if it doesn't work, it may return a useful error // message. diff --git a/src/steps/powershell.rs b/src/steps/powershell.rs index 7bd04036..bf81b3ef 100644 --- a/src/steps/powershell.rs +++ b/src/steps/powershell.rs @@ -119,4 +119,43 @@ impl Powershell { .args(["-NoProfile", &install_windowsupdate_verbose, accept_all]) .status_checked() } + + #[cfg(windows)] + pub fn microsoft_store(&self, ctx: &ExecutionContext) -> Result<()> { + let powershell = require_option(self.path.as_ref(), t!("Powershell is not installed").to_string())?; + + let mut command = if let Some(sudo) = ctx.sudo() { + let mut command = ctx.run_type().execute(sudo); + command.arg(powershell); + command + } else { + ctx.run_type().execute(powershell) + }; + + println!("{}", t!("Scanning for updates...")); + + // Scan for updates using the MDM UpdateScanMethod + // This method is also available for non-MDM devices + let update_command = "(Get-CimInstance -Namespace \"Root\\cimv2\\mdm\\dmmap\" -ClassName \"MDM_EnterpriseModernAppManagement_AppManagement01\" | Invoke-CimMethod -MethodName UpdateScanMethod).ReturnValue"; + + command.args(["-NoProfile", update_command]); + + command + .output_checked_with_utf8(|output| { + if output.stdout.trim() == "0" { + println!( + "{}", + t!("Success, Microsoft Store apps are being updated in the background") + ); + Ok(()) + } else { + println!( + "{}", + t!("Unable to update Microsoft Store apps, manual intervention is required") + ); + Err(()) + } + }) + .map(|_| ()) + } }