mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-04-03 10:11:15 +02:00
feat(cli): use templates from wry (#5030)
This commit is contained in:
committed by
GitHub
parent
ff4cb56b2e
commit
752ad3b203
2
examples/api/src-tauri/Cargo.lock
generated
2
examples/api/src-tauri/Cargo.lock
generated
@@ -4222,7 +4222,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "wry"
|
||||
version = "0.20.2"
|
||||
source = "git+https://github.com/tauri-apps/wry?branch=dev#1b26d605d6e33f5417eb6566a7381d8feb239c8b"
|
||||
source = "git+https://github.com/tauri-apps/wry?branch=dev#b4789034dc4d10ab83f6acce6b4152d79f702940"
|
||||
dependencies = [
|
||||
"block",
|
||||
"cocoa",
|
||||
|
||||
2
tooling/cli/Cargo.lock
generated
2
tooling/cli/Cargo.lock
generated
@@ -250,7 +250,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "cargo-mobile"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/tauri-apps/cargo-mobile?branch=dev#d5b4c4dd5ac23e700f07e362b8788d80a33d21d3"
|
||||
source = "git+https://github.com/tauri-apps/cargo-mobile?branch=dev#5b866af17df4e2310e084efd3550ec31f62ff984"
|
||||
dependencies = [
|
||||
"cocoa",
|
||||
"colored 1.9.3",
|
||||
|
||||
@@ -7,11 +7,12 @@ use cargo_mobile::{
|
||||
adb,
|
||||
config::{Config as AndroidConfig, Metadata as AndroidMetadata},
|
||||
device::{Device, RunError},
|
||||
env::{Env, Error as EnvError},
|
||||
env::{Env, Error as AndroidEnvError},
|
||||
target::{BuildError, Target},
|
||||
},
|
||||
config::Config,
|
||||
device::PromptError,
|
||||
env::Error as EnvError,
|
||||
os,
|
||||
util::prompt,
|
||||
};
|
||||
@@ -35,6 +36,8 @@ enum Error {
|
||||
#[error(transparent)]
|
||||
EnvInitFailed(EnvError),
|
||||
#[error(transparent)]
|
||||
AndroidEnvInitFailed(AndroidEnvError),
|
||||
#[error(transparent)]
|
||||
InitDotCargo(super::init::Error),
|
||||
#[error("invalid tauri configuration: {0}")]
|
||||
InvalidTauriConfig(String),
|
||||
@@ -105,6 +108,11 @@ fn with_config<T>(
|
||||
f(&config, config.android(), metadata.android())
|
||||
}
|
||||
|
||||
fn env() -> Result<Env, Error> {
|
||||
let env = super::env().map_err(Error::EnvInitFailed)?;
|
||||
cargo_mobile::android::env::Env::from_env(env).map_err(Error::AndroidEnvInitFailed)
|
||||
}
|
||||
|
||||
fn device_prompt<'a>(env: &'_ Env) -> Result<Device<'a>, PromptError<adb::device_list::Error>> {
|
||||
let device_list =
|
||||
adb::device_list(env).map_err(|cause| PromptError::detection_failed("Android", cause))?;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use super::{detect_target_ok, ensure_init, init_dot_cargo, with_config, Error, MobileTarget};
|
||||
use super::{detect_target_ok, ensure_init, env, init_dot_cargo, with_config, Error, MobileTarget};
|
||||
use crate::Result;
|
||||
use clap::Parser;
|
||||
|
||||
use cargo_mobile::{
|
||||
android::{env::Env, target::Target},
|
||||
android::target::Target,
|
||||
opts::{NoiseLevel, Profile},
|
||||
target::{call_for_targets_with_fallback, TargetTrait},
|
||||
};
|
||||
@@ -37,7 +37,7 @@ pub fn command(options: Options) -> Result<()> {
|
||||
ensure_init(config.project_dir(), MobileTarget::Android)
|
||||
.map_err(|e| Error::ProjectNotInitialized(e.to_string()))?;
|
||||
|
||||
let env = Env::new().map_err(Error::EnvInitFailed)?;
|
||||
let env = env()?;
|
||||
init_dot_cargo(root_conf, Some(&env)).map_err(Error::InitDotCargo)?;
|
||||
|
||||
call_for_targets_with_fallback(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{ensure_init, init_dot_cargo, log_finished, with_config, Error, MobileTarget};
|
||||
use super::{ensure_init, env, init_dot_cargo, log_finished, with_config, Error, MobileTarget};
|
||||
use crate::{
|
||||
helpers::{config::get as get_tauri_config, flock},
|
||||
interface::{AppSettings, Interface, Options as InterfaceOptions},
|
||||
@@ -64,7 +64,7 @@ pub fn command(options: Options) -> Result<()> {
|
||||
ensure_init(config.project_dir(), MobileTarget::Android)
|
||||
.map_err(|e| Error::ProjectNotInitialized(e.to_string()))?;
|
||||
|
||||
let env = Env::new().map_err(Error::EnvInitFailed)?;
|
||||
let env = env()?;
|
||||
init_dot_cargo(root_conf, Some(&env)).map_err(Error::InitDotCargo)?;
|
||||
|
||||
run_build(options, config, env).map_err(|e| Error::BuildFailed(format!("{:#}", e)))
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{device_prompt, ensure_init, init_dot_cargo, with_config, Error, MobileTarget};
|
||||
use super::{device_prompt, ensure_init, env, init_dot_cargo, with_config, Error, MobileTarget};
|
||||
use crate::{
|
||||
helpers::{config::get as get_tauri_config, flock},
|
||||
interface::{AppSettings, Interface, MobileOptions, Options as InterfaceOptions},
|
||||
@@ -8,10 +8,7 @@ use crate::{
|
||||
use clap::Parser;
|
||||
|
||||
use cargo_mobile::{
|
||||
android::{
|
||||
config::{Config as AndroidConfig, Metadata as AndroidMetadata},
|
||||
env::Env,
|
||||
},
|
||||
android::config::{Config as AndroidConfig, Metadata as AndroidMetadata},
|
||||
config::Config,
|
||||
opts::{NoiseLevel, Profile},
|
||||
os,
|
||||
@@ -143,7 +140,7 @@ fn run(
|
||||
|
||||
let build_app_bundle = metadata.asset_packs().is_some();
|
||||
|
||||
let env = Env::new().map_err(Error::EnvInitFailed)?;
|
||||
let env = env()?;
|
||||
init_dot_cargo(root_conf, Some(&env)).map_err(Error::InitDotCargo)?;
|
||||
|
||||
device_prompt(&env)
|
||||
|
||||
@@ -18,7 +18,7 @@ use cargo_mobile::{
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
use super::{
|
||||
ensure_init, env_vars, get_config,
|
||||
ensure_init, env, get_config,
|
||||
init::{command as init_command, init_dot_cargo, Options as InitOptions},
|
||||
log_finished, Target as MobileTarget,
|
||||
};
|
||||
@@ -35,7 +35,7 @@ mod xcode_script;
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
enum Error {
|
||||
#[error(transparent)]
|
||||
EnvInitFailed(EnvError),
|
||||
EnvInitFailed(#[from] EnvError),
|
||||
#[error(transparent)]
|
||||
InitDotCargo(super::init::Error),
|
||||
#[error("invalid tauri configuration: {0}")]
|
||||
@@ -116,13 +116,6 @@ fn with_config<T>(
|
||||
f(&config, config.apple(), metadata.apple())
|
||||
}
|
||||
|
||||
fn env() -> Result<Env, Error> {
|
||||
let env = Env::new()
|
||||
.map_err(Error::EnvInitFailed)?
|
||||
.explicit_env_vars(env_vars());
|
||||
Ok(env)
|
||||
}
|
||||
|
||||
fn device_prompt<'a>(env: &'_ Env) -> Result<Device<'a>, PromptError<ios_deploy::DeviceListError>> {
|
||||
let device_list =
|
||||
ios_deploy::device_list(env).map_err(|cause| PromptError::detection_failed("iOS", cause))?;
|
||||
|
||||
@@ -15,9 +15,17 @@ use cargo_mobile::{
|
||||
android::config::{Metadata as AndroidMetadata, Raw as RawAndroidConfig},
|
||||
bossy,
|
||||
config::{app::Raw as RawAppConfig, metadata::Metadata, Config, Raw},
|
||||
env::Error as EnvError,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{collections::HashMap, ffi::OsString, fmt::Write, path::PathBuf, process::ExitStatus};
|
||||
use std::{
|
||||
collections::HashMap, env::set_var, ffi::OsString, fmt::Write, path::PathBuf, process::ExitStatus,
|
||||
};
|
||||
|
||||
#[cfg(not(windows))]
|
||||
use cargo_mobile::env::Env;
|
||||
#[cfg(windows)]
|
||||
use cargo_mobile::os::Env;
|
||||
|
||||
pub mod android;
|
||||
mod init;
|
||||
@@ -110,13 +118,20 @@ fn env_vars() -> HashMap<String, OsString> {
|
||||
let mut vars = HashMap::new();
|
||||
for (k, v) in std::env::vars_os() {
|
||||
let k = k.to_string_lossy();
|
||||
if k.starts_with("TAURI") && k != "TAURI_PRIVATE_KEY" && k != "TAURI_KEY_PASSWORD" {
|
||||
if (k.starts_with("TAURI") && k != "TAURI_PRIVATE_KEY" && k != "TAURI_KEY_PASSWORD")
|
||||
|| k.starts_with("WRY")
|
||||
{
|
||||
vars.insert(k.into_owned(), v);
|
||||
}
|
||||
}
|
||||
vars
|
||||
}
|
||||
|
||||
fn env() -> Result<Env, EnvError> {
|
||||
let env = Env::new()?.explicit_env_vars(env_vars());
|
||||
Ok(env)
|
||||
}
|
||||
|
||||
/// Writes CLI options to be used later on the Xcode and Android Studio build commands
|
||||
pub fn write_options(
|
||||
mut options: CliOptions,
|
||||
@@ -133,7 +148,10 @@ pub fn write_options(
|
||||
|
||||
fn read_options(config: &TauriConfig, target: Target) -> crate::Result<CliOptions> {
|
||||
let data = std::fs::read_to_string(options_path(&config.tauri.bundle.identifier, target))?;
|
||||
let options = serde_json::from_str(&data)?;
|
||||
let options: CliOptions = serde_json::from_str(&data)?;
|
||||
for (k, v) in &options.vars {
|
||||
set_var(k, v);
|
||||
}
|
||||
Ok(options)
|
||||
}
|
||||
|
||||
@@ -147,6 +165,17 @@ fn get_config(config: &TauriConfig) -> (Config, Metadata) {
|
||||
}
|
||||
domain.pop();
|
||||
|
||||
let s = config.tauri.bundle.identifier.split('.');
|
||||
let last = s.clone().count() - 1;
|
||||
let mut reverse_domain = String::new();
|
||||
for (i, w) in s.enumerate() {
|
||||
if i != last {
|
||||
reverse_domain.push_str(w);
|
||||
reverse_domain.push('.');
|
||||
}
|
||||
}
|
||||
reverse_domain.pop();
|
||||
|
||||
let manifest_path = tauri_dir().join("Cargo.toml");
|
||||
let app_name = if let Ok(manifest) = crate::interface::manifest::read_manifest(&manifest_path) {
|
||||
manifest
|
||||
@@ -167,7 +196,7 @@ fn get_config(config: &TauriConfig) -> (Config, Metadata) {
|
||||
|
||||
let raw = Raw {
|
||||
app: RawAppConfig {
|
||||
name: app_name,
|
||||
name: app_name.clone(),
|
||||
stylized_name: config.package.product_name.clone(),
|
||||
domain,
|
||||
asset_dir: None,
|
||||
@@ -243,6 +272,21 @@ fn get_config(config: &TauriConfig) -> (Config, Metadata) {
|
||||
},
|
||||
};
|
||||
|
||||
set_var("WRY_ANDROID_REVERSED_DOMAIN", &reverse_domain);
|
||||
set_var("WRY_ANDROID_APP_NAME_SNAKE_CASE", &app_name);
|
||||
set_var(
|
||||
"WRY_ANDROID_KOTLIN_FILES_OUT_DIR",
|
||||
config
|
||||
.android()
|
||||
.project_dir()
|
||||
.join("app/src/main")
|
||||
.join(format!(
|
||||
"java/{}/{}",
|
||||
config.app().reverse_domain().replace('.', "/"),
|
||||
config.app().name()
|
||||
)),
|
||||
);
|
||||
|
||||
(config, metadata)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package {{reverse-domain app.domain}}.{{snake-case app.name}}
|
||||
|
||||
import android.webkit.*
|
||||
|
||||
class Ipc {
|
||||
@JavascriptInterface
|
||||
fun postMessage(message: String) {
|
||||
this.ipc(message)
|
||||
}
|
||||
|
||||
companion object {
|
||||
init {
|
||||
System.loadLibrary("{{snake-case app.name}}")
|
||||
}
|
||||
}
|
||||
|
||||
private external fun ipc(message: String)
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package {{reverse-domain app.domain}}.{{snake-case app.name}}
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.webkit.*
|
||||
|
||||
class RustWebViewClient(initScripts: Array<String>): WebViewClient() {
|
||||
private val initializationScripts: Array<String>
|
||||
|
||||
init {
|
||||
initializationScripts = initScripts
|
||||
}
|
||||
|
||||
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
|
||||
for (script in initializationScripts) {
|
||||
view?.evaluateJavascript(script, null)
|
||||
}
|
||||
super.onPageStarted(view, url, favicon)
|
||||
}
|
||||
|
||||
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun shouldInterceptRequest(
|
||||
view: WebView,
|
||||
request: WebResourceRequest
|
||||
): WebResourceResponse? {
|
||||
return handleRequest(request)
|
||||
}
|
||||
|
||||
companion object {
|
||||
init {
|
||||
System.loadLibrary("{{snake-case app.name}}")
|
||||
}
|
||||
}
|
||||
|
||||
private external fun handleRequest(request: WebResourceRequest): WebResourceResponse?
|
||||
}
|
||||
@@ -1,71 +1,6 @@
|
||||
package {{reverse-domain app.domain}}.{{snake-case app.name}}
|
||||
package {{app-domain-reversed}}.{{app-name-snake-case}}
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
|
||||
abstract class TauriActivity : AppCompatActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
create(this)
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
start()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
resume()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
pause()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
stop()
|
||||
}
|
||||
|
||||
override fun onWindowFocusChanged(hasFocus: Boolean) {
|
||||
super.onWindowFocusChanged(hasFocus)
|
||||
focus(hasFocus)
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
save()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
destroy()
|
||||
}
|
||||
|
||||
override fun onLowMemory() {
|
||||
super.onLowMemory()
|
||||
memory()
|
||||
}
|
||||
|
||||
fun getAppClass(name: String): Class<*> {
|
||||
return Class.forName(name)
|
||||
}
|
||||
|
||||
companion object {
|
||||
init {
|
||||
System.loadLibrary("{{snake-case app.name}}")
|
||||
}
|
||||
}
|
||||
|
||||
private external fun create(activity: TauriActivity)
|
||||
private external fun start()
|
||||
private external fun resume()
|
||||
private external fun pause()
|
||||
private external fun stop()
|
||||
private external fun save()
|
||||
private external fun destroy()
|
||||
private external fun memory()
|
||||
private external fun focus(focus: Boolean)
|
||||
}
|
||||
abstract class TauriActivity : AppCompatActivity()
|
||||
|
||||
Reference in New Issue
Block a user