|
| 1 | +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +//! In-app updates for Tauri applications. |
| 6 | +//! |
| 7 | +//! - Supported platforms: Windows, Linux and macOS.crypted database and secure runtime. |
| 8 | +
|
| 9 | +#![doc( |
| 10 | + html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png", |
| 11 | + html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png" |
| 12 | +)] |
| 13 | + |
| 14 | +use std::ffi::OsString; |
| 15 | + |
| 16 | +use tauri::{ |
| 17 | + plugin::{Builder as PluginBuilder, TauriPlugin}, |
| 18 | + Manager, Runtime, |
| 19 | +}; |
| 20 | + |
| 21 | +mod commands; |
| 22 | +mod config; |
| 23 | +mod error; |
| 24 | +mod updater; |
| 25 | + |
| 26 | +pub use config::Config; |
| 27 | +pub use error::{Error, Result}; |
| 28 | +pub use updater::*; |
| 29 | + |
| 30 | +/// Extensions to [`tauri::App`], [`tauri::AppHandle`], [`tauri::WebviewWindow`], [`tauri::Webview`] and [`tauri::Window`] to access the updater APIs. |
| 31 | +pub trait UpdaterExt<R: Runtime> { |
| 32 | + /// Gets the updater builder to build and updater |
| 33 | + /// that can manually check if an update is available. |
| 34 | + /// |
| 35 | + /// # Examples |
| 36 | + /// |
| 37 | + /// ```no_run |
| 38 | + /// use tauri_plugin_updater::UpdaterExt; |
| 39 | + /// tauri::Builder::default() |
| 40 | + /// .setup(|app| { |
| 41 | + /// let handle = app.handle().clone(); |
| 42 | + /// tauri::async_runtime::spawn(async move { |
| 43 | + /// let response = handle.updater_builder().build().unwrap().check().await; |
| 44 | + /// }); |
| 45 | + /// Ok(()) |
| 46 | + /// }); |
| 47 | + /// ``` |
| 48 | + fn updater_builder(&self) -> UpdaterBuilder; |
| 49 | + |
| 50 | + /// Gets the updater to manually check if an update is available. |
| 51 | + /// |
| 52 | + /// # Examples |
| 53 | + /// |
| 54 | + /// ```no_run |
| 55 | + /// use tauri_plugin_updater::UpdaterExt; |
| 56 | + /// tauri::Builder::default() |
| 57 | + /// .setup(|app| { |
| 58 | + /// let handle = app.handle().clone(); |
| 59 | + /// tauri::async_runtime::spawn(async move { |
| 60 | + /// let response = handle.updater().unwrap().check().await; |
| 61 | + /// }); |
| 62 | + /// Ok(()) |
| 63 | + /// }); |
| 64 | + /// ``` |
| 65 | + fn updater(&self) -> Result<Updater>; |
| 66 | +} |
| 67 | + |
| 68 | +impl<R: Runtime, T: Manager<R>> UpdaterExt<R> for T { |
| 69 | + fn updater_builder(&self) -> UpdaterBuilder { |
| 70 | + let app = self.app_handle(); |
| 71 | + let package_info = app.package_info(); |
| 72 | + let UpdaterState { config, target } = self.state::<UpdaterState>().inner(); |
| 73 | + |
| 74 | + let mut builder = UpdaterBuilder::new( |
| 75 | + package_info.name.clone(), |
| 76 | + package_info.version.clone(), |
| 77 | + config.clone(), |
| 78 | + ); |
| 79 | + |
| 80 | + if let Some(target) = target { |
| 81 | + builder = builder.target(target); |
| 82 | + } |
| 83 | + |
| 84 | + let args = self.env().args_os; |
| 85 | + if !args.is_empty() { |
| 86 | + builder = builder.current_exe_args(args); |
| 87 | + } |
| 88 | + |
| 89 | + #[cfg(any( |
| 90 | + target_os = "linux", |
| 91 | + target_os = "dragonfly", |
| 92 | + target_os = "freebsd", |
| 93 | + target_os = "netbsd", |
| 94 | + target_os = "openbsd" |
| 95 | + ))] |
| 96 | + { |
| 97 | + let env = app.env(); |
| 98 | + if let Some(appimage) = env.appimage { |
| 99 | + builder = builder.executable_path(appimage); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + let app_handle = app.app_handle().clone(); |
| 104 | + builder = builder.on_before_exit(move || { |
| 105 | + app_handle.cleanup_before_exit(); |
| 106 | + }); |
| 107 | + |
| 108 | + builder |
| 109 | + } |
| 110 | + |
| 111 | + fn updater(&self) -> Result<Updater> { |
| 112 | + self.updater_builder().build() |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +struct UpdaterState { |
| 117 | + target: Option<String>, |
| 118 | + config: Config, |
| 119 | +} |
| 120 | + |
| 121 | +#[derive(Default)] |
| 122 | +pub struct Builder { |
| 123 | + target: Option<String>, |
| 124 | + pubkey: Option<String>, |
| 125 | + installer_args: Vec<OsString>, |
| 126 | +} |
| 127 | + |
| 128 | +impl Builder { |
| 129 | + pub fn new() -> Self { |
| 130 | + Self::default() |
| 131 | + } |
| 132 | + |
| 133 | + pub fn target(mut self, target: impl Into<String>) -> Self { |
| 134 | + self.target.replace(target.into()); |
| 135 | + self |
| 136 | + } |
| 137 | + |
| 138 | + pub fn pubkey<S: Into<String>>(mut self, pubkey: S) -> Self { |
| 139 | + self.pubkey.replace(pubkey.into()); |
| 140 | + self |
| 141 | + } |
| 142 | + |
| 143 | + pub fn installer_args<I, S>(mut self, args: I) -> Self |
| 144 | + where |
| 145 | + I: IntoIterator<Item = S>, |
| 146 | + S: Into<OsString>, |
| 147 | + { |
| 148 | + let args = args.into_iter().map(|a| a.into()).collect::<Vec<_>>(); |
| 149 | + self.installer_args.extend_from_slice(&args); |
| 150 | + self |
| 151 | + } |
| 152 | + |
| 153 | + pub fn installer_arg<S>(mut self, arg: S) -> Self |
| 154 | + where |
| 155 | + S: Into<OsString>, |
| 156 | + { |
| 157 | + self.installer_args.push(arg.into()); |
| 158 | + self |
| 159 | + } |
| 160 | + |
| 161 | + pub fn clear_installer_args(mut self) -> Self { |
| 162 | + self.installer_args.clear(); |
| 163 | + self |
| 164 | + } |
| 165 | + |
| 166 | + pub fn build<R: Runtime>(self) -> TauriPlugin<R, Config> { |
| 167 | + let pubkey = self.pubkey; |
| 168 | + let target = self.target; |
| 169 | + let installer_args = self.installer_args; |
| 170 | + PluginBuilder::<R, Config>::new("updater") |
| 171 | + .setup(move |app, api| { |
| 172 | + let mut config = api.config().clone(); |
| 173 | + if let Some(pubkey) = pubkey { |
| 174 | + config.pubkey = pubkey; |
| 175 | + } |
| 176 | + if let Some(windows) = &mut config.windows { |
| 177 | + windows.installer_args.extend_from_slice(&installer_args); |
| 178 | + } |
| 179 | + app.manage(UpdaterState { target, config }); |
| 180 | + Ok(()) |
| 181 | + }) |
| 182 | + .invoke_handler(tauri::generate_handler![ |
| 183 | + commands::check, |
| 184 | + commands::download, |
| 185 | + commands::install, |
| 186 | + commands::download_and_install, |
| 187 | + ]) |
| 188 | + .build() |
| 189 | + } |
| 190 | +} |
0 commit comments