Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding semver compatibility check for Windows and Linux #502

Merged
merged 7 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ serde = { version = "1", features = ["derive"] }
log = "0.4"
tauri = "1"
tauri-build = "1"
semver = "1"
serde_json = "1"
thiserror = "1"

Expand Down
1 change: 1 addition & 0 deletions plugins/single-instance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exclude = ["/examples"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
semver.workspace = true
serde.workspace = true
serde_json.workspace = true
tauri.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions plugins/single-instance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod platform_impl;
#[path = "platform_impl/macos.rs"]
mod platform_impl;

mod semver_compat;

pub(crate) type SingleInstanceCallback<R> =
dyn FnMut(&AppHandle<R>, Vec<String>, String) + Send + Sync + 'static;

Expand Down
19 changes: 14 additions & 5 deletions plugins/single-instance/src/platform_impl/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::sync::Arc;

use crate::SingleInstanceCallback;
use crate::{semver_compat::semver_compat_string, SingleInstanceCallback};
use tauri::{
plugin::{self, TauriPlugin},
AppHandle, Config, Manager, RunEvent, Runtime,
Expand All @@ -26,14 +26,17 @@ impl<R: Runtime> SingleInstanceDBus<R> {
}
}

fn dbus_id(config: Arc<Config>) -> String {
config.tauri.bundle.identifier.replace(['.', '-'], "_")
fn dbus_id(config: Arc<Config>, version: semver::Version) -> String {
let mut id = config.tauri.bundle.identifier.replace(['.', '-'], "_");
id.push('_');
id.push_str(semver_compat_string(version).as_str());
id
}

pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
plugin::Builder::new("single-instance")
.setup(|app| {
let id = dbus_id(app.config());
let id = dbus_id(app.config(), app.package_info().version.clone());
let single_instance_dbus = SingleInstanceDBus {
callback: f,
app_handle: app.clone(),
Expand Down Expand Up @@ -85,7 +88,13 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {

pub fn destroy<R: Runtime, M: Manager<R>>(manager: &M) {
if let Some(connection) = manager.try_state::<ConnectionHandle>() {
let dbus_name = format!("org.{}.SingleInstance", dbus_id(manager.config()));
let dbus_name = format!(
"org.{}.SingleInstance",
dbus_id(
manager.config(),
manager.app_handle().package_info().version.clone()
)
);
let _ = connection.0.release_name(dbus_name);
}
}
6 changes: 4 additions & 2 deletions plugins/single-instance/src/platform_impl/windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(target_os = "windows")]

use crate::SingleInstanceCallback;
use crate::{semver_compat::semver_compat_string, SingleInstanceCallback};
use std::ffi::CStr;
use tauri::{
plugin::{self, TauriPlugin},
Expand Down Expand Up @@ -29,7 +29,9 @@ const WMCOPYDATA_SINGLE_INSTANCE_DATA: usize = 1542;
pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
plugin::Builder::new("single-instance")
.setup(|app| {
let id = &app.config().tauri.bundle.identifier;
let mut id = (&app.config().tauri.bundle.identifier).to_owned();
id.push('_');
id.push_str(semver_compat_string(app.package_info().version.clone()).as_str());

let class_name = encode_wide(format!("{id}-sic"));
let window_name = encode_wide(format!("{id}-siw"));
Expand Down
15 changes: 15 additions & 0 deletions plugins/single-instance/src/semver_compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// Takes a version and spits out a String with trailing _x, thus only considering the digits
/// relevant regarding semver compatibility
pub fn semver_compat_string(version: semver::Version) -> String {
// for pre-release always treat each version separately
if !version.pre.is_empty() {
return version.to_string().replace(['.', '-'], "_");
}
match version.major {
0 => match version.minor {
0 => format!("0_0_{}", version.patch),
_ => format!("0_{}_x", version.minor),
},
_ => format!("{}_x_x", version.major),
}
}