Fix recursive copy while bundling (#2419)

This commit is contained in:
Evan Almloff authored and GitHub committed 2024-06-07 12:43:17 -07:00
1 parent a35436ee4f
commit 7efe4d0e49
2 files changed
+58 -24

No files matched your search

+32 -19
View File
@@ -2,7 +2,7 @@ use crate::{assets::*, edits::EditQueue};
use dioxus_interpreter_js::eval::NATIVE_EVAL_JS;
use dioxus_interpreter_js::unified_bindings::SLEDGEHAMMER_JS;
use dioxus_interpreter_js::NATIVE_JS;
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};
use wry::{
http::{status::StatusCode, Request, Response},
RequestAsyncResponder, Result,
@@ -82,14 +82,8 @@ fn assets_head() -> Option<String> {
target_os = "openbsd"
))]
{
let root = crate::protocol::get_asset_root_or_default();
let assets_head_path = "__assets_head.html";
let mut head = root.join(assets_head_path);
// If we can't find it, add the dist directory and try again
// When bundling we currently copy the whole dist directory to the output directory instead of the individual files because of a limitation of cargo bundle2
if !head.exists() {
head = root.join("dist").join(assets_head_path);
}
let assets_head_path = PathBuf::from("__assets_head.html");
let head = resolve_resource(&assets_head_path);
match std::fs::read_to_string(&head) {
Ok(s) => Some(s),
Err(err) => {
@@ -112,6 +106,27 @@ fn assets_head() -> Option<String> {
}
}
fn resolve_resource(path: &Path) -> PathBuf {
let mut base_path = get_asset_root_or_default();
if running_in_dev_mode() {
base_path.push(path);
} else {
let mut resource_path = PathBuf::new();
for component in path.components() {
// Tauri-bundle inserts special path segments for abnormal component paths
match component {
Component::Prefix(_) => {}
Component::RootDir => resource_path.push("_root_"),
Component::CurDir => {}
Component::ParentDir => resource_path.push("_up_"),
Component::Normal(p) => resource_path.push(p),
}
}
base_path.push(resource_path);
}
base_path
}
/// Handle a request from the webview
///
/// - Tries to stream edits if they're requested.
@@ -157,19 +172,13 @@ pub(super) fn desktop_handler(
fn serve_from_fs(path: PathBuf) -> Result<Response<Vec<u8>>> {
// If the path is relative, we'll try to serve it from the assets directory.
let mut asset = get_asset_root_or_default().join(&path);
let mut asset = resolve_resource(&path);
// If we can't find it, make it absolute and try again
if !asset.exists() {
asset = PathBuf::from("/").join(&path);
}
// If we can't find it, add the dist directory and try again
// When bundling we currently copy the whole dist directory to the output directory instead of the individual files because of a limitation of cargo bundle2
if !asset.exists() {
asset = get_asset_root_or_default().join("dist").join(&path);
}
if !asset.exists() {
return Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
@@ -226,6 +235,12 @@ fn get_asset_root_or_default() -> PathBuf {
get_asset_root().unwrap_or_else(|| std::env::current_dir().unwrap())
}
fn running_in_dev_mode() -> bool {
// If running under cargo, there's no bundle!
// There might be a smarter/more resilient way of doing this
std::env::var_os("CARGO").is_some()
}
/// Get the asset directory, following tauri/cargo-bundles directory discovery approach
///
/// Currently supports:
@@ -237,9 +252,7 @@ fn get_asset_root_or_default() -> PathBuf {
/// - [ ] Android
#[allow(unreachable_code)]
fn get_asset_root() -> Option<PathBuf> {
// If running under cargo, there's no bundle!
// There might be a smarter/more resilient way of doing this
if std::env::var_os("CARGO").is_some() {
if running_in_dev_mode() {
return dioxus_cli_config::CURRENT_CONFIG
.as_ref()
.map(|c| c.out_dir())