2019-02-11 20:38:51 +02:00
|
|
|
use crate::error::{Error, ErrorKind};
|
2019-01-01 22:22:07 +02:00
|
|
|
use crate::executor::{CommandExt, RunType};
|
2019-01-21 20:12:56 +02:00
|
|
|
use crate::terminal::{is_dumb, print_separator};
|
2019-05-25 21:28:16 +03:00
|
|
|
use crate::utils::{require, require_option, which, PathExt};
|
2018-08-22 22:01:06 +03:00
|
|
|
use std::path::PathBuf;
|
2018-06-28 12:16:54 +03:00
|
|
|
use std::process::Command;
|
|
|
|
|
|
2019-02-11 20:38:51 +02:00
|
|
|
pub fn run_chocolatey(run_type: RunType) -> Result<(), Error> {
|
|
|
|
|
let choco = require("choco")?;
|
2018-06-28 12:16:54 +03:00
|
|
|
|
2019-02-11 20:38:51 +02:00
|
|
|
print_separator("Chocolatey");
|
|
|
|
|
run_type.execute(&choco).args(&["upgrade", "all"]).check_run()
|
2018-06-28 12:16:54 +03:00
|
|
|
}
|
2018-08-22 22:01:06 +03:00
|
|
|
|
2019-02-11 20:38:51 +02:00
|
|
|
pub fn run_scoop(run_type: RunType) -> Result<(), Error> {
|
|
|
|
|
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
|
|
|
|
2019-02-11 20:38:51 +02:00
|
|
|
run_type.execute(&scoop).args(&["update"]).check_run()?;
|
|
|
|
|
run_type.execute(&scoop).args(&["update", "*"]).check_run()
|
2018-10-17 13:57:30 +03:00
|
|
|
}
|
|
|
|
|
|
2018-08-22 22:01:06 +03:00
|
|
|
pub struct Powershell {
|
|
|
|
|
path: Option<PathBuf>,
|
2019-01-27 20:15:59 +02:00
|
|
|
profile: Option<PathBuf>,
|
2018-08-22 22:01:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Powershell {
|
2019-01-21 20:12:56 +02:00
|
|
|
/// 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.
|
2018-08-22 22:01:06 +03:00
|
|
|
pub fn new() -> Self {
|
2019-01-27 20:15:59 +02:00
|
|
|
let path = which("powershell").filter(|_| !is_dumb());
|
|
|
|
|
|
|
|
|
|
let profile = path.as_ref().and_then(|path| {
|
|
|
|
|
Command::new(path)
|
|
|
|
|
.args(&["-Command", "echo $profile"])
|
|
|
|
|
.check_output()
|
|
|
|
|
.map(|output| PathBuf::from(output.trim()))
|
2019-05-25 21:28:16 +03:00
|
|
|
.and_then(|p| p.require())
|
2019-01-27 20:15:59 +02:00
|
|
|
.ok()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Powershell { path, profile }
|
2018-08-22 22:01:06 +03:00
|
|
|
}
|
|
|
|
|
|
2018-08-22 22:18:48 +03:00
|
|
|
pub fn has_command(powershell: &PathBuf, command: &str) -> bool {
|
2018-12-11 16:43:26 +02:00
|
|
|
|| -> Result<(), Error> {
|
2018-08-22 22:18:48 +03:00
|
|
|
Command::new(&powershell)
|
|
|
|
|
.args(&["-Command", &format!("Get-Command {}", command)])
|
2019-01-01 22:22:07 +02:00
|
|
|
.check_output()?;
|
2018-08-22 22:18:48 +03:00
|
|
|
Ok(())
|
2018-12-11 16:00:19 +02:00
|
|
|
}()
|
|
|
|
|
.is_ok()
|
2018-08-22 22:18:48 +03:00
|
|
|
}
|
|
|
|
|
|
2019-01-27 20:15:59 +02:00
|
|
|
pub fn profile(&self) -> Option<&PathBuf> {
|
|
|
|
|
self.profile.as_ref()
|
2018-08-23 22:08:04 +03:00
|
|
|
}
|
|
|
|
|
|
2019-02-11 20:38:51 +02:00
|
|
|
pub fn update_modules(&self, run_type: RunType) -> Result<(), Error> {
|
|
|
|
|
let powershell = require_option(self.path.as_ref())?;
|
2018-08-22 22:18:48 +03:00
|
|
|
|
2019-02-11 20:38:51 +02:00
|
|
|
print_separator("Powershell Modules Update");
|
|
|
|
|
run_type.execute(&powershell).args(&["Update-Module", "-v"]).check_run()
|
2018-08-22 22:18:48 +03:00
|
|
|
}
|
|
|
|
|
|
2019-02-11 20:38:51 +02:00
|
|
|
pub fn windows_update(&self, run_type: RunType) -> Result<(), Error> {
|
|
|
|
|
let powershell = require_option(self.path.as_ref())?;
|
|
|
|
|
|
|
|
|
|
if !Self::has_command(&powershell, "Install-WindowsUpdate") {
|
|
|
|
|
Err(ErrorKind::SkipStep)?;
|
2018-08-22 22:01:06 +03:00
|
|
|
}
|
2019-02-11 20:38:51 +02:00
|
|
|
print_separator("Windows Update");
|
2018-08-22 22:01:06 +03:00
|
|
|
|
2019-02-11 20:38:51 +02:00
|
|
|
run_type
|
|
|
|
|
.execute(&powershell)
|
|
|
|
|
.args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"])
|
|
|
|
|
.check_run()
|
2018-08-22 22:01:06 +03:00
|
|
|
}
|
|
|
|
|
}
|