Skip to content

Commit

Permalink
fix(cli): builtin dev server should also be forwarded for Android (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 14, 2024
1 parent f35bcda commit f4cd68f
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/fix-cli-dev-server-android.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:bug
"@tauri-apps/cli": patch:bug
---

Fixes `android dev` not working when using the builtin dev server.
17 changes: 17 additions & 0 deletions tooling/cli/src/helpers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,20 @@ pub fn reload(merge_config: Option<&serde_json::Value>) -> crate::Result<ConfigH
Err(anyhow::anyhow!("config not loaded"))
}
}

/// merges the loaded config with the given value
pub fn merge_with(merge_config: &serde_json::Value) -> crate::Result<ConfigHandle> {
let handle = config_handle();
if let Some(config_metadata) = &mut *handle.lock().unwrap() {
let merge_config_str = serde_json::to_string(merge_config).unwrap();
set_var("TAURI_CONFIG", merge_config_str);

let mut value = serde_json::to_value(config_metadata.inner.clone())?;
merge(&mut value, merge_config);
config_metadata.inner = serde_json::from_value(value)?;

Ok(handle.clone())
} else {
Err(anyhow::anyhow!("config not loaded"))
}
}
4 changes: 2 additions & 2 deletions tooling/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use clap::{ArgAction, CommandFactory, FromArgMatches, Parser, Subcommand, ValueE
use env_logger::fmt::style::{AnsiColor, Style};
use env_logger::Builder;
use log::Level;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::io::{BufReader, Write};
use std::process::{exit, Command, ExitStatus, Output, Stdio};
use std::{
Expand All @@ -48,7 +48,7 @@ use std::{
};

/// Tauri configuration argument option.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigValue(pub(crate) serde_json::Value);

impl FromStr for ConfigValue {
Expand Down
6 changes: 6 additions & 0 deletions tooling/cli/src/mobile/android/android_studio_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ pub fn command(options: Options) -> Result<()> {
);
(config, metadata, cli_options)
};

ensure_init(
&tauri_config,
config.app(),
config.project_dir(),
MobileTarget::Android,
)?;

if let Some(config) = &cli_options.config {
crate::helpers::config::merge_with(&config.0)?;
}

let env = env()?;

if cli_options.dev {
Expand All @@ -77,6 +82,7 @@ pub fn command(options: Options) -> Result<()> {
.build
.dev_url
.clone();

if let Some(port) = dev_url.and_then(|url| url.port_or_known_default()) {
let forward = format!("tcp:{port}");
// ignore errors in case we do not have a device available
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/src/mobile/android/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ fn run_build(
args: build_options.args.clone(),
noise_level,
vars: Default::default(),
config: build_options.config.clone(),
};
let handle = write_options(
&tauri_config.lock().unwrap().as_ref().unwrap().identifier,
Expand Down
3 changes: 2 additions & 1 deletion tooling/cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn run_dev(
debug: !options.release_mode,
features: options.features,
args: Vec::new(),
config: options.config,
config: dev_options.config.clone(),
no_watch: options.no_watch,
},
|options| {
Expand All @@ -232,6 +232,7 @@ fn run_dev(
args: options.args.clone(),
noise_level,
vars: Default::default(),
config: dev_options.config.clone(),
};

let _handle = write_options(
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/src/mobile/ios/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ fn run_build(
args: build_options.args.clone(),
noise_level,
vars: Default::default(),
config: build_options.config.clone(),
};
let handle = write_options(
&tauri_config.lock().unwrap().as_ref().unwrap().identifier,
Expand Down
13 changes: 12 additions & 1 deletion tooling/cli/src/mobile/ios/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use std::{
sync::OnceLock,
};

const PHYSICAL_IPHONE_DEV_WARNING: &str = "To develop on physical phones you need the `--host` option (not required for Simulators). See the documentation for more information: https://v2.tauri.app/develop/#development-server";

#[derive(Debug, Clone, Parser)]
#[clap(
about = "Run your app in development mode on iOS",
Expand Down Expand Up @@ -367,6 +369,8 @@ fn run_dev(
let out_dir = bin_path.parent().unwrap();
let _lock = flock::open_rw(out_dir.join("lock").with_extension("ios"), "iOS")?;

let set_host = options.host.is_some();

configure_cargo(app, None)?;

let open = options.open;
Expand All @@ -377,7 +381,7 @@ fn run_dev(
debug: true,
features: options.features,
args: Vec::new(),
config: options.config,
config: dev_options.config.clone(),
no_watch: options.no_watch,
},
|options| {
Expand All @@ -387,13 +391,17 @@ fn run_dev(
args: options.args.clone(),
noise_level,
vars: Default::default(),
config: dev_options.config.clone(),
};
let _handle = write_options(
&tauri_config.lock().unwrap().as_ref().unwrap().identifier,
cli_options,
)?;

if open {
if !set_host {
log::warn!("{PHYSICAL_IPHONE_DEV_WARNING}");
}
open_and_wait(config, &env)
} else if let Some(device) = &device {
match run(device, options, config, &env) {
Expand All @@ -409,6 +417,9 @@ fn run_dev(
}
}
} else {
if !set_host {
log::warn!("{PHYSICAL_IPHONE_DEV_WARNING}");
}
open_and_wait(config, &env)
}
},
Expand Down
3 changes: 3 additions & 0 deletions tooling/cli/src/mobile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
config::{Config as TauriConfig, ConfigHandle},
},
interface::{AppInterface, AppSettings, DevProcess, Interface, Options as InterfaceOptions},
ConfigValue,
};
#[cfg(target_os = "macos")]
use anyhow::Context;
Expand Down Expand Up @@ -141,6 +142,7 @@ pub struct CliOptions {
pub args: Vec<String>,
pub noise_level: NoiseLevel,
pub vars: HashMap<String, OsString>,
pub config: Option<ConfigValue>,
}

impl Default for CliOptions {
Expand All @@ -151,6 +153,7 @@ impl Default for CliOptions {
args: vec!["--lib".into()],
noise_level: Default::default(),
vars: Default::default(),
config: None,
}
}
}
Expand Down

0 comments on commit f4cd68f

Please sign in to comment.