Skip to content

Commit ea6896c

Browse files
Merge pull request #285 from crabnebula-dev/chore/update-plugin
feat: expose connection info on plugin via tauri state
2 parents 00a430d + 0dae901 commit ea6896c

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

crates/devtools/src/lib.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener};
1212
use std::sync::Arc;
1313
use std::thread;
1414
use std::time::Duration;
15-
use tauri::Runtime;
15+
use tauri::{Manager, Runtime};
1616
use tokio::sync::mpsc;
1717
use tracing_subscriber::layer::SubscriberExt;
1818
use tracing_subscriber::util::SubscriberInitExt;
@@ -50,6 +50,10 @@ mod ios {
5050
));
5151
}
5252

53+
pub struct Devtools {
54+
pub connection: ConnectionInfo,
55+
}
56+
5357
fn init_plugin<R: Runtime>(
5458
addr: SocketAddr,
5559
publish_interval: Duration,
@@ -60,6 +64,10 @@ fn init_plugin<R: Runtime>(
6064
.setup(move |app_handle, _api| {
6165
let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
6266

67+
app_handle.manage(Devtools {
68+
connection: connection_info(&addr),
69+
});
70+
6371
health_reporter
6472
.set_serving::<TauriServer<server::TauriService<R>>>()
6573
.now_or_never()
@@ -280,7 +288,8 @@ impl Builder {
280288
// initialize early so we don't miss any spans
281289
tracing_subscriber::registry()
282290
.with(layer.with_filter(tracing_subscriber::filter::LevelFilter::TRACE))
283-
.try_init()?;
291+
.try_init()
292+
.map_err(devtools_core::Error::from)?;
284293

285294
let mut port = self.port;
286295
if !self.strict_port && !port_is_available(&self.host, port) {
@@ -300,16 +309,14 @@ fn port_is_available(host: &IpAddr, port: u16) -> bool {
300309
TcpListener::bind(SocketAddr::new(*host, port)).is_ok()
301310
}
302311

303-
fn print_link(addr: &SocketAddr) {
304-
let url = if option_env!("__DEVTOOLS_LOCAL_DEVELOPMENT").is_some() {
305-
"http://localhost:5173/dash/"
306-
} else {
307-
"https://devtools.crabnebula.dev/dash/"
308-
};
312+
pub struct ConnectionInfo {
313+
pub host: IpAddr,
314+
pub port: u16,
315+
}
309316

310-
let url = format!(
311-
"{url}{}/{}",
312-
if addr.ip() == Ipv4Addr::UNSPECIFIED {
317+
fn connection_info(addr: &SocketAddr) -> ConnectionInfo {
318+
ConnectionInfo {
319+
host: if addr.ip() == Ipv4Addr::UNSPECIFIED {
313320
#[cfg(target_os = "ios")]
314321
{
315322
local_ip_address::list_afinet_netifas()
@@ -333,8 +340,19 @@ fn print_link(addr: &SocketAddr) {
333340
} else {
334341
addr.ip()
335342
},
336-
addr.port()
337-
);
343+
port: addr.port(),
344+
}
345+
}
346+
347+
fn print_link(addr: &SocketAddr) {
348+
let url = if option_env!("__DEVTOOLS_LOCAL_DEVELOPMENT").is_some() {
349+
"http://localhost:5173/dash/"
350+
} else {
351+
"https://devtools.crabnebula.dev/dash/"
352+
};
353+
354+
let connection = connection_info(addr);
355+
let url = format!("{url}{}/{}", connection.host, connection.port);
338356

339357
#[cfg(target_os = "ios")]
340358
unsafe {

crates/devtools/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ mod tests {
313313
.await
314314
.unwrap();
315315

316-
// this will list this crates directory, so should produce the `Cargo.toml`, `build.rs`, `.gitignore`, `ios` and `src` entry
316+
// this will list this crates directory, so should produce the `Cargo.toml`, `build.rs`, `.gitignore`, `ios`, `permissions` and `src` entry
317317
let entries: Vec<_> = stream.into_inner().collect().await;
318318
assert!(entries.len() > 0);
319319
}

0 commit comments

Comments
 (0)