Skip to content

Commit

Permalink
Fix recursive copy while bundling (#2419)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmloff committed Jun 7, 2024
1 parent a35436e commit 7efe4d0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
31 changes: 26 additions & 5 deletions packages/cli/src/cli/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::panic;
use dioxus_cli_config::ExecutableType;
use std::{fs::create_dir_all, str::FromStr};
use std::{env::current_dir, fs::create_dir_all, str::FromStr};

use tauri_bundler::{BundleSettings, PackageSettings, SettingsBuilder};

Expand Down Expand Up @@ -153,10 +153,31 @@ impl Bundle {

let static_asset_output_dir = static_asset_output_dir.display().to_string();
println!("Adding assets from {} to bundle", static_asset_output_dir);
if let Some(resources) = &mut bundle_settings.resources {
resources.push(static_asset_output_dir);
} else {
bundle_settings.resources = Some(vec![static_asset_output_dir]);

// Don't copy the executable or the old bundle directory
let ignored_files = [
crate_config.out_dir().join("bundle"),
crate_config.out_dir().join(name),
];

for entry in std::fs::read_dir(&static_asset_output_dir)?.flatten() {
let path = entry.path().canonicalize()?;
if ignored_files.iter().any(|f| path.starts_with(f)) {
continue;
}

// Tauri bundle will add a __root__ prefix if the input path is absolute even though the output path is relative?
// We strip the prefix here to make sure the input path is relative so that the bundler puts the output path in the right place
let path = path
.strip_prefix(&current_dir()?)
.unwrap()
.display()
.to_string();
if let Some(resources) = &mut bundle_settings.resources_map {
resources.insert(path, "".to_string());
} else {
bundle_settings.resources_map = Some([(path, "".to_string())].into());
}
}

let mut settings = SettingsBuilder::new()
Expand Down
51 changes: 32 additions & 19 deletions packages/desktop/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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())
Expand Down

0 comments on commit 7efe4d0

Please sign in to comment.