Skip to content

Commit 8aa131a

Browse files
committed
fallback targets
1 parent 6ae853c commit 8aa131a

File tree

4 files changed

+126
-42
lines changed

4 files changed

+126
-42
lines changed

plugins/process/permissions/schemas/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,4 @@
322322
]
323323
}
324324
}
325-
}
325+
}

plugins/updater/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pub enum Error {
3030
/// Operating system is not supported.
3131
#[error("Unsupported OS, expected one of `linux`, `darwin` or `windows`.")]
3232
UnsupportedOs,
33+
/// Can't determine which type of installer was used for the app
34+
#[error("Couldn't determinet installation method")]
35+
UnknownInstaller,
3336
/// Failed to determine updater package extract path
3437
#[error("Failed to determine updater package extract path.")]
3538
FailedToDetermineExtractPath,
@@ -42,6 +45,9 @@ pub enum Error {
4245
/// The platform was not found on the updater JSON response.
4346
#[error("the platform `{0}` was not found on the response `platforms` object")]
4447
TargetNotFound(String),
48+
/// Neither the platform not the fallback platform was not found on the updater JSON response.
49+
#[error("the platform `{0}` and `{1}` were not found on the response `platforms` object")]
50+
TargetsNotFound(String, String),
4551
/// Download failed
4652
#[error("`{0}`")]
4753
Network(String),

plugins/updater/src/updater.rs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,30 @@ pub struct RemoteRelease {
6868

6969
impl RemoteRelease {
7070
/// The release's download URL for the given target.
71-
pub fn download_url(&self, target: &str) -> Result<&Url> {
71+
pub fn download_url(&self, target: &str, fallback_target: &Option<String>) -> Result<&Url> {
7272
match self.data {
7373
RemoteReleaseInner::Dynamic(ref platform) => Ok(&platform.url),
7474
RemoteReleaseInner::Static { ref platforms } => platforms
7575
.get(target)
76-
.map_or(Err(Error::TargetNotFound(target.to_string())), |p| {
77-
Ok(&p.url)
78-
}),
76+
.map_or_else(
77+
|| match fallback_target {
78+
Some(fallback) => platforms.get(fallback).map_or(Err(Error::TargetsNotFound(target.to_string(), fallback.to_string())), |p| Ok(&p.url)),
79+
None => Err(Error::TargetNotFound(target.to_string()))
80+
}, |p| { Ok(&p.url) })
7981
}
8082
}
8183

8284
/// The release's signature for the given target.
83-
pub fn signature(&self, target: &str) -> Result<&String> {
85+
pub fn signature(&self, target: &str, fallback_target: &Option<String>) -> Result<&String> {
8486
match self.data {
8587
RemoteReleaseInner::Dynamic(ref platform) => Ok(&platform.signature),
8688
RemoteReleaseInner::Static { ref platforms } => platforms
8789
.get(target)
88-
.map_or(Err(Error::TargetNotFound(target.to_string())), |platform| {
89-
Ok(&platform.signature)
90-
}),
90+
.map_or_else(
91+
|| match fallback_target {
92+
Some(fallback) => platforms.get(fallback).map_or(Err(Error::TargetsNotFound(target.to_string(), fallback.to_string())), |p| Ok(&p.signature)),
93+
None => Err(Error::TargetNotFound(target.to_string()))
94+
}, |p| { Ok(&p.signature) })
9195
}
9296
}
9397
}
@@ -242,11 +246,16 @@ impl UpdaterBuilder {
242246
};
243247

244248
let arch = get_updater_arch().ok_or(Error::UnsupportedArch)?;
245-
let (target, json_target) = if let Some(target) = self.target {
246-
(target.clone(), target)
249+
let (target, json_target, fallback_target) = if let Some(target) = self.target {
250+
(target.clone(), target, None)
247251
} else {
248252
let target = get_updater_target().ok_or(Error::UnsupportedOs)?;
249-
(target.to_string(), format!("{target}-{arch}"))
253+
let installer = get_updater_installer()?;
254+
let json_target = format!("{target}-{arch}");
255+
match installer {
256+
Some(installer) => (target.to_owned(), format!("{json_target}-{installer}"), Some(json_target)),
257+
None => (target.to_owned(), json_target, None)
258+
}
250259
};
251260

252261
let executable_path = self.executable_path.clone().unwrap_or(current_exe()?);
@@ -271,6 +280,7 @@ impl UpdaterBuilder {
271280
arch,
272281
target,
273282
json_target,
283+
fallback_target,
274284
headers: self.headers,
275285
extract_path,
276286
on_before_exit: self.on_before_exit,
@@ -299,10 +309,12 @@ pub struct Updater {
299309
proxy: Option<Url>,
300310
endpoints: Vec<Url>,
301311
arch: &'static str,
302-
// The `{{target}}` variable we replace in the endpoint
312+
// The `{{target}}` variable we replace in the endpoint and serach for in the JSON
303313
target: String,
304314
// The value we search if the updater server returns a JSON with the `platforms` object
305315
json_target: String,
316+
// If target doesn't exist in the JSON check for this one
317+
fallback_target: Option<String>,
306318
headers: HeaderMap,
307319
extract_path: PathBuf,
308320
on_before_exit: Option<OnBeforeExit>,
@@ -317,7 +329,6 @@ impl Updater {
317329
// we want JSON only
318330
let mut headers = self.headers.clone();
319331
headers.insert("Accept", HeaderValue::from_str("application/json").unwrap());
320-
321332
// Set SSL certs for linux if they aren't available.
322333
#[cfg(target_os = "linux")]
323334
{
@@ -420,9 +431,9 @@ impl Updater {
420431
extract_path: self.extract_path.clone(),
421432
version: release.version.to_string(),
422433
date: release.pub_date,
423-
download_url: release.download_url(&self.json_target)?.to_owned(),
434+
download_url: release.download_url(&self.json_target, &self.fallback_target)?.to_owned(),
424435
body: release.notes.clone(),
425-
signature: release.signature(&self.json_target)?.to_owned(),
436+
signature: release.signature(&self.json_target, &self.fallback_target)?.to_owned(),
426437
raw_json: raw_json.unwrap(),
427438
timeout: self.timeout,
428439
proxy: self.proxy.clone(),
@@ -1099,6 +1110,7 @@ pub(crate) fn get_updater_target() -> Option<&'static str> {
10991110
}
11001111
}
11011112

1113+
11021114
pub(crate) fn get_updater_arch() -> Option<&'static str> {
11031115
if cfg!(target_arch = "x86") {
11041116
Some("i686")
@@ -1113,6 +1125,18 @@ pub(crate) fn get_updater_arch() -> Option<&'static str> {
11131125
}
11141126
}
11151127

1128+
pub(crate) fn get_updater_installer() -> Result<Option<&'static str>> {
1129+
if cfg!(target_os = "linux") {
1130+
Ok(Some("deb"))
1131+
} else if cfg!(target_os = "windows") {
1132+
Ok(Some("wix"))
1133+
} else if cfg!(target_os = "macos") {
1134+
Ok(None)
1135+
} else {
1136+
Err(Error::UnknownInstaller)
1137+
}
1138+
}
1139+
11161140
pub fn extract_path_from_executable(executable_path: &Path) -> Result<PathBuf> {
11171141
// Return the path of the current executable by default
11181142
// Example C:\Program Files\My App\

plugins/updater/tests/app-updater/tests/update.rs

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use tauri::utils::config::{Updater, V1Compatible};
1717

1818
const UPDATER_PRIVATE_KEY: &str = "dW50cnVzdGVkIGNvbW1lbnQ6IHJzaWduIGVuY3J5cHRlZCBzZWNyZXQga2V5ClJXUlRZMEl5TlFOMFpXYzJFOUdjeHJEVXY4WE1TMUxGNDJVUjNrMmk1WlR3UVJVUWwva0FBQkFBQUFBQUFBQUFBQUlBQUFBQUpVK3ZkM3R3eWhyN3hiUXhQb2hvWFVzUW9FbEs3NlNWYjVkK1F2VGFRU1FEaGxuRUtlell5U0gxYS9DbVRrS0YyZVJGblhjeXJibmpZeGJjS0ZKSUYwYndYc2FCNXpHalM3MHcrODMwN3kwUG9SOWpFNVhCSUd6L0E4TGRUT096TEtLR1JwT1JEVFU9Cg==";
1919
const UPDATED_EXIT_CODE: i32 = 0;
20+
const ERROR_EXIT_CODE: i32 = 1;
2021
const UP_TO_DATE_EXIT_CODE: i32 = 2;
2122

2223
#[derive(Serialize)]
@@ -45,7 +46,7 @@ struct Update {
4546
platforms: HashMap<String, PlatformUpdate>,
4647
}
4748

48-
fn build_app(cwd: &Path, config: &Config, bundle_updater: bool, target: BundleTarget) {
49+
fn build_app(cwd: &Path, config: &Config, bundle_updater: bool, targets: Vec<BundleTarget>) {
4950
let mut command = Command::new("cargo");
5051
command
5152
.args(["tauri", "build", "--debug", "--verbose"])
@@ -55,19 +56,20 @@ fn build_app(cwd: &Path, config: &Config, bundle_updater: bool, target: BundleTa
5556
.env("TAURI_SIGNING_PRIVATE_KEY_PASSWORD", "")
5657
.current_dir(cwd);
5758

59+
command.args(["--bundles"]);
5860
#[cfg(target_os = "linux")]
59-
command.args(["--bundles", target.name()]);
61+
command.args(targets.into_iter().map(|t| t.name()).collect::<Vec<&str>>());
6062
#[cfg(target_os = "macos")]
61-
command.args(["--bundles", target.name()]);
63+
command.args([target.name()]);
6264

6365
if bundle_updater {
6466
#[cfg(windows)]
65-
command.args(["--bundles", "msi", "nsis"]);
67+
command.args(["msi", "nsis"]);
6668

67-
command.args(["--bundles", "updater"]);
69+
command.args(["updater"]);
6870
} else {
6971
#[cfg(windows)]
70-
command.args(["--bundles", target.name()]);
72+
command.args([target.name()]);
7173
}
7274

7375
let status = command
@@ -82,6 +84,8 @@ fn build_app(cwd: &Path, config: &Config, bundle_updater: bool, target: BundleTa
8284
#[derive(Copy, Clone)]
8385
enum BundleTarget {
8486
AppImage,
87+
Deb,
88+
Rpm,
8589

8690
App,
8791

@@ -93,32 +97,82 @@ impl BundleTarget {
9397
fn name(self) -> &'static str {
9498
match self {
9599
Self::AppImage => "appimage",
100+
Self::Deb => "deb",
101+
Self::Rpm => "rpm",
96102
Self::App => "app",
97103
Self::Msi => "msi",
98104
Self::Nsis => "nsis",
99105
}
100106
}
101107
}
102108

103-
impl Default for BundleTarget {
104-
fn default() -> Self {
109+
impl BundleTarget {
110+
fn get_targets() -> Vec<Self> {
105111
#[cfg(any(target_os = "macos", target_os = "ios"))]
106-
return Self::App;
112+
return vec![Self::App];
107113
#[cfg(target_os = "linux")]
108-
return Self::AppImage;
114+
return vec![Self::AppImage, Self::Deb, Self::Rpm];
109115
#[cfg(windows)]
110-
return Self::Nsis;
116+
return vec![Self::Nsis];
117+
}
118+
}
119+
120+
fn insert_plaforms(
121+
bundle_target: BundleTarget,
122+
platforms: &mut HashMap<String, PlatformUpdate>,
123+
target: String,
124+
signature: String,
125+
) {
126+
match bundle_target {
127+
// Should use deb, no fallback
128+
BundleTarget::Deb => {
129+
platforms.insert(
130+
format!("{target}-deb"),
131+
PlatformUpdate {
132+
signature,
133+
url: "http://localhost:3007/download",
134+
with_elevated_task: false,
135+
},
136+
);
137+
}
138+
// Should fail
139+
BundleTarget::Rpm => {}
140+
// AppImage should use fallback
141+
_ => {
142+
platforms.insert(
143+
target,
144+
PlatformUpdate {
145+
signature,
146+
url: "http://localhost:3007/download",
147+
with_elevated_task: false,
148+
},
149+
);
150+
}
111151
}
112152
}
113153

114154
#[cfg(target_os = "linux")]
115155
fn bundle_paths(root_dir: &Path, version: &str) -> Vec<(BundleTarget, PathBuf)> {
116-
vec![(
117-
BundleTarget::AppImage,
118-
root_dir.join(format!(
119-
"target/debug/bundle/appimage/app-updater_{version}_amd64.AppImage"
120-
)),
121-
)]
156+
vec![
157+
(
158+
BundleTarget::AppImage,
159+
root_dir.join(format!(
160+
"target/debug/bundle/appimage/app-updater_{version}_amd64.AppImage"
161+
)),
162+
),
163+
(
164+
BundleTarget::Deb,
165+
root_dir.join(format!(
166+
"target/debug/bundle/deb/app-updater_{version}_amd64.deb"
167+
)),
168+
),
169+
(
170+
BundleTarget::Rpm,
171+
root_dir.join(format!(
172+
"target/debug/bundle/rpm/app-updater_{version}_amd64.rpm"
173+
)),
174+
),
175+
]
122176
}
123177

124178
#[cfg(target_os = "macos")]
@@ -161,7 +215,6 @@ fn bundle_paths(root_dir: &Path, version: &str) -> Vec<(BundleTarget, PathBuf)>
161215
}
162216

163217
#[test]
164-
#[ignore]
165218
fn update_app() {
166219
let target =
167220
tauri_plugin_updater::target().expect("running updater test in an unsupported platform");
@@ -188,7 +241,7 @@ fn update_app() {
188241
);
189242

190243
// bundle app update
191-
build_app(&manifest_dir, &config, true, Default::default());
244+
build_app(&manifest_dir, &config, true, BundleTarget::get_targets());
192245

193246
let updater_zip_ext = if v1_compatible {
194247
if cfg!(windows) {
@@ -249,14 +302,13 @@ fn update_app() {
249302
"/" => {
250303
let mut platforms = HashMap::new();
251304

252-
platforms.insert(
305+
insert_plaforms(
306+
bundle_target,
307+
&mut platforms,
253308
target.clone(),
254-
PlatformUpdate {
255-
signature: signature.clone(),
256-
url: "http://localhost:3007/download",
257-
with_elevated_task: false,
258-
},
309+
signature.clone(),
259310
);
311+
260312
let body = serde_json::to_vec(&Update {
261313
version: "1.0.0",
262314
date: time::OffsetDateTime::now_utc()
@@ -293,11 +345,13 @@ fn update_app() {
293345
config.version = "0.1.0";
294346

295347
// bundle initial app version
296-
build_app(&manifest_dir, &config, false, bundle_target);
348+
build_app(&manifest_dir, &config, false, vec![bundle_target]);
297349

298350
let status_checks = if matches!(bundle_target, BundleTarget::Msi) {
299351
// for msi we can't really check if the app was updated, because we can't change the install path
300352
vec![UPDATED_EXIT_CODE]
353+
} else if matches!(bundle_target, BundleTarget::Rpm) {
354+
vec![ERROR_EXIT_CODE]
301355
} else {
302356
vec![UPDATED_EXIT_CODE, UP_TO_DATE_EXIT_CODE]
303357
};

0 commit comments

Comments
 (0)