Files
topgrade/src/steps/os/windows.rs

185 lines
5.3 KiB
Rust
Raw Normal View History

use std::convert::TryFrom;
use std::path::Path;
use std::{ffi::OsStr, process::Command};
2022-11-11 09:39:29 -05:00
use color_eyre::eyre::Result;
use etcetera::base_strategy::BaseStrategy;
2022-11-16 13:43:57 -05:00
use tracing::debug;
use crate::command::CommandExt;
2021-04-23 07:01:06 +03:00
use crate::execution_context::ExecutionContext;
2022-03-05 21:58:36 +02:00
use crate::terminal::{print_separator, print_warning};
2020-07-10 11:21:19 +03:00
use crate::utils::require;
use crate::{error::SkipStep, steps::git::Repositories};
use crate::{powershell, Step};
2018-06-28 12:16:54 +03:00
pub fn run_chocolatey(ctx: &ExecutionContext) -> Result<()> {
2019-02-11 20:38:51 +02:00
let choco = require("choco")?;
let yes = ctx.config().yes(Step::Chocolatey);
2018-06-28 12:16:54 +03:00
2019-02-11 20:38:51 +02:00
print_separator("Chocolatey");
let mut command = match ctx.sudo() {
Some(sudo) => {
let mut command = ctx.run_type().execute(sudo);
command.arg(choco);
command
}
None => ctx.run_type().execute(choco),
};
command.args(["upgrade", "all"]);
if yes {
command.arg("--yes");
}
command.status_checked()
2018-06-28 12:16:54 +03:00
}
2018-08-22 22:01:06 +03:00
2021-06-03 13:08:17 +03:00
pub fn run_winget(ctx: &ExecutionContext) -> Result<()> {
let winget = require("winget")?;
print_separator("winget");
2022-03-05 21:58:36 +02:00
if !ctx.config().enable_winget() {
print_warning("Winget is disabled by default. Enable it by setting enable_winget=true in the [windows] section in the configuration.");
return Err(SkipStep(String::from("Winget is disabled by default")).into());
}
ctx.run_type()
2023-01-09 09:50:21 +02:00
.execute(winget)
.args(["upgrade", "--all"])
.status_checked()
2021-06-03 13:08:17 +03:00
}
pub fn run_scoop(cleanup: bool, ctx: &ExecutionContext) -> Result<()> {
2019-02-11 20:38:51 +02:00
let scoop = require("scoop")?;
2018-10-17 13:57:30 +03:00
2019-02-11 20:38:51 +02:00
print_separator("Scoop");
2018-10-17 13:57:30 +03:00
ctx.run_type().execute(&scoop).args(["update"]).status_checked()?;
ctx.run_type().execute(&scoop).args(["update", "*"]).status_checked()?;
if cleanup {
ctx.run_type().execute(&scoop).args(["cleanup", "*"]).status_checked()?;
}
Ok(())
2018-10-17 13:57:30 +03:00
}
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
pub fn update_wsl(ctx: &ExecutionContext) -> Result<()> {
let wsl = require("wsl")?;
print_separator("Update WSL");
let mut wsl_command = ctx.run_type().execute(wsl);
wsl_command.args(["--update"]);
if ctx.config().wsl_update_pre_release() {
wsl_command.args(["--pre-release"]);
}
if ctx.config().wsl_update_use_web_download() {
wsl_command.args(["--web-download"]);
}
wsl_command.status_checked()?;
Ok(())
}
fn get_wsl_distributions(wsl: &Path) -> Result<Vec<String>> {
let output = Command::new(wsl).args(["--list", "-q"]).output_checked_utf8()?.stdout;
Ok(output
.lines()
.filter(|s| !s.is_empty())
.map(|x| x.replace(['\u{0}', '\r'], ""))
.collect())
}
fn upgrade_wsl_distribution(wsl: &Path, dist: &str, ctx: &ExecutionContext) -> Result<()> {
let topgrade = Command::new(wsl)
.args(["-d", dist, "bash", "-lc", "which topgrade"])
.output_checked_utf8()
.map_err(|_| SkipStep(String::from("Could not find Topgrade installed in WSL")))?;
let mut command = ctx.run_type().execute(wsl);
command
.args(["-d", dist, "bash", "-c"])
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
.arg(format!("TOPGRADE_PREFIX={dist} exec {topgrade}"));
2020-08-21 21:10:54 +03:00
if ctx.config().yes(Step::Wsl) {
2020-08-21 21:10:54 +03:00
command.arg("-y");
}
command.status_checked()
}
2019-06-13 22:05:18 +03:00
pub fn run_wsl_topgrade(ctx: &ExecutionContext) -> Result<()> {
let wsl = require("wsl")?;
let wsl_distributions = get_wsl_distributions(&wsl)?;
let mut ran = false;
debug!("WSL distributions: {:?}", wsl_distributions);
for distribution in wsl_distributions {
let result = upgrade_wsl_distribution(&wsl, &distribution, ctx);
debug!("Upgrading {:?}: {:?}", distribution, result);
if let Err(e) = result {
if e.is::<SkipStep>() {
continue;
}
}
ran = true
}
if ran {
Ok(())
} else {
Err(SkipStep(String::from("Could not find Topgrade in any WSL disribution")).into())
}
}
2020-02-27 22:06:14 +02:00
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")
.status_checked()?;
ctx.run_type().execute(&usoclient).arg("StartInstall").status_checked()
2020-02-27 22:06:14 +02:00
}
pub fn reboot() -> Result<()> {
// If this works, it won't return, but if it doesn't work, it may return a useful error
// message.
Command::new("shutdown").args(["/R", "/T", "0"]).status_checked()
2019-06-13 22:05:18 +03:00
}
pub fn insert_startup_scripts(git_repos: &mut Repositories) -> Result<()> {
let startup_dir = crate::WINDOWS_DIRS
.data_dir()
.join("Microsoft\\Windows\\Start Menu\\Programs\\Startup");
2021-05-08 22:50:42 +03:00
for entry in std::fs::read_dir(&startup_dir)?.flatten() {
let path = entry.path();
if path.extension().and_then(OsStr::to_str) == Some("lnk") {
if let Ok(lnk) = parselnk::Lnk::try_from(Path::new(&path)) {
debug!("Startup link: {:?}", lnk);
if let Some(path) = lnk.relative_path() {
git_repos.insert_if_repo(&startup_dir.join(path));
}
}
}
}
Ok(())
}