Skip to content

Commit

Permalink
feat(core): resources on mobile apps
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 20, 2024
1 parent 086271b commit 877c15c
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changes/inject-mobile-resources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:feat
"@tauri-apps/cli": patch:feat
---

Inject configured resources on mobil apps.
5 changes: 5 additions & 0 deletions .changes/resource-dir-ios.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-utils": patch:bug
---

Fix `resource_dir` on iOS.
5 changes: 5 additions & 0 deletions core/tauri-utils/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ fn resource_dir_from<P: AsRef<Path>>(
.map_err(Into::into);
}

#[cfg(target_os = "ios")]
{
res = exe_dir.join("assets").canonicalize().map_err(Into::into);
}

res
}

Expand Down
21 changes: 21 additions & 0 deletions tooling/cli/src/helpers/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use anyhow::Result;
use std::path::Path;

pub fn copy_file(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
let from = from.as_ref();
let to = to.as_ref();
if !from.exists() {
return Err(anyhow::anyhow!("{:?} does not exist", from));
}
if !from.is_file() {
return Err(anyhow::anyhow!("{:?} is not a file", from));
}
let dest_dir = to.parent().expect("No data in parent");
std::fs::create_dir_all(dest_dir)?;
std::fs::copy(from, to)?;
Ok(())
}
1 change: 1 addition & 0 deletions tooling/cli/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod cargo_manifest;
pub mod config;
pub mod flock;
pub mod framework;
pub mod fs;
pub mod npm;
pub mod plugins;
pub mod prompts;
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/mobile/android/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT

use super::{
configure_cargo, delete_codegen_vars, ensure_init, env, get_app, get_config, inject_assets,
configure_cargo, delete_codegen_vars, ensure_init, env, get_app, get_config, inject_resources,
log_finished, open_and_wait, MobileTarget, OptionsHandle,
};
use crate::{
Expand Down Expand Up @@ -209,7 +209,7 @@ fn run_build(
cli_options,
)?;

inject_assets(config, tauri_config.lock().unwrap().as_ref().unwrap())?;
inject_resources(config, tauri_config.lock().unwrap().as_ref().unwrap())?;

let apk_outputs = if options.apk {
apk::build(
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use super::{
configure_cargo, delete_codegen_vars, device_prompt, ensure_init, env, get_app, get_config,
inject_assets, open_and_wait, MobileTarget,
inject_resources, open_and_wait, MobileTarget,
};
use crate::{
dev::Options as DevOptions,
Expand Down Expand Up @@ -244,7 +244,7 @@ fn run_dev(
cli_options,
)?;

inject_assets(config, tauri_config.lock().unwrap().as_ref().unwrap())?;
inject_resources(config, tauri_config.lock().unwrap().as_ref().unwrap())?;

if open {
open_and_wait(config, &env)
Expand Down
21 changes: 19 additions & 2 deletions tooling/cli/src/mobile/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ use std::{
time::Duration,
};
use sublime_fuzzy::best_match;
use tauri_utils::resources::ResourcePaths;

use super::{
ensure_init, get_app,
init::{command as init_command, configure_cargo},
log_finished, read_options, CliOptions, OptionsHandle, Target as MobileTarget,
MIN_DEVICE_MATCH_SCORE,
};
use crate::{helpers::config::Config as TauriConfig, Result};
use crate::{
helpers::config::{BundleResources, Config as TauriConfig},
Result,
};

mod android_studio_script;
mod build;
Expand Down Expand Up @@ -296,7 +300,7 @@ fn open_and_wait(config: &AndroidConfig, env: &Env) -> ! {
}
}

fn inject_assets(config: &AndroidConfig, tauri_config: &TauriConfig) -> Result<()> {
fn inject_resources(config: &AndroidConfig, tauri_config: &TauriConfig) -> Result<()> {
let asset_dir = config
.project_dir()
.join("app/src/main")
Expand All @@ -308,5 +312,18 @@ fn inject_assets(config: &AndroidConfig, tauri_config: &TauriConfig) -> Result<(
serde_json::to_string(&tauri_config)?,
)?;

let resources = match &tauri_config.bundle.resources {
Some(BundleResources::List(paths)) => Some(ResourcePaths::new(paths.as_slice(), true)),
Some(BundleResources::Map(map)) => Some(ResourcePaths::from_map(map, true)),
None => None,
};
if let Some(resources) = resources {
for resource in resources.iter() {
let resource = resource?;
let dest = asset_dir.join(resource.target());
crate::helpers::fs::copy_file(resource.path(), dest)?;
}
}

Ok(())
}
4 changes: 2 additions & 2 deletions tooling/cli/src/mobile/ios/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT

use super::{
configure_cargo, detect_target_ok, ensure_init, env, get_app, get_config, inject_assets,
configure_cargo, detect_target_ok, ensure_init, env, get_app, get_config, inject_resources,
log_finished, merge_plist, open_and_wait, MobileTarget, OptionsHandle,
};
use crate::{
Expand Down Expand Up @@ -164,7 +164,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
config.project_dir(),
MobileTarget::Ios,
)?;
inject_assets(&config)?;
inject_resources(&config, tauri_config.lock().unwrap().as_ref().unwrap())?;

let info_plist_path = config
.project_dir()
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/mobile/ios/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT

use super::{
configure_cargo, device_prompt, ensure_init, env, get_app, get_config, inject_assets,
configure_cargo, device_prompt, ensure_init, env, get_app, get_config, inject_resources,
merge_plist, open_and_wait, MobileTarget,
};
use crate::{
Expand Down Expand Up @@ -179,7 +179,7 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
config.project_dir(),
MobileTarget::Ios,
)?;
inject_assets(&config)?;
inject_resources(&config, tauri_config.lock().unwrap().as_ref().unwrap())?;

let info_plist_path = config
.project_dir()
Expand Down
24 changes: 21 additions & 3 deletions tooling/cli/src/mobile/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use cargo_mobile2::{
};
use clap::{Parser, Subcommand};
use sublime_fuzzy::best_match;
use tauri_utils::resources::ResourcePaths;

use super::{
ensure_init, env, get_app,
Expand All @@ -28,7 +29,10 @@ use super::{
MIN_DEVICE_MATCH_SCORE,
};
use crate::{
helpers::{app_paths::tauri_dir, config::Config as TauriConfig},
helpers::{
app_paths::tauri_dir,
config::{BundleResources, Config as TauriConfig},
},
Result,
};

Expand Down Expand Up @@ -304,9 +308,23 @@ fn open_and_wait(config: &AppleConfig, env: &Env) -> ! {
}
}

fn inject_assets(config: &AppleConfig) -> Result<()> {
fn inject_resources(config: &AppleConfig, tauri_config: &TauriConfig) -> Result<()> {
let asset_dir = config.project_dir().join(DEFAULT_ASSET_DIR);
create_dir_all(asset_dir)?;
create_dir_all(&asset_dir)?;

let resources = match &tauri_config.bundle.resources {
Some(BundleResources::List(paths)) => Some(ResourcePaths::new(paths.as_slice(), true)),
Some(BundleResources::Map(map)) => Some(ResourcePaths::from_map(map, true)),
None => None,
};
if let Some(resources) = resources {
for resource in resources.iter() {
let resource = resource?;
let dest = asset_dir.join(resource.target());
crate::helpers::fs::copy_file(resource.path(), dest)?;
}
}

Ok(())
}

Expand Down

0 comments on commit 877c15c

Please sign in to comment.