Skip to content

Commit 6a709ce

Browse files
committed
feat:non-path-discovery for modules
1 parent c45e43c commit 6a709ce

File tree

2 files changed

+75
-41
lines changed

2 files changed

+75
-41
lines changed

src-tauri/src/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use aw_server::endpoints::build_rocket;
22
#[cfg(not(target_os = "linux"))]
33
use directories::ProjectDirs;
4-
#[cfg(target_os = "linux")]
54
use directories::UserDirs;
65
use lazy_static::lazy_static;
76
use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
@@ -186,14 +185,27 @@ pub struct Defaults {
186185
pub autostart: bool,
187186
pub autostart_minimized: bool,
188187
pub port: u16,
188+
pub discovery_path: PathBuf,
189189
}
190190

191191
impl Default for Defaults {
192192
fn default() -> Self {
193+
let discovery_path = if cfg!(unix) {
194+
UserDirs::new()
195+
.map(|dirs| dirs.home_dir().join("aw-modules"))
196+
.unwrap_or_default()
197+
} else if cfg!(windows) {
198+
let username = std::env::var("USERNAME").unwrap_or_default();
199+
PathBuf::from(format!(r"C:\Users\{}\aw-modules", username))
200+
} else {
201+
PathBuf::new()
202+
};
203+
193204
Defaults {
194205
autostart: true,
195206
autostart_minimized: true,
196207
port: 5699, // TODO: update before going stable
208+
discovery_path,
197209
}
198210
}
199211
}

src-tauri/src/manager.rs

+62-40
Original file line numberDiff line numberDiff line change
@@ -329,55 +329,77 @@ fn start_module_thread(name: String, custom_args: Option<Vec<String>>, tx: Sende
329329

330330
#[cfg(unix)]
331331
fn get_modules_in_path() -> BTreeSet<String> {
332-
let excluded = ["awk", "aw-tauri", "aw-client", "aw-cli"];
333-
env::var_os("PATH")
334-
.map(|paths| {
335-
env::split_paths(&paths)
336-
.flat_map(|path| fs::read_dir(path).ok())
337-
.flatten()
338-
.filter_map(Result::ok)
339-
.filter_map(|entry| {
340-
let metadata = entry.metadata().ok()?;
341-
let is_executable = (metadata.is_file() || metadata.is_symlink())
342-
&& metadata.permissions().mode() & 0o111 != 0;
343-
if !is_executable {
344-
return None;
345-
}
346-
347-
entry
348-
.file_name()
349-
.to_str()
350-
.map(|s| s.to_string())
351-
.filter(|name: &String| name.starts_with("aw") && !name.contains("."))
352-
})
353-
.collect::<BTreeSet<_>>()
332+
let excluded = ["awk", "aw-tauri", "aw-client", "aw-cli", "aw-qt"];
333+
let config = crate::get_config();
334+
335+
let path = env::var_os("PATH").unwrap_or_default();
336+
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
337+
338+
if !paths.contains(&config.defaults.discovery_path) {
339+
// add to the front of the path list
340+
paths.insert(0, config.defaults.discovery_path.to_owned());
341+
}
342+
343+
// Create new PATH-like string
344+
let new_paths = env::join_paths(paths).unwrap_or_default();
345+
346+
env::split_paths(&new_paths)
347+
.flat_map(|path| fs::read_dir(path).ok())
348+
.flatten()
349+
.filter_map(Result::ok)
350+
.filter_map(|entry| {
351+
let metadata = entry.metadata().ok()?;
352+
let is_executable = (metadata.is_file() || metadata.is_symlink())
353+
&& metadata.permissions().mode() & 0o111 != 0;
354+
if !is_executable {
355+
return None;
356+
}
357+
358+
entry
359+
.file_name()
360+
.to_str()
361+
.map(|s| s.to_string())
362+
.filter(|name: &String| name.starts_with("aw") && !name.contains("."))
354363
})
355-
.unwrap_or_default()
364+
.collect::<BTreeSet<_>>()
356365
.into_iter()
357366
.filter(|name: &String| !excluded.contains(&name.as_str()))
358367
.collect()
359368
}
360369

361370
#[cfg(windows)]
362371
fn get_modules_in_path() -> BTreeSet<String> {
363-
let excluded = ["aw-tauri", "aw-client", "aw-cli"];
364-
env::var_os("PATH")
365-
.map(|paths| {
366-
env::split_paths(&paths)
367-
.flat_map(|path| fs::read_dir(path).ok())
368-
.flatten()
369-
.filter_map(Result::ok)
370-
.filter_map(|entry| {
371-
let path = entry.path();
372-
if path.is_file() && path.extension().map_or(false, |ext| ext == "exe") {
373-
path.file_stem()?.to_str().map(String::from)
374-
} else {
375-
None
376-
}
377-
})
378-
.collect::<BTreeSet<_>>()
372+
let excluded = ["aw-tauri", "aw-client", "aw-cli", "aw-qt"];
373+
374+
// Get the discovery path from config
375+
let config = crate::get_config();
376+
377+
// Get the current PATH
378+
let path = env::var_os("PATH").unwrap_or_default();
379+
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
380+
381+
// Add discovery path if not already in PATH
382+
if !paths.contains(&config.defaults.discovery_path) {
383+
paths.push(config.defaults.discovery_path.clone());
384+
}
385+
386+
// Create new PATH-like string
387+
let new_paths = env::join_paths(paths).unwrap_or_default();
388+
389+
// Use the combined paths to find modules
390+
env::split_paths(&new_paths)
391+
.flat_map(|path| fs::read_dir(path).ok())
392+
.flatten()
393+
.filter_map(Result::ok)
394+
.filter_map(|entry| {
395+
let path = entry.path();
396+
if path.is_file() && path.extension().map_or(false, |ext| ext == "exe") {
397+
path.file_stem()?.to_str().map(String::from)
398+
} else {
399+
None
400+
}
379401
})
380-
.unwrap_or_default()
402+
.collect::<BTreeSet<_>>()
381403
.into_iter()
382404
.filter(|name| !excluded.contains(&name.as_str()))
383405
.collect()

0 commit comments

Comments
 (0)