retrom_plugin_installer/
lib.rs

1use tauri::{
2    plugin::{Builder, TauriPlugin},
3    Manager, Runtime,
4};
5
6mod desktop;
7
8mod commands;
9mod error;
10
11pub use error::{Error, Result};
12
13use desktop::Installer;
14use tracing::{info_span, Instrument};
15
16/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the installer APIs.
17pub trait InstallerExt<R: Runtime> {
18    fn installer(&self) -> &Installer<R>;
19}
20
21impl<R: Runtime, T: Manager<R>> crate::InstallerExt<R> for T {
22    fn installer(&self) -> &Installer<R> {
23        self.state::<Installer<R>>().inner()
24    }
25}
26
27/// Initializes the plugin.
28#[tracing::instrument]
29pub fn init<R: Runtime>() -> TauriPlugin<R> {
30    Builder::<R>::new("installer")
31        .invoke_handler(tauri::generate_handler![
32            commands::install_game,
33            commands::uninstall_game,
34            commands::get_game_installation_status,
35            commands::get_installation_state,
36            commands::open_installation_dir,
37            commands::migrate_installation_dir,
38            commands::clear_installation_dir,
39            commands::update_steam_installations
40        ])
41        .setup(|app, api| {
42            let installer = desktop::init(app, api)?;
43            app.manage(installer);
44
45            let app = app.clone();
46            let (tx, rx) = std::sync::mpsc::channel::<crate::Result<()>>();
47
48            tauri::async_runtime::spawn_blocking(|| {
49                tauri::async_runtime::block_on(
50                    async move {
51                        let installer = app.installer();
52                        let res = installer.init_installation_index().await;
53
54                        tx.send(res).unwrap();
55                    }
56                    .instrument(info_span!("installer_setup")),
57                )
58            });
59
60            if let Err(why) = rx.recv().expect("Failed to receive from channel") {
61                tracing::error!("Failed to initialize installer: {:#?}", why);
62            }
63
64            Ok(())
65        })
66        .build()
67}