Skip to content

Commit

Permalink
feat:non-path-discovery for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbrayo committed Jan 19, 2025
1 parent c45e43c commit fe4cbe9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 41 deletions.
14 changes: 13 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use aw_server::endpoints::build_rocket;
#[cfg(not(target_os = "linux"))]
use directories::ProjectDirs;
#[cfg(target_os = "linux")]
use directories::UserDirs;
use lazy_static::lazy_static;
use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
Expand Down Expand Up @@ -186,14 +185,27 @@ pub struct Defaults {
pub autostart: bool,
pub autostart_minimized: bool,
pub port: u16,
pub discovery_path: PathBuf,
}

impl Default for Defaults {
fn default() -> Self {
let discovery_path = if cfg!(unix) {
UserDirs::new()
.map(|dirs| dirs.home_dir().join("aw-modules"))
.unwrap_or_default()
} else if cfg!(windows) {
let username = std::env::var("USERNAME").unwrap_or_default();
PathBuf::from(format!(r"C:\Users\{}\aw-modules", username))
} else {
PathBuf::new()
};

Defaults {
autostart: true,
autostart_minimized: true,
port: 5699, // TODO: update before going stable
discovery_path,
}
}
}
Expand Down
102 changes: 62 additions & 40 deletions src-tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,55 +329,77 @@ fn start_module_thread(name: String, custom_args: Option<Vec<String>>, tx: Sende

#[cfg(unix)]
fn get_modules_in_path() -> BTreeSet<String> {
let excluded = ["awk", "aw-tauri", "aw-client", "aw-cli"];
env::var_os("PATH")
.map(|paths| {
env::split_paths(&paths)
.flat_map(|path| fs::read_dir(path).ok())
.flatten()
.filter_map(Result::ok)
.filter_map(|entry| {
let metadata = entry.metadata().ok()?;
let is_executable = (metadata.is_file() || metadata.is_symlink())
&& metadata.permissions().mode() & 0o111 != 0;
if !is_executable {
return None;
}

entry
.file_name()
.to_str()
.map(|s| s.to_string())
.filter(|name: &String| name.starts_with("aw") && !name.contains("."))
})
.collect::<BTreeSet<_>>()
let excluded = ["awk", "aw-tauri", "aw-client", "aw-cli", "aw-qt"];
let config = crate::get_config();

let path = env::var_os("PATH").unwrap_or_default();
let mut paths = env::split_paths(&path).collect::<Vec<_>>();

if !paths.contains(&config.defaults.discovery_path) {
// add to the front of the path list
paths.insert(0, config.defaults.discovery_path.to_owned());
}

// Create new PATH-like string
let new_paths = env::join_paths(paths).unwrap_or_default();

env::split_paths(&new_paths)
.flat_map(|path| fs::read_dir(path).ok())
.flatten()
.filter_map(Result::ok)
.filter_map(|entry| {
let metadata = entry.metadata().ok()?;
let is_executable = (metadata.is_file() || metadata.is_symlink())
&& metadata.permissions().mode() & 0o111 != 0;
if !is_executable {
return None;
}

entry
.file_name()
.to_str()
.map(|s| s.to_string())
.filter(|name: &String| name.starts_with("aw") && !name.contains("."))
})
.unwrap_or_default()
.collect::<BTreeSet<_>>()
.into_iter()
.filter(|name: &String| !excluded.contains(&name.as_str()))
.collect()
}

#[cfg(windows)]
fn get_modules_in_path() -> BTreeSet<String> {
let excluded = ["aw-tauri", "aw-client", "aw-cli"];
env::var_os("PATH")
.map(|paths| {
env::split_paths(&paths)
.flat_map(|path| fs::read_dir(path).ok())
.flatten()
.filter_map(Result::ok)
.filter_map(|entry| {
let path = entry.path();
if path.is_file() && path.extension().map_or(false, |ext| ext == "exe") {
path.file_stem()?.to_str().map(String::from)
} else {
None
}
})
.collect::<BTreeSet<_>>()
let excluded = ["aw-tauri", "aw-client", "aw-cli", "aw-qt"];

// Get the discovery path from config
let config = crate::get_config();

// Get the current PATH
let path = env::var_os("PATH").unwrap_or_default();
let mut paths = env::split_paths(&path).collect::<Vec<_>>();

// Add discovery path if not already in PATH
if !paths.contains(&config.defaults.discovery_path) {
paths.push(config.defaults.discovery_path.clone());
}

// Create new PATH-like string
let new_paths = env::join_paths(paths).unwrap_or_default();

// Use the combined paths to find modules
env::split_paths(&new_paths)
.flat_map(|path| fs::read_dir(path).ok())
.flatten()
.filter_map(Result::ok)
.filter_map(|entry| {
let path = entry.path();
if path.is_file() && path.extension().map_or(false, |ext| ext == "exe") {
path.file_stem()?.to_str().map(String::from)
} else {
None
}
})
.unwrap_or_default()
.collect::<BTreeSet<_>>()
.into_iter()
.filter(|name| !excluded.contains(&name.as_str()))
.collect()
Expand Down

0 comments on commit fe4cbe9

Please sign in to comment.