refactor(powershell): store powershell path directly

This commit is contained in:
Andre Toerien
2025-07-17 15:13:29 +02:00
committed by Gideon
parent b166aae835
commit 3f9fe845e5
6 changed files with 67 additions and 71 deletions

View File

@@ -1,5 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use rust_i18n::t;
use std::env::var; use std::env::var;
use std::path::Path; use std::path::Path;
use std::sync::{LazyLock, Mutex}; use std::sync::{LazyLock, Mutex};
@@ -24,7 +25,7 @@ pub struct ExecutionContext<'a> {
under_ssh: bool, under_ssh: bool,
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
distribution: &'a Result<Distribution>, distribution: &'a Result<Distribution>,
powershell: LazyLock<Powershell>, powershell: LazyLock<Option<Powershell>>,
} }
impl<'a> ExecutionContext<'a> { impl<'a> ExecutionContext<'a> {
@@ -81,7 +82,11 @@ impl<'a> ExecutionContext<'a> {
self.distribution self.distribution
} }
pub fn powershell(&self) -> &Powershell { pub fn powershell(&self) -> &Option<Powershell> {
&self.powershell &self.powershell
} }
pub fn require_powershell(&self) -> Result<&Powershell> {
require_option(self.powershell.as_ref(), t!("Powershell is not installed").to_string())
}
} }

View File

@@ -463,14 +463,7 @@ impl Step {
PlatformioCore => runner.execute(*self, "PlatformIO Core", || generic::run_platform_io(ctx))?, PlatformioCore => runner.execute(*self, "PlatformIO Core", || generic::run_platform_io(ctx))?,
Pnpm => runner.execute(*self, "pnpm", || node::run_pnpm_upgrade(ctx))?, Pnpm => runner.execute(*self, "pnpm", || node::run_pnpm_upgrade(ctx))?,
Poetry => runner.execute(*self, "Poetry", || generic::run_poetry(ctx))?, Poetry => runner.execute(*self, "Poetry", || generic::run_poetry(ctx))?,
Powershell => { Powershell => runner.execute(Powershell, "Powershell Modules Update", || generic::run_powershell(ctx))?,
let powershell = ctx.powershell();
if powershell.is_available() {
runner.execute(Powershell, "Powershell Modules Update", || {
powershell.update_modules(ctx)
})?;
}
}
Protonup => Protonup =>
{ {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]

View File

@@ -1036,6 +1036,14 @@ pub fn run_dotnet_upgrade(ctx: &ExecutionContext) -> Result<()> {
Ok(()) 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 { enum Hx {
Helix(PathBuf), Helix(PathBuf),
HxHexdump, HxHexdump,

View File

@@ -58,9 +58,10 @@ pub fn run_git_pull(ctx: &ExecutionContext) -> Result<()> {
repos.insert_if_repo(HOME_DIR.join(".dotfiles")); repos.insert_if_repo(HOME_DIR.join(".dotfiles"));
} }
let powershell = crate::steps::powershell::Powershell::new(); if let Some(powershell) = ctx.powershell() {
if let Some(profile) = powershell.profile() { if let Some(profile) = powershell.profile() {
repos.insert_if_repo(profile); repos.insert_if_repo(profile);
}
} }
} }

View File

@@ -225,7 +225,7 @@ pub fn run_wsl_topgrade(ctx: &ExecutionContext) -> Result<()> {
} }
pub fn windows_update(ctx: &ExecutionContext) -> Result<()> { pub fn windows_update(ctx: &ExecutionContext) -> Result<()> {
let powershell = ctx.powershell(); let powershell = ctx.require_powershell()?;
print_separator(t!("Windows Update")); print_separator(t!("Windows Update"));
@@ -241,7 +241,7 @@ pub fn windows_update(ctx: &ExecutionContext) -> Result<()> {
} }
pub fn microsoft_store(ctx: &ExecutionContext) -> Result<()> { pub fn microsoft_store(ctx: &ExecutionContext) -> Result<()> {
let powershell = ctx.powershell(); let powershell = ctx.require_powershell()?;
print_separator(t!("Microsoft Store")); print_separator(t!("Microsoft Store"));

View File

@@ -10,23 +10,19 @@ use tracing::debug;
use crate::command::CommandExt; use crate::command::CommandExt;
use crate::execution_context::ExecutionContext; use crate::execution_context::ExecutionContext;
use crate::step::Step; use crate::step::Step;
use crate::terminal::{self, print_separator}; use crate::terminal;
use crate::utils::{require_option, which, PathExt}; use crate::utils::{which, PathExt};
pub struct Powershell { pub struct Powershell {
path: Option<PathBuf>, path: PathBuf,
profile: Option<PathBuf>, profile: Option<PathBuf>,
is_pwsh: bool, is_pwsh: bool,
} }
impl Powershell { impl Powershell {
pub fn new() -> Self { pub fn new() -> Option<Self> {
if terminal::is_dumb() { if terminal::is_dumb() {
return Self { return None;
path: None,
profile: None,
is_pwsh: false,
};
} }
let (path, is_pwsh) = which("pwsh") let (path, is_pwsh) = which("pwsh")
@@ -34,39 +30,42 @@ impl Powershell {
.or_else(|| which("powershell").map(|p| (Some(p), false))) .or_else(|| which("powershell").map(|p| (Some(p), false)))
.unwrap_or((None, false)); .unwrap_or((None, false));
let profile = path.as_ref().and_then(|path| Self::get_profile(path, is_pwsh)); path.map(|path| {
let mut ret = Self {
Self { path, profile, is_pwsh } path,
} profile: None,
is_pwsh,
pub fn is_available(&self) -> bool { };
self.path.is_some() ret.set_profile();
ret
})
} }
pub fn profile(&self) -> Option<&PathBuf> { pub fn profile(&self) -> Option<&PathBuf> {
self.profile.as_ref() self.profile.as_ref()
} }
fn get_profile(path: &PathBuf, is_pwsh: bool) -> Option<PathBuf> { fn set_profile(&mut self) {
let profile = Self::build_command_internal(path, is_pwsh, "Split-Path $PROFILE") let profile = self
.build_command_internal("Split-Path $PROFILE")
.output_checked_utf8() .output_checked_utf8()
.map(|output| output.stdout.trim().to_string()) .map(|output| output.stdout.trim().to_string())
.and_then(|s| PathBuf::from(s).require()) .and_then(|s| PathBuf::from(s).require())
.ok(); .ok();
debug!("Found PowerShell profile: {:?}", profile); debug!("Found PowerShell profile: {:?}", profile);
profile self.profile = profile;
} }
/// Builds an "internal" powershell command /// Builds an "internal" powershell command
fn build_command_internal(path: &PathBuf, is_pwsh: bool, cmd: &str) -> Command { fn build_command_internal(&self, cmd: &str) -> Command {
let mut command = Command::new(path); let mut command = Command::new(&self.path);
command.args(["-NoProfile", "-Command"]); command.args(["-NoProfile", "-Command"]);
command.arg(cmd); command.arg(cmd);
// If topgrade was run from pwsh, but we are trying to run powershell, then // If topgrade was run from pwsh, but we are trying to run powershell, then
// the inherited PSModulePath breaks module imports // the inherited PSModulePath breaks module imports
if !is_pwsh { if !self.is_pwsh {
command.env_remove("PSModulePath"); command.env_remove("PSModulePath");
} }
@@ -76,14 +75,13 @@ impl Powershell {
/// Builds a "primary" powershell command (uses dry-run if required): /// Builds a "primary" powershell command (uses dry-run if required):
/// {powershell} -NoProfile -Command {cmd} /// {powershell} -NoProfile -Command {cmd}
fn build_command<'a>(&self, ctx: &'a ExecutionContext, cmd: &str, use_sudo: bool) -> Result<impl CommandExt + 'a> { fn build_command<'a>(&self, ctx: &'a ExecutionContext, cmd: &str, use_sudo: bool) -> Result<impl CommandExt + 'a> {
let powershell = require_option(self.path.as_ref(), t!("Powershell is not installed").to_string())?;
let executor = &mut ctx.run_type(); let executor = &mut ctx.run_type();
let mut command = if use_sudo && ctx.sudo().is_some() { let mut command = if use_sudo && ctx.sudo().is_some() {
let mut cmd = executor.execute(ctx.sudo().as_ref().unwrap()); let mut cmd = executor.execute(ctx.sudo().as_ref().unwrap());
cmd.arg(powershell); cmd.arg(&self.path);
cmd cmd
} else { } else {
executor.execute(powershell) executor.execute(&self.path)
}; };
#[cfg(windows)] #[cfg(windows)]
@@ -105,8 +103,6 @@ impl Powershell {
} }
pub fn update_modules(&self, ctx: &ExecutionContext) -> Result<()> { pub fn update_modules(&self, ctx: &ExecutionContext) -> Result<()> {
print_separator(t!("Powershell Modules Update"));
let mut cmd = "Update-Module".to_string(); let mut cmd = "Update-Module".to_string();
if ctx.config().verbose() { if ctx.config().verbose() {
@@ -146,48 +142,41 @@ impl Powershell {
#[cfg(windows)] #[cfg(windows)]
fn is_execution_policy_set(&self, policy: &str) -> bool { 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
// These policies are ordered from most restrictive to least restrictive let valid_policies = ["Restricted", "AllSigned", "RemoteSigned", "Unrestricted", "Bypass"];
let valid_policies = ["Restricted", "AllSigned", "RemoteSigned", "Unrestricted", "Bypass"];
// Find the index of our target policy // Find the index of our target policy
let target_idx = valid_policies.iter().position(|&p| p == 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 debug!("Found PowerShell ExecutionPolicy: {:?}", current_policy);
.output_checked_utf8()
.map(|output| output.stdout.trim().to_string());
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 { // Check if current policy exists and is at least as permissive as the target
// Find the index of the current policy match (current_idx, target_idx) {
let current_idx = valid_policies.iter().position(|&p| p == current_policy); (Some(current), Some(target)) => current >= target,
_ => false,
// 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,
};
} }
} })
false
} }
} }
#[cfg(windows)] #[cfg(windows)]
impl Powershell { impl Powershell {
fn has_module(&self, module_name: &str) -> bool { 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) self.build_command_internal(&cmd)
.output_checked() .output_checked()
.map(|output| !output.stdout.trim_ascii().is_empty()) .map(|output| !output.stdout.trim_ascii().is_empty())
.unwrap_or(false); .unwrap_or(false)
}
false
} }
pub fn supports_windows_update(&self) -> bool { pub fn supports_windows_update(&self) -> bool {