2021-12-06 14:44:20 +02:00
|
|
|
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;
|
2022-11-16 13:43:57 -05:00
|
|
|
use tracing::debug;
|
2021-12-06 14:44:20 +02:00
|
|
|
|
2022-11-08 05:54:35 -05:00
|
|
|
use crate::command::CommandExt;
|
2021-04-23 07:01:06 +03:00
|
|
|
use crate::execution_context::ExecutionContext;
|
2022-11-08 05:54:35 -05:00
|
|
|
use crate::executor::RunType;
|
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;
|
2021-05-13 21:53:13 +03:00
|
|
|
use crate::{error::SkipStep, steps::git::Repositories};
|
2021-12-06 14:44:20 +02:00
|
|
|
use crate::{powershell, Step};
|
2018-06-28 12:16:54 +03:00
|
|
|
|
2020-06-30 11:55:39 +03:00
|
|
|
pub fn run_chocolatey(ctx: &ExecutionContext) -> Result<()> {
|
2019-02-11 20:38:51 +02:00
|
|
|
let choco = require("choco")?;
|
2021-12-06 14:44:20 +02:00
|
|
|
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");
|
2020-06-30 11:55:39 +03:00
|
|
|
|
2022-11-25 17:19:32 -05:00
|
|
|
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"]);
|
2020-06-30 11:55:39 +03:00
|
|
|
|
|
|
|
|
if yes {
|
|
|
|
|
command.arg("--yes");
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 05:54:35 -05:00
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 05:54:35 -05:00
|
|
|
ctx.run_type()
|
2023-01-09 09:50:21 +02:00
|
|
|
.execute(winget)
|
2022-11-08 05:54:35 -05:00
|
|
|
.args(["upgrade", "--all"])
|
|
|
|
|
.status_checked()
|
2021-06-03 13:08:17 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-30 20:32:37 +02:00
|
|
|
pub fn run_scoop(cleanup: bool, run_type: RunType) -> 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
|
|
|
|
2022-11-08 05:54:35 -05:00
|
|
|
run_type.execute(&scoop).args(["update"]).status_checked()?;
|
|
|
|
|
run_type.execute(&scoop).args(["update", "*"]).status_checked()?;
|
2020-01-30 20:32:37 +02:00
|
|
|
|
|
|
|
|
if cleanup {
|
2022-11-08 05:54:35 -05:00
|
|
|
run_type.execute(&scoop).args(["cleanup", "*"]).status_checked()?;
|
2020-01-30 20:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2018-10-17 13:57:30 +03:00
|
|
|
}
|
|
|
|
|
|
2022-11-03 21:43:45 +00:00
|
|
|
fn get_wsl_distributions(wsl: &Path) -> Result<Vec<String>> {
|
2022-11-08 05:54:35 -05:00
|
|
|
let output = Command::new(wsl).args(["--list", "-q"]).output_checked_utf8()?.stdout;
|
2022-02-13 08:08:18 +02:00
|
|
|
Ok(output
|
|
|
|
|
.lines()
|
|
|
|
|
.filter(|s| !s.is_empty())
|
2022-11-03 18:54:40 +00:00
|
|
|
.map(|x| x.replace(['\u{0}', '\r'], ""))
|
2022-02-13 08:08:18 +02:00
|
|
|
.collect())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn upgrade_wsl_distribution(wsl: &Path, dist: &str, ctx: &ExecutionContext) -> Result<()> {
|
2022-11-03 18:54:40 +00:00
|
|
|
let topgrade = Command::new(wsl)
|
|
|
|
|
.args(["-d", dist, "bash", "-lc", "which topgrade"])
|
2022-11-08 05:54:35 -05:00
|
|
|
.output_checked_utf8()
|
2021-05-13 21:53:13 +03:00
|
|
|
.map_err(|_| SkipStep(String::from("Could not find Topgrade installed in WSL")))?;
|
|
|
|
|
|
2022-11-03 21:43:45 +00:00
|
|
|
let mut command = ctx.run_type().execute(wsl);
|
2021-05-13 21:53:13 +03:00
|
|
|
command
|
2022-11-03 18:54:40 +00:00
|
|
|
.args(["-d", dist, "bash", "-c"])
|
2022-02-13 08:08:18 +02:00
|
|
|
.arg(format!("TOPGRADE_PREFIX={} exec {}", dist, topgrade));
|
2020-08-21 21:10:54 +03:00
|
|
|
|
2021-12-06 14:44:20 +02:00
|
|
|
if ctx.config().yes(Step::Wsl) {
|
2020-08-21 21:10:54 +03:00
|
|
|
command.arg("-y");
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 05:54:35 -05:00
|
|
|
command.status_checked()
|
2019-06-04 09:35:29 +03:00
|
|
|
}
|
2019-06-13 22:05:18 +03:00
|
|
|
|
2022-02-13 08:08:18 +02:00
|
|
|
pub fn run_wsl_topgrade(ctx: &ExecutionContext) -> Result<()> {
|
|
|
|
|
let wsl = require("wsl")?;
|
2022-11-03 21:43:45 +00:00
|
|
|
let wsl_distributions = get_wsl_distributions(&wsl)?;
|
2022-02-13 08:08:18 +02:00
|
|
|
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.");
|
2022-11-08 05:54:35 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2022-11-08 05:54:35 -05: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
|
|
|
}
|
2021-02-02 22:28:22 +02:00
|
|
|
|
|
|
|
|
pub fn insert_startup_scripts(ctx: &ExecutionContext, git_repos: &mut Repositories) -> Result<()> {
|
|
|
|
|
let startup_dir = ctx
|
|
|
|
|
.base_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));
|
2021-02-02 22:28:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|