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

121 lines
3.3 KiB
Rust
Raw Normal View History

use std::convert::TryFrom;
use std::path::Path;
use std::{ffi::OsStr, process::Command};
use anyhow::Result;
use log::debug;
2021-04-23 07:01:06 +03:00
use crate::execution_context::ExecutionContext;
use crate::executor::{CommandExt, RunType};
2019-08-14 21:36:57 +03:00
use crate::terminal::print_separator;
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");
2020-07-10 11:21:19 +03:00
let mut cmd = &choco;
let mut args = vec!["upgrade", "all"];
2020-07-10 11:21:19 +03:00
if let Some(sudo) = ctx.sudo() {
cmd = sudo;
args.insert(0, "choco");
}
let mut command = ctx.run_type().execute(&cmd);
command.args(&args);
if yes {
command.arg("--yes");
}
command.check_run()
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");
2021-10-28 22:05:35 +03:00
ctx.run_type().execute(&winget).args(&["upgrade", "--all"]).check_run()
2021-06-03 13:08:17 +03: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
2021-10-28 22:05:35 +03:00
run_type.execute(&scoop).args(&["update"]).check_run()?;
run_type.execute(&scoop).args(&["update", "*"]).check_run()?;
if cleanup {
2021-10-28 22:05:35 +03:00
run_type.execute(&scoop).args(&["cleanup", "*"]).check_run()?;
}
Ok(())
2018-10-17 13:57:30 +03:00
}
2020-08-21 21:10:54 +03:00
pub fn run_wsl_topgrade(ctx: &ExecutionContext) -> Result<()> {
let wsl = require("wsl")?;
let topgrade = Command::new(&wsl)
2021-10-28 22:05:35 +03:00
.args(&["bash", "-lc", "which topgrade"])
.check_output()
.map_err(|_| SkipStep(String::from("Could not find Topgrade installed in WSL")))?;
2020-08-21 21:10:54 +03:00
let mut command = ctx.run_type().execute(&wsl);
command
2021-10-28 22:05:35 +03:00
.args(&["bash", "-c"])
.arg(format!("TOPGRADE_PREFIX=WSL 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.check_run()
}
2019-06-13 22:05:18 +03:00
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").check_run()?;
ctx.run_type().execute(&usoclient).arg("StartInstall").check_run()
}
2019-06-13 22:05:18 +03:00
pub fn reboot() {
2021-10-28 22:05:35 +03:00
Command::new("shutdown").args(&["/R", "/T", "0"]).spawn().ok();
2019-06-13 22:05:18 +03: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));
}
}
}
}
Ok(())
}