From 4f981fd504845af861e2c53c554394e21374ce68 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Sun, 27 Jan 2019 20:15:59 +0200 Subject: [PATCH] Only update Powershell modules if profile is present and allow disabling this step (fix #114) --- src/config.rs | 4 ++++ src/main.rs | 6 +++++- src/steps/os/windows.rs | 31 ++++++++++++++----------------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index cc435342..3bc57324 100644 --- a/src/config.rs +++ b/src/config.rs @@ -39,6 +39,10 @@ pub enum Step { Emacs, /// Don't upgrade ruby gems Gem, + + #[cfg(windows)] + /// Don't update Powershell modules + Powershell, } impl Step { diff --git a/src/main.rs b/src/main.rs index 7992d586..e9e3c3ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,7 +96,11 @@ fn run() -> Result<(), Error> { let powershell = windows::Powershell::new(); #[cfg(windows)] - report.push_result(execute(|| powershell.update_modules(run_type), opt.no_retry)?); + { + if powershell.profile().is_some() && !opt.disable.contains(&Step::Powershell) { + report.push_result(execute(|| powershell.update_modules(run_type), opt.no_retry)?); + } + } #[cfg(target_os = "linux")] let distribution = linux::Distribution::detect(); diff --git a/src/steps/os/windows.rs b/src/steps/os/windows.rs index fa5be586..dada49d7 100644 --- a/src/steps/os/windows.rs +++ b/src/steps/os/windows.rs @@ -2,7 +2,6 @@ use crate::error::Error; use crate::executor::{CommandExt, RunType}; use crate::terminal::{is_dumb, print_separator}; use crate::utils::{self, which}; -use log::error; use std::path::PathBuf; use std::process::Command; @@ -43,6 +42,7 @@ pub fn run_scoop(run_type: RunType) -> Option<(&'static str, bool)> { pub struct Powershell { path: Option, + profile: Option, } impl Powershell { @@ -51,9 +51,17 @@ impl Powershell { /// 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 { - Powershell { - path: which("powershell").filter(|_| !is_dumb()), - } + 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())) + .ok() + }); + + Powershell { path, profile } } pub fn has_command(powershell: &PathBuf, command: &str) -> bool { @@ -66,19 +74,8 @@ impl Powershell { .is_ok() } - pub fn profile(&self) -> Option { - if let Some(powershell) = &self.path { - let result = Command::new(powershell) - .args(&["-Command", "echo $profile"]) - .check_output() - .map(|output| PathBuf::from(output.trim())); - - match result { - Err(e) => error!("Error getting Powershell profile: {}", e), - Ok(path) => return Some(path), - } - } - None + pub fn profile(&self) -> Option<&PathBuf> { + self.profile.as_ref() } #[must_use]