pay-respects/utils/src/files.rs

177 lines
3.8 KiB
Rust
Raw Normal View History

2024-12-29 16:16:13 +01:00
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::evals::find_similar;
2025-01-20 15:37:45 +01:00
use itertools::Itertools;
2023-08-07 17:12:38 +02:00
2023-08-03 21:26:41 +02:00
pub fn get_path_files() -> Vec<String> {
2025-03-13 21:06:21 +01:00
let env = std::env::var("_PR_EXECUTABLES");
if let Ok(env) = env {
if !env.is_empty() {
return env.split(' ').map(|s| s.to_owned()).collect();
}
}
2024-12-07 17:27:39 +01:00
let path_env = path_env();
2024-11-24 20:00:31 +08:00
2025-01-04 22:30:19 +01:00
#[cfg(debug_assertions)]
eprintln!("path_env: {path_env}");
2024-11-24 20:00:31 +08:00
2024-12-07 17:27:39 +01:00
let path_env_sep = path_env_sep();
let path = path_env.split(path_env_sep).collect::<Vec<&str>>();
2023-08-03 21:26:41 +02:00
let mut all_executable = vec![];
for p in path {
2024-11-24 20:00:31 +08:00
#[cfg(windows)]
2024-12-07 17:27:39 +01:00
let p = path_convert(p);
2024-11-24 20:00:31 +08:00
2024-12-07 17:27:39 +01:00
#[cfg(debug_assertions)]
eprintln!("p={p}");
2024-11-24 20:00:31 +08:00
2023-08-03 21:26:41 +02:00
let files = match std::fs::read_dir(p) {
Ok(files) => files,
Err(_) => continue,
};
for file in files {
let file = file.unwrap();
2024-11-24 20:00:31 +08:00
#[allow(unused_mut)]
let mut file_name = file.file_name().into_string().unwrap();
#[cfg(windows)]
2024-12-07 17:27:39 +01:00
strip_extension(&mut file_name);
2024-11-24 20:00:31 +08:00
2023-08-03 21:26:41 +02:00
all_executable.push(file_name);
}
}
2024-11-24 20:00:31 +08:00
2024-12-07 17:27:39 +01:00
#[cfg(debug_assertions)]
{
2024-11-24 20:00:31 +08:00
let mut all_executable = all_executable.clone();
all_executable.sort_unstable();
eprintln!("all_executable={all_executable:?}");
}
2025-01-20 15:37:45 +01:00
all_executable.iter().unique().cloned().collect()
2023-08-03 21:26:41 +02:00
}
2024-12-09 16:27:39 +01:00
pub fn best_match_file(input: &str) -> Option<String> {
2023-08-03 21:26:41 +02:00
let mut input = input.trim_matches(|c| c == '\'' || c == '"').to_owned();
2025-01-04 22:30:19 +01:00
#[cfg(debug_assertions)]
eprintln!("best_match_file input: {input}");
2023-08-07 17:12:38 +02:00
let mut exit_dirs = Vec::new();
let mut files = loop {
2023-08-03 21:26:41 +02:00
match std::fs::read_dir(&input) {
Ok(files) => break files,
Err(_) => {
2024-11-24 20:00:31 +08:00
if let Some((dirs, exit_dir)) = input.rsplit_once(std::path::MAIN_SEPARATOR) {
2023-08-07 17:12:38 +02:00
exit_dirs.push(exit_dir.to_owned());
2023-08-03 21:26:41 +02:00
input = dirs.to_owned();
} else {
2023-08-07 17:12:38 +02:00
exit_dirs.push(input.to_owned());
input = ".".to_owned();
2023-08-03 21:26:41 +02:00
break std::fs::read_dir("./").unwrap();
}
}
}
};
2023-08-07 17:12:38 +02:00
while let Some(exit_dir) = exit_dirs.pop() {
2023-08-07 20:21:02 +02:00
let dir_files = files
.map(|file| {
let file = file.unwrap();
file.file_name().into_string().unwrap().replace(' ', "\\ ")
2023-08-07 20:21:02 +02:00
})
.collect::<Vec<String>>();
2023-08-07 17:12:38 +02:00
2024-12-07 01:51:15 +01:00
let best_match = find_similar(&exit_dir, &dir_files, Some(2));
2023-08-07 20:21:02 +02:00
best_match.as_ref()?;
2023-08-07 17:12:38 +02:00
input = format!("{}/{}", input, best_match.unwrap());
files = match std::fs::read_dir(&input) {
Ok(files) => files,
Err(_) => return Some(input),
};
2023-08-03 21:26:41 +02:00
}
2023-08-07 17:12:38 +02:00
Some(input)
2023-08-03 21:26:41 +02:00
}
2024-11-24 20:00:31 +08:00
#[cfg(windows)]
fn msys2_conv_path(p: &str) -> std::io::Result<String> {
std::process::Command::new("cygpath")
.arg("-w")
.arg(p)
.output()
.map(|output| String::from_utf8_lossy(&output.stdout).trim().to_owned())
}
#[cfg(windows)]
fn is_msystem() -> bool {
std::env::var("MSYSTEM").is_ok()
}
2024-12-07 17:27:39 +01:00
#[cfg(windows)]
fn path_env() -> String {
if is_msystem() {
String::from_utf8_lossy(
&std::process::Command::new("bash")
.arg("-c")
.arg("echo $PATH")
.output()
.unwrap()
.stdout,
)
.trim()
.to_owned()
} else {
std::env::var("PATH").unwrap()
}
}
#[cfg(windows)]
pub fn path_env_sep() -> &'static str {
2024-12-07 17:27:39 +01:00
if is_msystem() {
":"
} else {
";"
}
}
#[cfg(windows)]
pub fn path_convert(path: &str) -> String {
2024-12-07 17:27:39 +01:00
if is_msystem() {
msys2_conv_path(path).expect("Failed to convert path for msys")
} else {
path.to_owned()
}
}
#[cfg(windows)]
fn strip_extension(file_name: &str) -> String {
let mut file_name = file_name.to_owned();
let suffixies = [".exe", ".sh", ".ps1"];
for suffix in suffixies {
if let Some(file_name_strip) = file_name.strip_suffix(suffix) {
file_name = file_name_strip.to_owned();
break;
}
}
if !file_name.contains(".") {
file_name = file_name.to_owned();
}
file_name
}
#[cfg(not(windows))]
fn path_env() -> String {
std::env::var("PATH").unwrap()
}
#[cfg(not(windows))]
pub fn path_env_sep() -> &'static str {
2024-12-07 17:27:39 +01:00
":"
}