lean_ctx/core/
portable_binary.rs1pub fn resolve_portable_binary() -> String {
2 let which_cmd = if cfg!(windows) { "where" } else { "which" };
3 if let Ok(output) = std::process::Command::new(which_cmd)
4 .arg("lean-ctx")
5 .stderr(std::process::Stdio::null())
6 .output()
7 {
8 if output.status.success() {
9 let raw = String::from_utf8_lossy(&output.stdout).trim().to_string();
10 if !raw.is_empty() {
11 let path = pick_best_binary_line(&raw);
12 return sanitize_exe_path(&path);
13 }
14 }
15 }
16 let path = std::env::current_exe().map_or_else(
17 |_| "lean-ctx".to_string(),
18 |p| p.to_string_lossy().to_string(),
19 );
20 sanitize_exe_path(&path)
21}
22
23fn pick_best_binary_line(raw: &str) -> String {
27 let lines: Vec<&str> = raw
28 .lines()
29 .map(str::trim)
30 .filter(|l| !l.is_empty())
31 .collect();
32 if lines.len() <= 1 {
33 return lines.first().unwrap_or(&"lean-ctx").to_string();
34 }
35 if cfg!(windows) {
36 if let Some(cmd) = lines.iter().find(|l| {
37 std::path::Path::new(*l).extension().is_some_and(|ext| {
38 ext.eq_ignore_ascii_case("cmd") || ext.eq_ignore_ascii_case("exe")
39 })
40 }) {
41 return cmd.to_string();
42 }
43 }
44 lines[0].to_string()
45}
46
47fn sanitize_exe_path(path: &str) -> String {
48 let cleaned = path.trim_end_matches(" (deleted)");
49 if cfg!(windows) {
50 super::pathutil::normalize_tool_path(cleaned)
51 } else {
52 cleaned.to_string()
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn single_line_returns_as_is() {
62 assert_eq!(
63 pick_best_binary_line("/usr/bin/lean-ctx"),
64 "/usr/bin/lean-ctx"
65 );
66 }
67
68 #[test]
69 fn multiline_returns_first_line() {
70 let raw = "/usr/bin/lean-ctx\n/usr/local/bin/lean-ctx";
71 let result = pick_best_binary_line(raw);
72 assert_eq!(result, "/usr/bin/lean-ctx");
73 }
74
75 #[test]
76 fn empty_returns_fallback() {
77 assert_eq!(pick_best_binary_line(""), "lean-ctx");
78 }
79
80 #[test]
81 fn sanitize_removes_deleted_suffix() {
82 assert_eq!(
83 sanitize_exe_path("/usr/bin/lean-ctx (deleted)"),
84 "/usr/bin/lean-ctx"
85 );
86 }
87
88 #[test]
89 fn whitespace_lines_are_filtered() {
90 let raw = " /usr/bin/lean-ctx \n \n /usr/local/bin/lean-ctx ";
91 assert_eq!(pick_best_binary_line(raw), "/usr/bin/lean-ctx");
92 }
93
94 #[cfg(windows)]
95 #[test]
96 fn sanitize_normalizes_msys_path_on_windows() {
97 assert_eq!(
98 sanitize_exe_path("/c/Users/ABC/.local/bin/lean-ctx"),
99 "C:/Users/ABC/.local/bin/lean-ctx"
100 );
101 }
102
103 #[cfg(windows)]
104 #[test]
105 fn sanitize_keeps_native_windows_path() {
106 assert_eq!(
107 sanitize_exe_path(r"C:\Users\ABC\lean-ctx.exe"),
108 "C:/Users/ABC/lean-ctx.exe"
109 );
110 }
111
112 #[cfg(not(windows))]
113 #[test]
114 fn sanitize_unix_path_unchanged() {
115 assert_eq!(
116 sanitize_exe_path("/usr/local/bin/lean-ctx"),
117 "/usr/local/bin/lean-ctx"
118 );
119 }
120}