diff --git a/src/execution_context.rs b/src/execution_context.rs index cdd543e3..3f195cca 100644 --- a/src/execution_context.rs +++ b/src/execution_context.rs @@ -1,5 +1,6 @@ #![allow(dead_code)] use color_eyre::eyre::Result; +use rust_i18n::t; use std::env::var; use std::path::Path; use std::sync::{LazyLock, Mutex}; @@ -24,7 +25,7 @@ pub struct ExecutionContext<'a> { under_ssh: bool, #[cfg(target_os = "linux")] distribution: &'a Result, - powershell: LazyLock, + powershell: LazyLock>, } impl<'a> ExecutionContext<'a> { @@ -81,7 +82,11 @@ impl<'a> ExecutionContext<'a> { self.distribution } - pub fn powershell(&self) -> &Powershell { + pub fn powershell(&self) -> &Option { &self.powershell } + + pub fn require_powershell(&self) -> Result<&Powershell> { + require_option(self.powershell.as_ref(), t!("Powershell is not installed").to_string()) + } } diff --git a/src/step.rs b/src/step.rs index 9896bd79..757f15af 100644 --- a/src/step.rs +++ b/src/step.rs @@ -463,14 +463,7 @@ impl Step { PlatformioCore => runner.execute(*self, "PlatformIO Core", || generic::run_platform_io(ctx))?, Pnpm => runner.execute(*self, "pnpm", || node::run_pnpm_upgrade(ctx))?, Poetry => runner.execute(*self, "Poetry", || generic::run_poetry(ctx))?, - Powershell => { - let powershell = ctx.powershell(); - if powershell.is_available() { - runner.execute(Powershell, "Powershell Modules Update", || { - powershell.update_modules(ctx) - })?; - } - } + Powershell => runner.execute(Powershell, "Powershell Modules Update", || generic::run_powershell(ctx))?, Protonup => { #[cfg(target_os = "linux")] diff --git a/src/steps/generic.rs b/src/steps/generic.rs index a4fe9140..47d69d8a 100644 --- a/src/steps/generic.rs +++ b/src/steps/generic.rs @@ -1036,6 +1036,14 @@ pub fn run_dotnet_upgrade(ctx: &ExecutionContext) -> Result<()> { Ok(()) } +pub fn run_powershell(ctx: &ExecutionContext) -> Result<()> { + let powershell = ctx.require_powershell()?; + + print_separator(t!("Powershell Modules Update")); + + powershell.update_modules(ctx) +} + enum Hx { Helix(PathBuf), HxHexdump, diff --git a/src/steps/git.rs b/src/steps/git.rs index 0e6dc970..6621959a 100644 --- a/src/steps/git.rs +++ b/src/steps/git.rs @@ -58,9 +58,10 @@ pub fn run_git_pull(ctx: &ExecutionContext) -> Result<()> { repos.insert_if_repo(HOME_DIR.join(".dotfiles")); } - let powershell = crate::steps::powershell::Powershell::new(); - if let Some(profile) = powershell.profile() { - repos.insert_if_repo(profile); + if let Some(powershell) = ctx.powershell() { + if let Some(profile) = powershell.profile() { + repos.insert_if_repo(profile); + } } } diff --git a/src/steps/os/windows.rs b/src/steps/os/windows.rs index ad0e283a..3f19ec93 100644 --- a/src/steps/os/windows.rs +++ b/src/steps/os/windows.rs @@ -225,7 +225,7 @@ pub fn run_wsl_topgrade(ctx: &ExecutionContext) -> Result<()> { } pub fn windows_update(ctx: &ExecutionContext) -> Result<()> { - let powershell = ctx.powershell(); + let powershell = ctx.require_powershell()?; print_separator(t!("Windows Update")); @@ -241,7 +241,7 @@ pub fn windows_update(ctx: &ExecutionContext) -> Result<()> { } pub fn microsoft_store(ctx: &ExecutionContext) -> Result<()> { - let powershell = ctx.powershell(); + let powershell = ctx.require_powershell()?; print_separator(t!("Microsoft Store")); diff --git a/src/steps/powershell.rs b/src/steps/powershell.rs index 305a0ec4..5d0f1a36 100644 --- a/src/steps/powershell.rs +++ b/src/steps/powershell.rs @@ -10,23 +10,19 @@ use tracing::debug; use crate::command::CommandExt; use crate::execution_context::ExecutionContext; use crate::step::Step; -use crate::terminal::{self, print_separator}; -use crate::utils::{require_option, which, PathExt}; +use crate::terminal; +use crate::utils::{which, PathExt}; pub struct Powershell { - path: Option, + path: PathBuf, profile: Option, is_pwsh: bool, } impl Powershell { - pub fn new() -> Self { + pub fn new() -> Option { if terminal::is_dumb() { - return Self { - path: None, - profile: None, - is_pwsh: false, - }; + return None; } let (path, is_pwsh) = which("pwsh") @@ -34,39 +30,42 @@ impl Powershell { .or_else(|| which("powershell").map(|p| (Some(p), false))) .unwrap_or((None, false)); - let profile = path.as_ref().and_then(|path| Self::get_profile(path, is_pwsh)); - - Self { path, profile, is_pwsh } - } - - pub fn is_available(&self) -> bool { - self.path.is_some() + path.map(|path| { + let mut ret = Self { + path, + profile: None, + is_pwsh, + }; + ret.set_profile(); + ret + }) } pub fn profile(&self) -> Option<&PathBuf> { self.profile.as_ref() } - fn get_profile(path: &PathBuf, is_pwsh: bool) -> Option { - let profile = Self::build_command_internal(path, is_pwsh, "Split-Path $PROFILE") + fn set_profile(&mut self) { + let profile = self + .build_command_internal("Split-Path $PROFILE") .output_checked_utf8() .map(|output| output.stdout.trim().to_string()) .and_then(|s| PathBuf::from(s).require()) .ok(); debug!("Found PowerShell profile: {:?}", profile); - profile + self.profile = profile; } /// Builds an "internal" powershell command - fn build_command_internal(path: &PathBuf, is_pwsh: bool, cmd: &str) -> Command { - let mut command = Command::new(path); + fn build_command_internal(&self, cmd: &str) -> Command { + let mut command = Command::new(&self.path); command.args(["-NoProfile", "-Command"]); command.arg(cmd); // If topgrade was run from pwsh, but we are trying to run powershell, then // the inherited PSModulePath breaks module imports - if !is_pwsh { + if !self.is_pwsh { command.env_remove("PSModulePath"); } @@ -76,14 +75,13 @@ impl Powershell { /// Builds a "primary" powershell command (uses dry-run if required): /// {powershell} -NoProfile -Command {cmd} fn build_command<'a>(&self, ctx: &'a ExecutionContext, cmd: &str, use_sudo: bool) -> Result { - let powershell = require_option(self.path.as_ref(), t!("Powershell is not installed").to_string())?; let executor = &mut ctx.run_type(); let mut command = if use_sudo && ctx.sudo().is_some() { let mut cmd = executor.execute(ctx.sudo().as_ref().unwrap()); - cmd.arg(powershell); + cmd.arg(&self.path); cmd } else { - executor.execute(powershell) + executor.execute(&self.path) }; #[cfg(windows)] @@ -105,8 +103,6 @@ impl Powershell { } pub fn update_modules(&self, ctx: &ExecutionContext) -> Result<()> { - print_separator(t!("Powershell Modules Update")); - let mut cmd = "Update-Module".to_string(); if ctx.config().verbose() { @@ -146,48 +142,41 @@ impl Powershell { #[cfg(windows)] fn is_execution_policy_set(&self, policy: &str) -> bool { - if let Some(powershell) = &self.path { - // These policies are ordered from most restrictive to least restrictive - let valid_policies = ["Restricted", "AllSigned", "RemoteSigned", "Unrestricted", "Bypass"]; + // These policies are ordered from most restrictive to least restrictive + let valid_policies = ["Restricted", "AllSigned", "RemoteSigned", "Unrestricted", "Bypass"]; - // Find the index of our target policy - let target_idx = valid_policies.iter().position(|&p| p == policy); + // Find the index of our target policy + let target_idx = valid_policies.iter().position(|&p| p == policy); - let mut command = Self::build_command_internal(powershell, self.is_pwsh, "Get-ExecutionPolicy"); + let current_policy = self + .build_command_internal("Get-ExecutionPolicy") + .output_checked_utf8() + .map(|output| output.stdout.trim().to_string()); - let current_policy = command - .output_checked_utf8() - .map(|output| output.stdout.trim().to_string()); + debug!("Found PowerShell ExecutionPolicy: {:?}", current_policy); - debug!("Found PowerShell ExecutionPolicy: {:?}", current_policy); + current_policy.is_ok_and(|current_policy| { + // Find the index of the current policy + let current_idx = valid_policies.iter().position(|&p| p == current_policy); - if let Ok(current_policy) = current_policy { - // Find the index of the current policy - let current_idx = valid_policies.iter().position(|&p| p == current_policy); - - // Check if current policy exists and is at least as permissive as the target - return match (current_idx, target_idx) { - (Some(current), Some(target)) => current >= target, - _ => false, - }; + // Check if current policy exists and is at least as permissive as the target + match (current_idx, target_idx) { + (Some(current), Some(target)) => current >= target, + _ => false, } - } - false + }) } } #[cfg(windows)] impl Powershell { fn has_module(&self, module_name: &str) -> bool { - if let Some(powershell) = &self.path { - let cmd = format!("Get-Module -ListAvailable {}", module_name); + let cmd = format!("Get-Module -ListAvailable {}", module_name); - return Self::build_command_internal(powershell, self.is_pwsh, &cmd) - .output_checked() - .map(|output| !output.stdout.trim_ascii().is_empty()) - .unwrap_or(false); - } - false + self.build_command_internal(&cmd) + .output_checked() + .map(|output| !output.stdout.trim_ascii().is_empty()) + .unwrap_or(false) } pub fn supports_windows_update(&self) -> bool {