From c43991ecbd79c905340aea23643550f016b206c8 Mon Sep 17 00:00:00 2001 From: "poegl.dev" Date: Mon, 6 Nov 2023 08:47:38 +0100 Subject: [PATCH] refactor: Use Rust to get installed apps and consider also user apps --- src-tauri/Cargo.lock | 38 ++++++++++++++++++++++++++-- src-tauri/Cargo.toml | 1 + src-tauri/src/apps.rs | 28 ++++++++++++++++++++ src-tauri/src/main.rs | 12 +++++---- src-tauri/tauri.conf.json | 14 ---------- src/utils/get-installed-app-names.ts | 10 +++----- 6 files changed, 76 insertions(+), 27 deletions(-) create mode 100644 src-tauri/src/apps.rs diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 165eee3..a0ad849 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -384,6 +384,7 @@ name = "browsernaut" version = "1.0.1" dependencies = [ "enigo", + "rust_search", "serde", "serde_json", "swift-rs", @@ -832,13 +833,22 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys 0.3.7", +] + [[package]] name = "dirs" version = "5.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" dependencies = [ - "dirs-sys", + "dirs-sys 0.4.1", ] [[package]] @@ -851,6 +861,17 @@ dependencies = [ "dirs-sys-next", ] +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "dirs-sys" version = "0.4.1" @@ -3086,6 +3107,19 @@ dependencies = [ "windows 0.37.0", ] +[[package]] +name = "rust_search" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d27d7be20245d289c9dde663f06521de08663d73cbaefc45785aa65d02022378" +dependencies = [ + "dirs 4.0.0", + "ignore", + "num_cpus", + "regex", + "strsim", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -3845,7 +3879,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4536f5f6602e8fdfaa7b3b185076c2a0704f8eb7015f4e58461eb483ec3ed1f8" dependencies = [ - "dirs", + "dirs 5.0.1", "interprocess", "log", "objc2", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 155b7a2..3641eea 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -22,6 +22,7 @@ tauri-plugin-deep-link = "0.1.2" tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } enigo = "0.1.3" swift-rs = "1.0.6" +rust_search = "2.1.0" [features] diff --git a/src-tauri/src/apps.rs b/src-tauri/src/apps.rs new file mode 100644 index 0000000..efbc51e --- /dev/null +++ b/src-tauri/src/apps.rs @@ -0,0 +1,28 @@ +use rust_search::SearchBuilder; + +#[tauri::command] +pub fn get_apps() -> Vec { + let result: Vec; + result = search( + vec!["~/Applications", "/Applications", "/System/Applications"], + Some(".app"), + Some(1), + ); + return result; +} + +fn search( + search_locations: Vec<&str>, + extension: Option<&str>, + depth: Option, +) -> Vec { + let (location, more_locations) = search_locations.split_first().unwrap(); + let result: Vec = SearchBuilder::default() + .location(location) + .more_locations(more_locations.to_vec()) + .depth(depth.unwrap_or(1)) + .ext(extension.unwrap_or("*")) + .build() + .collect(); + return result; +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index afd73f3..2e7d05c 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,12 +1,13 @@ -use std::{env, path::PathBuf}; +mod apps; +use apps::get_apps; +use enigo::{Enigo, MouseControllable}; use serde_json::json; +use std::{env, path::PathBuf}; +use swift_rs::{swift, Bool, Int, SRString}; use tauri::{ ActivationPolicy, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, Wry, }; - -use enigo::{Enigo, MouseControllable}; -use swift_rs::{swift, Bool, Int, SRString}; use tauri_plugin_store::{with_store, StoreCollection}; swift!(fn get_default_browser() -> SRString); @@ -35,7 +36,8 @@ fn main() { make_default_browser, retrieve_app_icon, open_picker_window, - open_preferences_window + open_preferences_window, + get_apps ]) .setup(|app| { //? Allows application to receive and parse the URI passed in when opened. diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index db40e24..09660a0 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -30,20 +30,6 @@ "read", "~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure" ] - }, - { - "name": "findApps", - "cmd": "find", - "args": [ - "~/Applications", - "/Applications", - "-iname", - "*.app", - "-prune", - "-not", - "-path", - "\"*/.*\"" - ] } ] }, diff --git a/src/utils/get-installed-app-names.ts b/src/utils/get-installed-app-names.ts index b269bd5..8a90acb 100644 --- a/src/utils/get-installed-app-names.ts +++ b/src/utils/get-installed-app-names.ts @@ -1,12 +1,10 @@ -import { path } from '@tauri-apps/api'; - import { AppName, apps } from '@config/apps'; -import { Command } from '@tauri-apps/api/shell'; +import { path } from '@tauri-apps/api'; +import { invoke } from '@tauri-apps/api/tauri' async function getAllInstalledAppNames(): Promise { - const output = await new Command('findApps').execute(); - const appNamesRaw = output.stdout.trim().split('\n'); - const appNamePromises = appNamesRaw.map(async (appPath) => { + const apps = await invoke('get_apps') as Array + const appNamePromises = apps.map(async (appPath) => { const baseName = await path.basename(appPath); const appName = baseName.substring(0, baseName.length - 4); return appName;