mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-28 17:38:53 +02:00
feat(updater): support bundle-specific targets (#2624)
* fallback targets * linux test * linux ready * RPM installation * small error fix * fix windows build * windows tests * add aider files to .gitignore * get bundle type out of patched variable * windows tests * patch windows binary * format * fix bundler * remove local tauri dependency * remove print * rever Cargo.lock * move __TAURI_BUNDLE_TYPE to tauri::utils * get_current_bundle_type * update tauri * fix macos integration test * fix fallback logic Signed-off-by: Krzysztof Andrelczyk <cristof@curiana.net> * amend! fallback targets fallback targets * reformat * fix tests * reformat * bump tari versio * fix fallback logic * restore Cargo.lock * Bump tauri and add notes * Rename some staffs * move target logic * Refactor the target fallback to a function * Format and clippy * Keep target in `Update` since it's public * Keep updater/tests/app-updater/src/main.rs lf * Revert changes in tests/app-updater/src/main.rs * Clean up * changefile * Bump updater-js as well * update pub fn target docs * update pub fn target docs * Update plugins/updater/src/error.rs Co-authored-by: Fabian-Lars <github@fabianlars.de> * Update plugins/updater/src/updater.rs Co-authored-by: Fabian-Lars <github@fabianlars.de> * Update plugins/updater/src/updater.rs Co-authored-by: Fabian-Lars <github@fabianlars.de> * suggestios * add comment * restore error * Revert "Bump tauri and add notes" This reverts commit0a495ccc6a. * Revert "bump tari versio" This reverts commit5b4c1c164b. --------- Signed-off-by: Krzysztof Andrelczyk <cristof@curiana.net> Co-authored-by: Lucas Nogueira <lucas@tauri.app> Co-authored-by: Tony <legendmastertony@gmail.com> Co-authored-by: Fabian-Lars <github@fabianlars.de>
This commit is contained in:
co-authored by
Fabian-Lars
Lucas Nogueira
Tony
parent
c247410319
commit
9ac5fe84e7
@@ -39,9 +39,14 @@ pub enum Error {
|
||||
/// `reqwest` crate errors.
|
||||
#[error(transparent)]
|
||||
Reqwest(#[from] reqwest::Error),
|
||||
/// The platform was not found on the updater JSON response.
|
||||
#[error("the platform `{0}` was not found on the response `platforms` object")]
|
||||
/// The platform was not found in the updater JSON response.
|
||||
#[error("the platform `{0}` was not found in the response `platforms` object")]
|
||||
TargetNotFound(String),
|
||||
/// Neither the platform nor the fallback platform was found in the updater JSON response.
|
||||
#[error(
|
||||
"None of the fallback platforms `{0:?}` were found in the response `platforms` object"
|
||||
)]
|
||||
TargetsNotFound(Vec<String>),
|
||||
/// Download failed
|
||||
#[error("`{0}`")]
|
||||
Network(String),
|
||||
@@ -69,6 +74,8 @@ pub enum Error {
|
||||
AuthenticationFailed,
|
||||
#[error("Failed to install .deb package")]
|
||||
DebInstallFailed,
|
||||
#[error("Failed to install package")]
|
||||
PackageInstallFailed,
|
||||
#[error("invalid updater binary format")]
|
||||
InvalidUpdaterFormat,
|
||||
#[error(transparent)]
|
||||
|
||||
+151
-80
@@ -26,7 +26,13 @@ use reqwest::{
|
||||
};
|
||||
use semver::Version;
|
||||
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize};
|
||||
use tauri::{utils::platform::current_exe, AppHandle, Resource, Runtime};
|
||||
use tauri::{
|
||||
utils::{
|
||||
config::BundleType,
|
||||
platform::{bundle_type, current_exe},
|
||||
},
|
||||
AppHandle, Resource, Runtime,
|
||||
};
|
||||
use time::OffsetDateTime;
|
||||
use url::Url;
|
||||
|
||||
@@ -37,6 +43,31 @@ use crate::{
|
||||
|
||||
const UPDATER_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum Installer {
|
||||
AppImage,
|
||||
Deb,
|
||||
Rpm,
|
||||
|
||||
App,
|
||||
|
||||
Msi,
|
||||
Nsis,
|
||||
}
|
||||
|
||||
impl Installer {
|
||||
fn name(self) -> &'static str {
|
||||
match self {
|
||||
Self::AppImage => "appimage",
|
||||
Self::Deb => "deb",
|
||||
Self::Rpm => "rpm",
|
||||
Self::App => "app",
|
||||
Self::Msi => "msi",
|
||||
Self::Nsis => "nsis",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct ReleaseManifestPlatform {
|
||||
/// Download URL for the platform
|
||||
@@ -265,13 +296,7 @@ impl UpdaterBuilder {
|
||||
return Err(Error::EmptyEndpoints);
|
||||
};
|
||||
|
||||
let arch = get_updater_arch().ok_or(Error::UnsupportedArch)?;
|
||||
let (target, json_target) = if let Some(target) = self.target {
|
||||
(target.clone(), target)
|
||||
} else {
|
||||
let target = get_updater_target().ok_or(Error::UnsupportedOs)?;
|
||||
(target.to_string(), format!("{target}-{arch}"))
|
||||
};
|
||||
let arch = updater_arch().ok_or(Error::UnsupportedArch)?;
|
||||
|
||||
let executable_path = self.executable_path.clone().unwrap_or(current_exe()?);
|
||||
|
||||
@@ -294,8 +319,7 @@ impl UpdaterBuilder {
|
||||
installer_args: self.installer_args,
|
||||
current_exe_args: self.current_exe_args,
|
||||
arch,
|
||||
target,
|
||||
json_target,
|
||||
target: self.target,
|
||||
headers: self.headers,
|
||||
extract_path,
|
||||
on_before_exit: self.on_before_exit,
|
||||
@@ -327,10 +351,9 @@ pub struct Updater {
|
||||
proxy: Option<Url>,
|
||||
endpoints: Vec<Url>,
|
||||
arch: &'static str,
|
||||
// The `{{target}}` variable we replace in the endpoint
|
||||
target: String,
|
||||
// The value we search if the updater server returns a JSON with the `platforms` object
|
||||
json_target: String,
|
||||
// The `{{target}}` variable we replace in the endpoint and serach for in the JSON,
|
||||
// this is either the user provided target or the current operating system by default
|
||||
target: Option<String>,
|
||||
headers: HeaderMap,
|
||||
extract_path: PathBuf,
|
||||
on_before_exit: Option<OnBeforeExit>,
|
||||
@@ -359,6 +382,11 @@ impl Updater {
|
||||
std::env::set_var("SSL_CERT_DIR", "/etc/ssl/certs");
|
||||
}
|
||||
}
|
||||
let target = if let Some(target) = &self.target {
|
||||
target
|
||||
} else {
|
||||
updater_os().ok_or(Error::UnsupportedOs)?
|
||||
};
|
||||
|
||||
let mut remote_release: Option<RemoteRelease> = None;
|
||||
let mut raw_json: Option<serde_json::Value> = None;
|
||||
@@ -381,11 +409,11 @@ impl Updater {
|
||||
.to_string()
|
||||
// url::Url automatically url-encodes the path components
|
||||
.replace("%7B%7Bcurrent_version%7D%7D", &encoded_version)
|
||||
.replace("%7B%7Btarget%7D%7D", &self.target)
|
||||
.replace("%7B%7Btarget%7D%7D", target)
|
||||
.replace("%7B%7Barch%7D%7D", self.arch)
|
||||
// but not query parameters
|
||||
.replace("{{current_version}}", &encoded_version)
|
||||
.replace("{{target}}", &self.target)
|
||||
.replace("{{target}}", target)
|
||||
.replace("{{arch}}", self.arch)
|
||||
.parse()?;
|
||||
|
||||
@@ -466,6 +494,9 @@ impl Updater {
|
||||
None => release.version > self.current_version,
|
||||
};
|
||||
|
||||
let installer = installer_for_bundle_type(bundle_type());
|
||||
let (download_url, signature) = self.get_urls(&release, &installer)?;
|
||||
|
||||
let update = if should_update {
|
||||
Some(Update {
|
||||
run_on_main_thread: self.run_on_main_thread.clone(),
|
||||
@@ -473,12 +504,12 @@ impl Updater {
|
||||
on_before_exit: self.on_before_exit.clone(),
|
||||
app_name: self.app_name.clone(),
|
||||
current_version: self.current_version.to_string(),
|
||||
target: self.target.clone(),
|
||||
target: target.to_owned(),
|
||||
extract_path: self.extract_path.clone(),
|
||||
version: release.version.to_string(),
|
||||
date: release.pub_date,
|
||||
download_url: release.download_url(&self.json_target)?.to_owned(),
|
||||
signature: release.signature(&self.json_target)?.to_owned(),
|
||||
download_url: download_url.clone(),
|
||||
signature: signature.to_owned(),
|
||||
body: release.notes,
|
||||
raw_json: raw_json.unwrap(),
|
||||
timeout: None,
|
||||
@@ -494,6 +525,38 @@ impl Updater {
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
fn get_urls<'a>(
|
||||
&self,
|
||||
release: &'a RemoteRelease,
|
||||
installer: &Option<Installer>,
|
||||
) -> Result<(&'a Url, &'a String)> {
|
||||
// Use the user provided target
|
||||
if let Some(target) = &self.target {
|
||||
return Ok((release.download_url(target)?, release.signature(target)?));
|
||||
}
|
||||
|
||||
// Or else we search for [`{os}-{arch}-{installer}`, `{os}-{arch}`] in order
|
||||
let os = updater_os().ok_or(Error::UnsupportedOs)?;
|
||||
let arch = self.arch;
|
||||
let mut targets = Vec::new();
|
||||
if let Some(installer) = installer {
|
||||
let installer = installer.name();
|
||||
targets.push(format!("{os}-{arch}-{installer}"));
|
||||
}
|
||||
targets.push(format!("{os}-{arch}"));
|
||||
|
||||
for target in &targets {
|
||||
log::debug!("Searching for updater target '{target}' in release data");
|
||||
if let (Ok(download_url), Ok(signature)) =
|
||||
(release.download_url(target), release.signature(target))
|
||||
{
|
||||
return Ok((download_url, signature));
|
||||
};
|
||||
}
|
||||
|
||||
Err(Error::TargetsNotFound(targets))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -511,7 +574,8 @@ pub struct Update {
|
||||
pub version: String,
|
||||
/// Update publish date
|
||||
pub date: Option<OffsetDateTime>,
|
||||
/// Target
|
||||
/// The `{{target}}` variable we replace in the endpoint and search for in the JSON,
|
||||
/// this is either the user provided target or the current operating system by default
|
||||
pub target: String,
|
||||
/// Download URL announced
|
||||
pub download_url: Url,
|
||||
@@ -852,11 +916,10 @@ impl Update {
|
||||
/// └── ...
|
||||
///
|
||||
fn install_inner(&self, bytes: &[u8]) -> Result<()> {
|
||||
if self.is_deb_package() {
|
||||
self.install_deb(bytes)
|
||||
} else {
|
||||
// Handle AppImage or other formats
|
||||
self.install_appimage(bytes)
|
||||
match installer_for_bundle_type(bundle_type()) {
|
||||
Some(Installer::Deb) => self.install_deb(bytes),
|
||||
Some(Installer::Rpm) => self.install_rpm(bytes),
|
||||
_ => self.install_appimage(bytes),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -933,39 +996,6 @@ impl Update {
|
||||
Err(Error::TempDirNotOnSameMountPoint)
|
||||
}
|
||||
|
||||
fn is_deb_package(&self) -> bool {
|
||||
// First check if we're in a typical Debian installation path
|
||||
let in_system_path = self
|
||||
.extract_path
|
||||
.to_str()
|
||||
.map(|p| p.starts_with("/usr"))
|
||||
.unwrap_or(false);
|
||||
|
||||
if !in_system_path {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Then verify it's actually a Debian-based system by checking for dpkg
|
||||
let dpkg_exists = std::path::Path::new("/var/lib/dpkg").exists();
|
||||
let apt_exists = std::path::Path::new("/etc/apt").exists();
|
||||
|
||||
// Additional check for the package in dpkg database
|
||||
let package_in_dpkg = if let Ok(output) = std::process::Command::new("dpkg")
|
||||
.args(["-S", &self.extract_path.to_string_lossy()])
|
||||
.output()
|
||||
{
|
||||
output.status.success()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
// Consider it a deb package only if:
|
||||
// 1. We're in a system path AND
|
||||
// 2. We have Debian package management tools AND
|
||||
// 3. The binary is tracked by dpkg
|
||||
dpkg_exists && apt_exists && package_in_dpkg
|
||||
}
|
||||
|
||||
fn install_deb(&self, bytes: &[u8]) -> Result<()> {
|
||||
// First verify the bytes are actually a .deb package
|
||||
if !infer::archive::is_deb(bytes) {
|
||||
@@ -973,6 +1003,18 @@ impl Update {
|
||||
return Err(Error::InvalidUpdaterFormat);
|
||||
}
|
||||
|
||||
self.try_tmp_locations(bytes, "dpkg", "-i")
|
||||
}
|
||||
|
||||
fn install_rpm(&self, bytes: &[u8]) -> Result<()> {
|
||||
// First verify the bytes are actually a .rpm package
|
||||
if !infer::archive::is_rpm(bytes) {
|
||||
return Err(Error::InvalidUpdaterFormat);
|
||||
}
|
||||
self.try_tmp_locations(bytes, "rpm", "-U")
|
||||
}
|
||||
|
||||
fn try_tmp_locations(&self, bytes: &[u8], install_cmd: &str, install_arg: &str) -> Result<()> {
|
||||
// Try different temp directories
|
||||
let tmp_dir_locations = vec![
|
||||
Box::new(|| Some(std::env::temp_dir())) as Box<dyn FnOnce() -> Option<PathBuf>>,
|
||||
@@ -984,15 +1026,19 @@ impl Update {
|
||||
for tmp_dir_location in tmp_dir_locations {
|
||||
if let Some(path) = tmp_dir_location() {
|
||||
if let Ok(tmp_dir) = tempfile::Builder::new()
|
||||
.prefix("tauri_deb_update")
|
||||
.prefix("tauri_rpm_update")
|
||||
.tempdir_in(path)
|
||||
{
|
||||
let deb_path = tmp_dir.path().join("package.deb");
|
||||
let pkg_path = tmp_dir.path().join("package.rpm");
|
||||
|
||||
// Try writing the .deb file
|
||||
if std::fs::write(&deb_path, bytes).is_ok() {
|
||||
if std::fs::write(&pkg_path, bytes).is_ok() {
|
||||
// If write succeeds, proceed with installation
|
||||
return self.try_install_with_privileges(&deb_path);
|
||||
return self.try_install_with_privileges(
|
||||
&pkg_path,
|
||||
install_cmd,
|
||||
install_arg,
|
||||
);
|
||||
}
|
||||
// If write fails, continue to next temp location
|
||||
}
|
||||
@@ -1003,12 +1049,17 @@ impl Update {
|
||||
Err(Error::TempDirNotFound)
|
||||
}
|
||||
|
||||
fn try_install_with_privileges(&self, deb_path: &Path) -> Result<()> {
|
||||
fn try_install_with_privileges(
|
||||
&self,
|
||||
pkg_path: &Path,
|
||||
install_cmd: &str,
|
||||
install_arg: &str,
|
||||
) -> Result<()> {
|
||||
// 1. First try using pkexec (graphical sudo prompt)
|
||||
if let Ok(status) = std::process::Command::new("pkexec")
|
||||
.arg("dpkg")
|
||||
.arg("-i")
|
||||
.arg(deb_path)
|
||||
.arg(install_cmd)
|
||||
.arg(install_arg)
|
||||
.arg(pkg_path)
|
||||
.status()
|
||||
{
|
||||
if status.success() {
|
||||
@@ -1019,7 +1070,7 @@ impl Update {
|
||||
|
||||
// 2. Try zenity or kdialog for a graphical sudo experience
|
||||
if let Ok(password) = self.get_password_graphically() {
|
||||
if self.install_with_sudo(deb_path, &password)? {
|
||||
if self.install_with_sudo(pkg_path, &password, install_cmd, install_arg)? {
|
||||
log::debug!("installed deb with GUI sudo");
|
||||
return Ok(());
|
||||
}
|
||||
@@ -1027,16 +1078,16 @@ impl Update {
|
||||
|
||||
// 3. Final fallback: terminal sudo
|
||||
let status = std::process::Command::new("sudo")
|
||||
.arg("dpkg")
|
||||
.arg("-i")
|
||||
.arg(deb_path)
|
||||
.arg(install_cmd)
|
||||
.arg(install_arg)
|
||||
.arg(pkg_path)
|
||||
.status()?;
|
||||
|
||||
if status.success() {
|
||||
log::debug!("installed deb with sudo");
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::DebInstallFailed)
|
||||
Err(Error::PackageInstallFailed)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1070,15 +1121,21 @@ impl Update {
|
||||
Err(Error::AuthenticationFailed)
|
||||
}
|
||||
|
||||
fn install_with_sudo(&self, deb_path: &Path, password: &str) -> Result<bool> {
|
||||
fn install_with_sudo(
|
||||
&self,
|
||||
pkg_path: &Path,
|
||||
password: &str,
|
||||
install_cmd: &str,
|
||||
install_arg: &str,
|
||||
) -> Result<bool> {
|
||||
use std::io::Write;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
let mut child = Command::new("sudo")
|
||||
.arg("-S") // read password from stdin
|
||||
.arg("dpkg")
|
||||
.arg("-i")
|
||||
.arg(deb_path)
|
||||
.arg(install_cmd)
|
||||
.arg(install_arg)
|
||||
.arg(pkg_path)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
@@ -1086,7 +1143,7 @@ impl Update {
|
||||
|
||||
if let Some(mut stdin) = child.stdin.take() {
|
||||
// Write password to stdin
|
||||
writeln!(stdin, "{}", password)?;
|
||||
writeln!(stdin, "{password}")?;
|
||||
}
|
||||
|
||||
let status = child.wait()?;
|
||||
@@ -1199,16 +1256,18 @@ impl Update {
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the target string used on the updater.
|
||||
/// Gets the base target string used by the updater. If bundle type is available it
|
||||
/// will be added to this string when selecting the download URL and signature.
|
||||
/// `tauri::utils::platform::bundle_type` method is used to obtain current bundle type.
|
||||
pub fn target() -> Option<String> {
|
||||
if let (Some(target), Some(arch)) = (get_updater_target(), get_updater_arch()) {
|
||||
if let (Some(target), Some(arch)) = (updater_os(), updater_arch()) {
|
||||
Some(format!("{target}-{arch}"))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_updater_target() -> Option<&'static str> {
|
||||
fn updater_os() -> Option<&'static str> {
|
||||
if cfg!(target_os = "linux") {
|
||||
Some("linux")
|
||||
} else if cfg!(target_os = "macos") {
|
||||
@@ -1221,7 +1280,7 @@ pub(crate) fn get_updater_target() -> Option<&'static str> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_updater_arch() -> Option<&'static str> {
|
||||
fn updater_arch() -> Option<&'static str> {
|
||||
if cfg!(target_arch = "x86") {
|
||||
Some("i686")
|
||||
} else if cfg!(target_arch = "x86_64") {
|
||||
@@ -1315,6 +1374,18 @@ impl<'de> Deserialize<'de> for RemoteRelease {
|
||||
}
|
||||
}
|
||||
|
||||
fn installer_for_bundle_type(bundle: Option<BundleType>) -> Option<Installer> {
|
||||
match bundle? {
|
||||
BundleType::Deb => Some(Installer::Deb),
|
||||
BundleType::Rpm => Some(Installer::Rpm),
|
||||
BundleType::AppImage => Some(Installer::AppImage),
|
||||
BundleType::Msi => Some(Installer::Msi),
|
||||
BundleType::Nsis => Some(Installer::Nsis),
|
||||
BundleType::App => Some(Installer::App), // App is also returned for Dmg type
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_version<'de, D>(deserializer: D) -> std::result::Result<Version, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
|
||||
Reference in New Issue
Block a user