Files
topgrade/src/steps/powershell.rs

123 lines
3.5 KiB
Rust
Raw Normal View History

2021-04-07 13:32:58 +03:00
#[cfg(windows)]
use std::path::Path;
use std::path::PathBuf;
2019-08-14 21:36:57 +03:00
use std::process::Command;
2022-11-11 09:39:29 -05:00
use color_eyre::eyre::Result;
use rust_i18n::t;
use crate::command::CommandExt;
use crate::execution_context::ExecutionContext;
use crate::terminal::{is_dumb, print_separator};
use crate::utils::{require_option, which, PathExt};
use crate::Step;
2019-08-14 21:36:57 +03:00
pub struct Powershell {
path: Option<PathBuf>,
profile: Option<PathBuf>,
}
impl Powershell {
/// Returns a powershell instance.
///
/// If the powershell binary is not found, or the current terminal is dumb
/// then the instance of this struct will skip all the powershell steps.
pub fn new() -> Self {
let path = which("pwsh").or_else(|| which("powershell")).filter(|_| !is_dumb());
let profile = path.as_ref().and_then(|path| {
Command::new(path)
.args(["-NoProfile", "-Command", "Split-Path $profile"])
.output_checked_utf8()
.map(|output| PathBuf::from(output.stdout.trim()))
2019-08-14 21:36:57 +03:00
.and_then(|p| p.require())
.ok()
});
Powershell { path, profile }
}
#[cfg(windows)]
pub fn windows_powershell() -> Self {
Powershell {
path: which("powershell").filter(|_| !is_dumb()),
profile: None,
}
}
2019-08-14 21:36:57 +03:00
#[cfg(windows)]
2021-04-07 10:23:26 +03:00
pub fn has_module(powershell: &Path, command: &str) -> bool {
Command::new(powershell)
.args([
"-NoProfile",
"-Command",
v10.2.5 release (#330) * Don't show desktop notification on error (if `skip_notify = true`) (#275) * Use ─ (U+2500) to draw borders (#282) * Adds Pclinuxos support (#283) * Add Devkitpro Pacman support (#291) * Added support for Neovim package manager lazy.nvim (#293) * Added support for lazy.nvim From https://github.com/folke/lazy.nvim Authored-by: Jacob Lane Ledbetter <jledbetter460@gmail.com> * Make garuda-update update AUR packages by default (#296) * fix(#298): Don't throw error if no Helm repository found (#305) * Skip .NET when `dotnet tool list` is not successful (#302) * feat(pacstall): add `-y` flag variant (#312) * Add openSUSE MicroOS support (#315) * Adds notify-send timeout of 10s (#318) * Don't run yum when rpm-ostree is available (#313) * don't run yum when rpm-ostree is available * Clippy fix * rpm-ostree: set default value to true * Fixes if loop error * Fixes gem update --system requires sudo now (#317) * Fixes gem update --system requires sudo now * rubygem: Adds arg -EH to sudo * Use fixed nala path instead of which(nala) (#314) * Adds notify-send bug warning when topgrade is run (#324) * Adds notify-send bug warning when topgrade is run * fix typo + clippy * notify-send warning respects skip_notify flag * nix: Adds additional arguments support (#325) * Adds pip-review and pipupgrade support (#316) * Adds pip-review and pipupgrade support * Python: fixes pip_review and pipupgrade * v10.2.5 patch (#329) * WSL: Adds new wsl --update flags (#327) * wsl: Updates available flags * Clippy fix * Add WslUpdate runner * wsl: Code Typo * wsl: Code Typos * wsl: Code Typos * wsl: Code Typo * Adds AM Package Manager (#328) * Adds AM Package Manager * Clippy fixes * Cargo fmt * Moves am to linux only in main file --------- Co-authored-by: Guilherme Silva <626206+guihkx@users.noreply.github.com> Co-authored-by: Gabriel Augendre <gabriel@augendre.info> Co-authored-by: Cat Core <34719527+arthurbambou@users.noreply.github.com> Co-authored-by: Hugo Haas <hugoh@hugoh.net> Co-authored-by: Baptiste <32563450+BapRx@users.noreply.github.com> Co-authored-by: bbx0 <39773919+bbx0@users.noreply.github.com> Co-authored-by: Sourajyoti Basak <wiz28@protonmail.com>
2023-01-29 19:19:27 +00:00
&format!("Get-Module -ListAvailable {command}"),
])
.output_checked_utf8()
.map(|result| !result.stdout.is_empty())
2019-08-15 09:20:31 +03:00
.unwrap_or(false)
2019-08-14 21:36:57 +03:00
}
pub fn profile(&self) -> Option<&PathBuf> {
self.profile.as_ref()
}
2020-07-15 08:47:15 +03:00
pub fn update_modules(&self, ctx: &ExecutionContext) -> Result<()> {
let powershell = require_option(self.path.as_ref(), t!("Powershell is not installed").to_string())?;
2019-08-14 21:36:57 +03:00
print_separator(t!("Powershell Modules Update"));
2020-07-15 08:47:15 +03:00
2020-08-23 09:10:09 +03:00
let mut cmd = vec!["Update-Module"];
if ctx.config().verbose() {
cmd.push("-Verbose")
}
2020-07-15 08:47:15 +03:00
if ctx.config().yes(Step::Powershell) {
cmd.push("-Force")
2021-04-24 06:07:50 +03:00
}
println!("{}", t!("Updating modules..."));
2020-08-23 09:10:09 +03:00
ctx.run_type()
.execute(powershell)
// This probably doesn't need `shell_words::join`.
.args(["-NoProfile", "-Command", &cmd.join(" ")])
.status_checked()
2019-08-14 21:36:57 +03:00
}
#[cfg(windows)]
2020-02-27 22:06:14 +02:00
pub fn supports_windows_update(&self) -> bool {
self.path
.as_ref()
2021-09-02 13:14:41 +00:00
.map(|p| Self::has_module(p, "PSWindowsUpdate"))
2020-02-27 22:06:14 +02:00
.unwrap_or(false)
}
#[cfg(windows)]
pub fn windows_update(&self, ctx: &ExecutionContext) -> Result<()> {
let powershell = require_option(self.path.as_ref(), t!("Powershell is not installed").to_string())?;
2020-02-27 22:06:14 +02:00
debug_assert!(self.supports_windows_update());
2019-08-14 21:36:57 +03:00
let accept_all = if ctx.config().accept_all_windows_updates() {
"-AcceptAll"
} else {
""
};
let install_windowsupdate_verbose = "Install-WindowsUpdate -Verbose".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)
};
command
.args(["-NoProfile", &install_windowsupdate_verbose, accept_all])
.status_checked()
2019-08-14 21:36:57 +03:00
}
}