Skip to content

Commit

Permalink
feat: keep CLI alive when iOS app exits, show logs, closes #5855 (#5902)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Dec 27, 2022
1 parent ced8e9a commit dee9460
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 37 deletions.
6 changes: 6 additions & 0 deletions .changes/ios-keep-alive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Keep the process alive even when the iOS application is closed.
7 changes: 7 additions & 0 deletions .changes/ios-logs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"cli.rs": patch
"cli.js": patch
"tauri": patch
---

Show all application logs on iOS.
6 changes: 6 additions & 0 deletions .changes/mobile-env-vars-rename.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-build": patch
"tauri-macros": patch
---

Refactor mobile environment variables.
10 changes: 8 additions & 2 deletions core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,20 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
let s = config.tauri.bundle.identifier.split('.');
let last = s.clone().count() - 1;
let mut domain = String::new();
let mut android_package_prefix = String::new();
for (i, w) in s.enumerate() {
if i != last {
domain.push_str(w);
domain.push('_');
domain.push('.');

android_package_prefix.push_str(w);
android_package_prefix.push('_');
}
}
domain.pop();
println!("cargo:rustc-env=TAURI_ANDROID_DOMAIN={domain}");
android_package_prefix.pop();
println!("cargo:rustc-env=TAURI_MOBILE_DOMAIN={domain}");
println!("cargo:rustc-env=TAURI_ANDROID_PACKAGE_PREFIX={android_package_prefix}");

cfg_alias("dev", !has_feature("custom-protocol"));

Expand Down
7 changes: 5 additions & 2 deletions core/tauri-macros/src/mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ pub fn entry_point(_attributes: TokenStream, item: TokenStream) -> TokenStream {
let function_name = function.sig.ident.clone();

let mut error = None;
let domain = get_env_var("TAURI_ANDROID_DOMAIN", |r| r, &mut error, &function);
let domain = get_env_var("TAURI_ANDROID_PACKAGE_PREFIX", |r| r, &mut error, &function);
let app_name = get_env_var(
"CARGO_PKG_NAME",
|r| r.replace('_', "_1"),
&mut error,
&function,
);
let domain_str = var("TAURI_MOBILE_DOMAIN").unwrap();
let app_name_str = var("CARGO_PKG_NAME").unwrap();

if let Some(e) = error {
Expand All @@ -59,9 +60,11 @@ pub fn entry_point(_attributes: TokenStream, item: TokenStream) -> TokenStream {
#function

fn _start_app() {
::tauri::init_logging(#app_name_str);
#[cfg(target_os = "ios")]
::tauri::init_logging(&format!("{}.{}", #domain_str, #app_name_str));
#[cfg(target_os = "android")]
{
::tauri::init_logging(#app_name_str);
use ::tauri::paste;
::tauri::wry_android_binding!(#domain, #app_name, _start_app, ::tauri::wry);
}
Expand Down
4 changes: 3 additions & 1 deletion core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ android_logger = "0.9"
log = "0.4"

[target."cfg(target_os = \"ios\")".dependencies]
env_logger = "0.9.0"
oslog = "0.2"
log = "0.4"
libc = "0.2"

[build-dependencies]
heck = "0.4"
Expand Down
40 changes: 38 additions & 2 deletions core/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,44 @@ pub fn init_logging(tag: &str) {

#[cfg(target_os = "ios")]
#[doc(hidden)]
pub fn init_logging(_tag: &str) {
env_logger::init();
pub fn init_logging(subsystem: &str) {
use std::{
ffi::CString,
fs::File,
io::{BufRead, BufReader},
os::unix::prelude::*,
thread,
};

let mut logpipe: [RawFd; 2] = Default::default();
unsafe {
libc::pipe(logpipe.as_mut_ptr());
libc::dup2(logpipe[1], libc::STDOUT_FILENO);
libc::dup2(logpipe[1], libc::STDERR_FILENO);
}
thread::spawn(move || unsafe {
let file = File::from_raw_fd(logpipe[0]);
let mut reader = BufReader::new(file);
let mut buffer = String::new();
loop {
buffer.clear();
if let Ok(len) = reader.read_line(&mut buffer) {
if len == 0 {
break;
} else if let Ok(msg) = CString::new(buffer.clone())
.map_err(|_| ())
.and_then(|c| c.into_string().map_err(|_| ()))
{
log::info!("{}", msg);
}
}
}
});

oslog::OsLogger::new(subsystem)
.level_filter(log::LevelFilter::Trace)
.init()
.unwrap();
}

/// Updater events.
Expand Down
54 changes: 30 additions & 24 deletions examples/api/src-tauri/Cargo.lock

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

4 changes: 2 additions & 2 deletions tooling/cli/Cargo.lock

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

2 changes: 1 addition & 1 deletion tooling/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ name = "cargo-tauri"
path = "src/main.rs"

[dependencies]
tauri-mobile = { version = "0.1", default-features = false }
tauri-mobile = { version = "0.1.1", default-features = false }
textwrap = { version = "0.11.0", features = [ "term_size" ] }
jsonrpsee = { version = "0.16", features = [ "client", "server" ] }
thiserror = "1"
Expand Down
4 changes: 1 addition & 3 deletions tooling/cli/src/mobile/ios/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,13 @@ fn run(
Profile::Release
};

let non_interactive = true; // ios-deploy --noninteractive (quit when app crashes or exits)

device_prompt(env, device)
.map_err(|e| RunError::FailedToPromptForDevice(e.to_string()))?
.run(
config,
env,
NoiseLevel::FranklyQuitePedantic,
non_interactive,
false, // do not quit on app exit
profile,
)
.map(DevChild::new)
Expand Down

0 comments on commit dee9460

Please sign in to comment.