Run WSL in Topgrade if present (fix #163)

This commit is contained in:
Roey Darwish Dror
2019-06-04 09:35:29 +03:00
parent 50a0563bb4
commit 5b179d93d9
4 changed files with 29 additions and 3 deletions

View File

@@ -56,6 +56,7 @@ Just run `topgrade`. It will run the following steps:
* **Unix**: Run `brew update && brew upgrade`. This should handle both Homebrew and Linuxbrew * **Unix**: Run `brew update && brew upgrade`. This should handle both Homebrew and Linuxbrew
* **Unix**: Run `nix upgrade-nix && nix --upgrade`. * **Unix**: Run `nix upgrade-nix && nix --upgrade`.
* **Unix**: Run [Pearl](https://github.com/pearl-core/pearl) `pearl update`. * **Unix**: Run [Pearl](https://github.com/pearl-core/pearl) `pearl update`.
* **Windows**: Run Topgrade inside WSL.
* **Windows**: Upgrade Powershell modules * **Windows**: Upgrade Powershell modules
* **Windows**: Upgrade all [Chocolatey](https://chocolatey.org/) packages * **Windows**: Upgrade all [Chocolatey](https://chocolatey.org/) packages
* **Windows**: Upgrade all [Scoop](https://scoop.sh) packages * **Windows**: Upgrade all [Scoop](https://scoop.sh) packages

View File

@@ -122,6 +122,9 @@ fn run() -> Result<(), Error> {
#[cfg(windows)] #[cfg(windows)]
let should_run_powershell = powershell.profile().is_some() && config.should_run(Step::Powershell); let should_run_powershell = powershell.profile().is_some() && config.should_run(Step::Powershell);
#[cfg(windows)]
execute(&mut report, "WSL", || windows::run_wsl_topgrade(run_type), true)?;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
let distribution = linux::Distribution::detect(); let distribution = linux::Distribution::detect();

View File

@@ -81,3 +81,19 @@ impl Powershell {
.check_run() .check_run()
} }
} }
pub fn run_wsl_topgrade(run_type: RunType) -> Result<(), Error> {
let wsl = require("wsl")?;
let topgrade = Command::new(&wsl)
.args(&["bash", "-l", "which", "topgrade"])
.check_output()
.map_err(|_| ErrorKind::SkipStep)?;
print_separator("WSL");
run_type
.execute(&wsl)
.args(&["bash", "-c"])
.arg(format!("TOPGRADE_PREFIX=WSL exec {}", topgrade))
.check_run()
}

View File

@@ -2,6 +2,7 @@ use chrono::{Local, Timelike};
use console::{style, Term}; use console::{style, Term};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::cmp::{max, min}; use std::cmp::{max, min};
use std::env;
use std::io::{self, Write}; use std::io::{self, Write};
use std::process::Command; use std::process::Command;
use std::sync::Mutex; use std::sync::Mutex;
@@ -12,7 +13,6 @@ lazy_static! {
#[cfg(unix)] #[cfg(unix)]
fn shell() -> String { fn shell() -> String {
use std::env;
env::var("SHELL").unwrap_or_else(|_| "sh".to_string()) env::var("SHELL").unwrap_or_else(|_| "sh".to_string())
} }
@@ -23,6 +23,7 @@ fn shell() -> &'static str {
struct Terminal { struct Terminal {
width: Option<u16>, width: Option<u16>,
prefix: String,
term: Term, term: Term,
} }
@@ -32,13 +33,17 @@ impl Terminal {
Self { Self {
width: term.size_checked().map(|(_, w)| w), width: term.size_checked().map(|(_, w)| w),
term, term,
prefix: env::var("TOPGRADE_PREFIX")
.map(|prefix| format!("({}) ", prefix))
.unwrap_or_else(|_| String::new()),
} }
} }
fn print_separator<P: AsRef<str>>(&mut self, message: P) { fn print_separator<P: AsRef<str>>(&mut self, message: P) {
let now = Local::now(); let now = Local::now();
let message = format!( let message = format!(
"{:02}:{:02}:{:02} - {}", "{}{:02}:{:02}:{:02} - {}",
self.prefix,
now.hour(), now.hour(),
now.minute(), now.minute(),
now.second(), now.second(),
@@ -104,7 +109,8 @@ impl Terminal {
.write_fmt(format_args!( .write_fmt(format_args!(
"\n{}", "\n{}",
style(format!( style(format!(
"Retry? (y)es/(N)o/(s)hell {}", "{}Retry? (y)es/(N)o/(s)hell {}",
self.prefix,
if interrupted { if interrupted {
"(Press Ctrl+C again to stop Topgrade) " "(Press Ctrl+C again to stop Topgrade) "
} else { } else {