feat: CLI

This commit is contained in:
2025-03-06 23:29:13 +00:00
parent cb813a7050
commit 48dc9ec4df
8 changed files with 161 additions and 15 deletions

View File

@ -10,18 +10,20 @@ mod download_handler;
mod appdata;
mod display;
use anyhow::anyhow;
use closure::closure;
use appdata::AppData;
use model::misc::Game;
use pkg::PkgKey;
use profile::Profile;
use tauri::{Listener, Manager};
use tauri_plugin_deep_link::DeepLinkExt;
use tauri_plugin_cli::CliExt;
use tokio::{sync::Mutex, fs, try_join};
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub async fn run(_args: Vec<String>) {
simple_logger::init_with_env()
.expect("Unable to initialize the logger");
simple_logger::init_with_env().expect("Unable to initialize the logger");
log::info!(
"Running from {}",
@ -62,6 +64,7 @@ pub async fn run(_args: Vec<String>) {
}
}
}))
.plugin(tauri_plugin_cli::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_deep_link::init())
.plugin(tauri_plugin_dialog::init())
@ -72,7 +75,38 @@ pub async fn run(_args: Vec<String>) {
util::init_dirs(&apph);
let app_data = AppData::new(app.handle().clone());
let mut app_data = AppData::new(app.handle().clone());
let start_immediately;
if let Ok(matches) = app.cli().matches() {
let start_arg = matches.args.get("start").expect("Invalid argument configuration");
let game_arg = matches.args.get("game").expect("Invalid argument configuration");
let name_arg = matches.args.get("name").expect("Invalid argument configuration");
log::debug!("{:?} {:?} {:?}", start_arg, game_arg, name_arg);
if start_arg.occurrences > 0 {
start_immediately = true;
app_data.remain_open = false;
} else {
tauri::WebviewWindowBuilder::new(app, "main", tauri::WebviewUrl::App("index.html".into()))
.title("STARTLINER")
.inner_size(600f64, 500f64)
.min_inner_size(600f64, 500f64)
.build()?;
start_immediately = false;
}
if game_arg.occurrences == 1 && name_arg.occurrences == 1 {
let game = game_arg.value.as_str().unwrap();
let name = name_arg.value.as_str().unwrap();
app_data.switch_profile(
Game::from_str(game).ok_or_else(|| anyhow!("Invalid game"))?,
name.to_owned()
)?;
}
} else {
return Err(anyhow!("Invalid command line arguments").into());
}
app.manage(Mutex::new(app_data));
app.deep_link().register_all()?;
@ -103,6 +137,36 @@ pub async fn run(_args: Vec<String>) {
});
}));
app.listen("launch-end", closure!(clone apph, |_| {
let apph = apph.clone();
tauri::async_runtime::spawn(async move {
let mutex = apph.state::<Mutex<AppData>>();
let appd = mutex.lock().await;
if !appd.remain_open {
apph.exit(0);
}
});
}));
if start_immediately == true {
let apph_clone = apph.clone();
tauri::async_runtime::spawn(async {
let apph_clone_clone = apph_clone.clone();
{
let mtx = apph_clone.state::<Mutex<AppData>>();
let mut appd = mtx.lock().await;
if let Err(e) = appd.pkgs.reload_all().await {
log::error!("Unable to reload packages: {}", e);
apph_clone.exit(1);
}
}
if let Err(e) = cmd::startline(apph_clone).await {
log::error!("Unable to launch: {}", e);
apph_clone_clone.exit(1);
}
});
}
Ok(())
})
.invoke_handler(tauri::generate_handler![