Skip to content

Commit

Permalink
feat(cli): properly fill target for TAURI_ env vars on mobile (#6116)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jan 23, 2023
1 parent 78eaada commit 1af9be9
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 77 deletions.
6 changes: 6 additions & 0 deletions .changes/fix-mobile-env-vars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Fixes `TAURI_*` environment variables for hook scripts on mobile commands.
2 changes: 1 addition & 1 deletion examples/api/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { internalIpV4 } from 'internal-ip'

// https://vitejs.dev/config/
export default defineConfig(async ({ command, mode }) => {
const host = await internalIpV4()
const host = process.env.TAURI_PLATFORM === 'android' || process.env.TAURI_PLATFORM === 'ios' ? (await internalIpV4()) : 'localhost'
return {
plugins: [Unocss(), svelte()],
build: {
Expand Down
26 changes: 1 addition & 25 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn command_internal(mut options: Options) -> Result<()> {
})
}

fn local_ip_address() -> &'static IpAddr {
pub fn local_ip_address() -> &'static IpAddr {
static LOCAL_IP: OnceCell<IpAddr> = OnceCell::new();
LOCAL_IP.get_or_init(|| {
let addresses: Vec<IpAddr> = local_ip_address::list_afinet_netifas()
Expand Down Expand Up @@ -148,30 +148,6 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
.dev_path
.clone();

if mobile {
if let AppUrl::Url(WindowUrl::External(url)) = &mut dev_path {
let localhost = match url.host() {
Some(url::Host::Domain(d)) => d == "localhost",
Some(url::Host::Ipv4(i)) => {
i == std::net::Ipv4Addr::LOCALHOST || i == std::net::Ipv4Addr::UNSPECIFIED
}
_ => false,
};
if localhost {
let ip = local_ip_address();
url.set_host(Some(&ip.to_string())).unwrap();
if let Some(c) = &options.config {
let mut c: tauri_utils::config::Config = serde_json::from_str(c)?;
c.build.dev_path = dev_path.clone();
options.config = Some(serde_json::to_string(&c).unwrap());
} else {
options.config = Some(format!(r#"{{ "build": {{ "devPath": "{}" }} }}"#, url))
}
reload_config(options.config.as_deref())?;
}
}
}

if let Some(before_dev) = config
.lock()
.unwrap()
Expand Down
3 changes: 2 additions & 1 deletion tooling/cli/src/mobile/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use tauri_mobile::{
use super::{
ensure_init, get_app,
init::{command as init_command, init_dot_cargo},
log_finished, read_options, CliOptions, Target as MobileTarget, MIN_DEVICE_MATCH_SCORE,
log_finished, read_options, setup_dev_config, CliOptions, Target as MobileTarget,
MIN_DEVICE_MATCH_SCORE,
};
use crate::{
helpers::config::{get as get_tauri_config, Config as TauriConfig},
Expand Down
12 changes: 10 additions & 2 deletions tooling/cli/src/mobile/android/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::{
MobileTarget,
};
use crate::{
build::Options as BuildOptions,
helpers::flock,
interface::{AppSettings, Interface, Options as InterfaceOptions},
mobile::{write_options, CliOptions},
Expand Down Expand Up @@ -53,7 +54,7 @@ pub struct Options {
pub open: bool,
}

impl From<Options> for crate::build::Options {
impl From<Options> for BuildOptions {
fn from(options: Options) -> Self {
Self {
runner: None,
Expand Down Expand Up @@ -111,7 +112,14 @@ fn run_build(
options.aab = true;
}

let mut build_options = options.clone().into();
let mut build_options: BuildOptions = options.clone().into();
build_options.target = Some(
Target::all()
.get(Target::DEFAULT_KEY)
.unwrap()
.triple
.into(),
);
let interface = crate::build::setup(&mut build_options, true)?;

let app_settings = interface.app_settings();
Expand Down
54 changes: 29 additions & 25 deletions tooling/cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::{
delete_codegen_vars, device_prompt, ensure_init, env, init_dot_cargo, open_and_wait, with_config,
MobileTarget,
delete_codegen_vars, device_prompt, ensure_init, env, init_dot_cargo, open_and_wait,
setup_dev_config, with_config, MobileTarget,
};
use crate::{
dev::Options as DevOptions,
helpers::flock,
interface::{AppSettings, Interface, MobileOptions, Options as InterfaceOptions},
mobile::{write_options, CliOptions, DevChild, DevProcess},
Expand All @@ -13,6 +14,7 @@ use clap::{ArgAction, Parser};
use tauri_mobile::{
android::{
config::{Config as AndroidConfig, Metadata as AndroidMetadata},
device::Device,
env::Env,
},
config::app::App,
Expand Down Expand Up @@ -55,7 +57,7 @@ pub struct Options {
pub device: Option<String>,
}

impl From<Options> for crate::dev::Options {
impl From<Options> for DevOptions {
fn from(options: Options) -> Self {
Self {
runner: None,
Expand Down Expand Up @@ -89,13 +91,29 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
}

fn run_dev(
options: Options,
mut options: Options,
app: &App,
config: &AndroidConfig,
metadata: &AndroidMetadata,
noise_level: NoiseLevel,
) -> Result<()> {
let mut dev_options = options.clone().into();
setup_dev_config(&mut options.config)?;
let env = env()?;
let device = match device_prompt(&env, options.device.as_deref()) {
Ok(d) => Some(d),
Err(e) => {
log::error!("{e}");
None
}
};

let mut dev_options: DevOptions = options.clone().into();
dev_options.target = Some(
device
.as_ref()
.map(|d| d.target().triple.to_string())
.unwrap_or_else(|| "aarch64-linux-android".into()),
);
let mut interface = crate::dev::setup(&mut dev_options, true)?;

let app_settings = interface.app_settings();
Expand All @@ -106,13 +124,11 @@ fn run_dev(
let out_dir = bin_path.parent().unwrap();
let _lock = flock::open_rw(out_dir.join("lock").with_extension("android"), "Android")?;

let env = env()?;
init_dot_cargo(app, Some((&env, config)))?;

let open = options.open;
let exit_on_panic = options.exit_on_panic;
let no_watch = options.no_watch;
let device = options.device;
interface.mobile_dev(
MobileOptions {
debug: true,
Expand All @@ -133,45 +149,34 @@ fn run_dev(

if open {
open_and_wait(config, &env)
} else {
match run(
device.as_deref(),
options,
config,
&env,
metadata,
noise_level,
) {
} else if let Some(device) = &device {
match run(device, options, config, &env, metadata, noise_level) {
Ok(c) => {
crate::dev::wait_dev_process(c.clone(), move |status, reason| {
crate::dev::on_app_exit(status, reason, exit_on_panic, no_watch)
});
Ok(Box::new(c) as Box<dyn DevProcess>)
}
Err(RunError::FailedToPromptForDevice(e)) => {
log::error!("{}", e);
open_and_wait(config, &env)
}
Err(e) => {
crate::dev::kill_before_dev_process();
Err(e.into())
}
}
} else {
open_and_wait(config, &env)
}
},
)
}

#[derive(Debug, thiserror::Error)]
enum RunError {
#[error("{0}")]
FailedToPromptForDevice(String),
#[error("{0}")]
RunFailed(String),
}

fn run(
device: Option<&str>,
device: &Device<'_>,
options: MobileOptions,
config: &AndroidConfig,
env: &Env,
Expand All @@ -186,8 +191,7 @@ fn run(

let build_app_bundle = metadata.asset_packs().is_some();

device_prompt(env, device)
.map_err(|e| RunError::FailedToPromptForDevice(e.to_string()))?
device
.run(
config,
env,
Expand Down
3 changes: 2 additions & 1 deletion tooling/cli/src/mobile/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use tauri_mobile::{
use super::{
ensure_init, env, get_app,
init::{command as init_command, init_dot_cargo},
log_finished, read_options, CliOptions, Target as MobileTarget, MIN_DEVICE_MATCH_SCORE,
log_finished, read_options, setup_dev_config, CliOptions, Target as MobileTarget,
MIN_DEVICE_MATCH_SCORE,
};
use crate::{
helpers::config::{get as get_tauri_config, Config as TauriConfig},
Expand Down
12 changes: 10 additions & 2 deletions tooling/cli/src/mobile/ios/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::{
MobileTarget,
};
use crate::{
build::Options as BuildOptions,
helpers::flock,
interface::{AppSettings, Interface, Options as InterfaceOptions},
mobile::{write_options, CliOptions},
Expand Down Expand Up @@ -49,7 +50,7 @@ pub struct Options {
pub open: bool,
}

impl From<Options> for crate::build::Options {
impl From<Options> for BuildOptions {
fn from(options: Options) -> Self {
Self {
runner: None,
Expand Down Expand Up @@ -97,7 +98,14 @@ fn run_build(
Profile::Release
};

let mut build_options = options.clone().into();
let mut build_options: BuildOptions = options.clone().into();
build_options.target = Some(
Target::all()
.get(Target::DEFAULT_KEY)
.unwrap()
.triple
.into(),
);
let interface = crate::build::setup(&mut build_options, true)?;

let app_settings = interface.app_settings();
Expand Down
Loading

0 comments on commit 1af9be9

Please sign in to comment.