[F] Fix rust windows build

This commit is contained in:
Azalea 2024-12-22 00:27:10 -05:00
parent 1ff4b12f19
commit 89cced9950
2 changed files with 28 additions and 103 deletions

View file

@ -229,10 +229,11 @@ fn create_config(
} else if color_level.has_256 { } else if color_level.has_256 {
AnsiMode::Ansi256 AnsiMode::Ansi256
} else if color_level.has_basic { } else if color_level.has_basic {
unimplemented!( // unimplemented!(
"{mode} color mode not supported", // "{mode} color mode not supported",
mode = AnsiMode::Ansi16.as_ref() // mode = AnsiMode::Ansi16.as_ref()
); // );
AnsiMode::Ansi256
} else { } else {
unreachable!(); unreachable!();
} }

View file

@ -2,8 +2,6 @@ use std::borrow::Cow;
use std::ffi::OsStr; use std::ffi::OsStr;
#[cfg(feature = "macchina")] #[cfg(feature = "macchina")]
use std::fs; use std::fs;
#[cfg(windows)]
use std::io;
use std::io::{self, Write as _}; use std::io::{self, Write as _};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::Command; use std::process::Command;
@ -315,16 +313,10 @@ pub fn run(asc: RecoloredAsciiArt, backend: Backend, args: Option<&Vec<String>>)
let asc = asc.lines.join("\n"); let asc = asc.lines.join("\n");
match backend { match backend {
Backend::Neofetch => { Backend::Neofetch => run_neofetch(asc, args).context("failed to run neofetch")?,
run_neofetch(asc, args).context("failed to run neofetch")?; Backend::Fastfetch => run_fastfetch(asc, args).context("failed to run fastfetch")?,
},
Backend::Fastfetch => {
run_fastfetch(asc, args).context("failed to run fastfetch")?;
},
#[cfg(feature = "macchina")] #[cfg(feature = "macchina")]
Backend::Macchina => { Backend::Macchina => run_macchina(asc, args).context("failed to run macchina")?,
run_macchina(asc, args).context("failed to run macchina")?;
},
} }
Ok(()) Ok(())
@ -379,98 +371,30 @@ where
#[cfg(windows)] #[cfg(windows)]
fn bash_path() -> Result<PathBuf> { fn bash_path() -> Result<PathBuf> {
// Find `bash.exe` in `PATH`, but exclude the known bad paths // Find `bash.exe` in `PATH`, but exclude the known bad paths
let bash_path = find_in_path("bash.exe") if let Some(bash_path) = find_in_path("bash.exe").context("bash.exe not found")? {
.context("failed to check existence of `bash.exe` in `PATH`")? // Check if it's not MSYS bash https://stackoverflow.com/a/58418686/1529493
.map_or_else( if !bash_path.ends_with(r"Git\usr\bin\bash.exe") {
|| Ok(None), // Check if it's not WSL bash
|bash_path| { // See https://github.com/hykilpikonna/hyfetch/issues/233
if bash_path.ends_with(r"Git\usr\bin\bash.exe") { let windir = env::var_os("windir").context("`windir` environ not found")?;
// See https://stackoverflow.com/a/58418686/1529493 match is_same_file(&bash_path, Path::new(&windir).join(r"System32\bash.exe")) {
Ok(None) Ok(false) => return Ok(bash_path),
} else { Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(bash_path),
// See https://github.com/hykilpikonna/hyfetch/issues/233 _ => {}
let windir = env::var_os("windir")
.context("`windir` environment variable is not set or invalid")?;
match is_same_file(&bash_path, Path::new(&windir).join(r"System32\bash.exe")) {
Ok(true) => Ok(None),
Ok(false) => Ok(Some(bash_path)),
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(Some(bash_path)),
Err(err) => {
Err(err).context("failed to check if paths refer to the same file")
},
}
}
},
)?;
// Detect any Git for Windows installation in `PATH`
let bash_path = bash_path.map_or_else(
|| {
let git_path = find_in_path("git.exe")
.context("failed to check existence of `git.exe` in `PATH`")?;
match git_path {
Some(git_path) if git_path.ends_with(r"Git\cmd\git.exe") => {
let bash_path = git_path
.parent()
.expect("parent should not be `None`")
.parent()
.expect("parent should not be `None`")
.join(r"bin\bash.exe");
if bash_path.is_file() {
Ok(Some(bash_path))
} else {
Ok(None)
}
},
_ => Ok(None),
} }
}, }
|path| Ok(Some(path)), }
)?;
// Fall back to default Git for Windows installation paths if let Some(bash_path) = find_in_path("git.exe").context("failed to find `git.exe` in `PATH`")? {
let bash_path = bash_path if bash_path.ends_with(r"Git\cmd\git.exe") {
.or_else(|| { let pth = bash_path.parent().unwrap().parent().unwrap().join(r"bin\bash.exe");
let program_files_dir = env::var_os("ProgramFiles")?; if pth.is_file() {
let bash_path = Path::new(&program_files_dir).join(r"Git\bin\bash.exe"); return Ok(pth);
if bash_path.is_file() {
Some(bash_path)
} else {
None
} }
}) }
.or_else(|| { }
let program_files_x86_dir = env::var_os("ProgramFiles(x86)")?;
let bash_path = Path::new(&program_files_x86_dir).join(r"Git\bin\bash.exe");
if bash_path.is_file() {
Some(bash_path)
} else {
None
}
});
// Bundled git bash Err(anyhow!("bash.exe not found"))
let bash_path = bash_path.map_or_else(
|| {
let current_exe_path: PathBuf = env::current_exe()
.and_then(|p| p.normalize().map(|p| p.into()))
.context("failed to get path of current running executable")?;
let bash_path = current_exe_path
.parent()
.expect("parent should not be `None`")
.join(r"git\bin\bash.exe");
if bash_path.is_file() {
Ok(Some(bash_path))
} else {
Ok(None)
}
},
|path| Ok(Some(path)),
)?;
let bash_path = bash_path.context("bash command not found")?;
Ok(bash_path)
} }
/// Runs neofetch command, returning the piped stdout output. /// Runs neofetch command, returning the piped stdout output.