diff --git a/README.md b/README.md index 9a518cdc..06891fa5 100644 --- a/README.md +++ b/README.md @@ -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 `nix upgrade-nix && nix --upgrade`. * **Unix**: Run [Pearl](https://github.com/pearl-core/pearl) `pearl update`. +* **Windows**: Run Topgrade inside WSL. * **Windows**: Upgrade Powershell modules * **Windows**: Upgrade all [Chocolatey](https://chocolatey.org/) packages * **Windows**: Upgrade all [Scoop](https://scoop.sh) packages diff --git a/src/main.rs b/src/main.rs index 854aeb14..8c54b68f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,6 +122,9 @@ fn run() -> Result<(), Error> { #[cfg(windows)] 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")] let distribution = linux::Distribution::detect(); diff --git a/src/steps/os/windows.rs b/src/steps/os/windows.rs index 266b44e8..33d629cc 100644 --- a/src/steps/os/windows.rs +++ b/src/steps/os/windows.rs @@ -81,3 +81,19 @@ impl Powershell { .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() +} diff --git a/src/terminal.rs b/src/terminal.rs index 48dea298..3b316ec9 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -2,6 +2,7 @@ use chrono::{Local, Timelike}; use console::{style, Term}; use lazy_static::lazy_static; use std::cmp::{max, min}; +use std::env; use std::io::{self, Write}; use std::process::Command; use std::sync::Mutex; @@ -12,7 +13,6 @@ lazy_static! { #[cfg(unix)] fn shell() -> String { - use std::env; env::var("SHELL").unwrap_or_else(|_| "sh".to_string()) } @@ -23,6 +23,7 @@ fn shell() -> &'static str { struct Terminal { width: Option, + prefix: String, term: Term, } @@ -32,13 +33,17 @@ impl Terminal { Self { width: term.size_checked().map(|(_, w)| w), term, + prefix: env::var("TOPGRADE_PREFIX") + .map(|prefix| format!("({}) ", prefix)) + .unwrap_or_else(|_| String::new()), } } fn print_separator>(&mut self, message: P) { let now = Local::now(); let message = format!( - "{:02}:{:02}:{:02} - {}", + "{}{:02}:{:02}:{:02} - {}", + self.prefix, now.hour(), now.minute(), now.second(), @@ -104,7 +109,8 @@ impl Terminal { .write_fmt(format_args!( "\n{}", style(format!( - "Retry? (y)es/(N)o/(s)hell {}", + "{}Retry? (y)es/(N)o/(s)hell {}", + self.prefix, if interrupted { "(Press Ctrl+C again to stop Topgrade) " } else {