feat(cli): properly fill target for TAURI_ env vars on mobile (#6116)

This commit is contained in:
Lucas Fernandes Nogueira 2023-01-23 13:26:13 -08:00 committed by GitHub
parent 78eaadae2e
commit 1af9be904a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 133 additions and 77 deletions

View File

@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---
Fixes `TAURI_*` environment variables for hook scripts on mobile commands.

View File

@ -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: {

View File

@ -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()
@ -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()

View File

@ -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},

View File

@ -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},
@ -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,
@ -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();

View File

@ -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},
@ -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,
@ -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,
@ -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();
@ -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,
@ -133,30 +149,21 @@ 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)
}
},
)
@ -164,14 +171,12 @@ fn run_dev(
#[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,
@ -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,

View File

@ -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},

View File

@ -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},
@ -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,
@ -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();

View File

@ -1,8 +1,9 @@
use super::{
device_prompt, ensure_init, env, init_dot_cargo, open_and_wait, with_config, MobileTarget,
APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME,
device_prompt, ensure_init, env, init_dot_cargo, open_and_wait, setup_dev_config, with_config,
MobileTarget, APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME,
};
use crate::{
dev::Options as DevOptions,
helpers::flock,
interface::{AppSettings, Interface, MobileOptions, Options as InterfaceOptions},
mobile::{write_options, CliOptions, DevChild, DevProcess},
@ -12,7 +13,7 @@ use clap::{ArgAction, Parser};
use dialoguer::{theme::ColorfulTheme, Select};
use tauri_mobile::{
apple::{config::Config as AppleConfig, teams::find_development_teams},
apple::{config::Config as AppleConfig, device::Device, teams::find_development_teams},
config::app::App,
env::Env,
opts::{NoiseLevel, Profile},
@ -48,7 +49,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,
@ -105,12 +106,28 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
}
fn run_dev(
options: Options,
mut options: Options,
app: &App,
config: &AppleConfig,
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-apple-ios".into()),
);
let mut interface = crate::dev::setup(&mut dev_options, true)?;
let app_settings = interface.app_settings();
@ -121,13 +138,11 @@ fn run_dev(
let out_dir = bin_path.parent().unwrap();
let _lock = flock::open_rw(&out_dir.join("lock").with_extension("ios"), "iOS")?;
let env = env()?;
init_dot_cargo(app, None)?;
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,
@ -148,23 +163,21 @@ fn run_dev(
if open {
open_and_wait(config, &env)
} else {
match run(device.as_deref(), options, config, &env) {
} else if let Some(device) = &device {
match run(device, options, config, &env) {
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)
}
},
)
@ -172,13 +185,11 @@ fn run_dev(
#[derive(Debug, thiserror::Error)]
enum RunError {
#[error("{0}")]
FailedToPromptForDevice(String),
#[error("{0}")]
RunFailed(String),
}
fn run(
device: Option<&str>,
device: &Device<'_>,
options: MobileOptions,
config: &AppleConfig,
env: &Env,
@ -189,8 +200,7 @@ fn run(
Profile::Release
};
device_prompt(env, device)
.map_err(|e| RunError::FailedToPromptForDevice(e.to_string()))?
device
.run(
config,
env,

View File

@ -3,7 +3,12 @@
// SPDX-License-Identifier: MIT
use crate::{
helpers::{app_paths::tauri_dir, config::Config as TauriConfig},
helpers::{
app_paths::tauri_dir,
config::{
get as get_config, reload as reload_config, AppUrl, Config as TauriConfig, WindowUrl,
},
},
interface::{AppInterface, AppSettings, DevProcess, Interface, Options as InterfaceOptions},
};
use anyhow::{bail, Result};
@ -127,6 +132,43 @@ pub struct CliOptions {
pub vars: HashMap<String, OsString>,
}
fn setup_dev_config(config_extension: &mut Option<String>) -> crate::Result<()> {
let config = get_config(config_extension.as_deref())?;
let mut dev_path = config
.lock()
.unwrap()
.as_ref()
.unwrap()
.build
.dev_path
.clone();
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 = crate::dev::local_ip_address();
url.set_host(Some(&ip.to_string())).unwrap();
if let Some(c) = config_extension {
let mut c: tauri_utils::config::Config = serde_json::from_str(c)?;
c.build.dev_path = dev_path.clone();
config_extension.replace(serde_json::to_string(&c).unwrap());
} else {
config_extension.replace(format!(r#"{{ "build": {{ "devPath": "{}" }} }}"#, url));
}
reload_config(config_extension.as_deref())?;
}
}
Ok(())
}
fn env_vars() -> HashMap<String, OsString> {
let mut vars = HashMap::new();
vars.insert("RUST_LOG_STYLE".into(), "always".into());