From 7e00d354f389244876cbcba7ea85be0a413aef59 Mon Sep 17 00:00:00 2001 From: Tim Jentzsch Date: Mon, 23 Dec 2024 10:56:20 +0100 Subject: [PATCH] Add copying of assets folder --- Cargo.lock | 7 +++++ Cargo.toml | 3 +++ src/run/bundle.rs | 69 +++++++++++++++++++++++++++-------------------- 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a933dfaf..562612d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -492,6 +492,7 @@ dependencies = [ "cargo-generate", "clap", "dialoguer", + "fs_extra", "regex", "reqwest", "semver", @@ -1536,6 +1537,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "fs_extra" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" + [[package]] name = "futures-channel" version = "0.3.31" diff --git a/Cargo.toml b/Cargo.toml index 36ae76d0..4f0d2e65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,3 +42,6 @@ actix-web = "4.9.0" # Opening the app in the browser webbrowser = "1.0.2" + +# Copying directories +fs_extra = "1.3.0" diff --git a/src/run/bundle.rs b/src/run/bundle.rs index 3f5df2cb..3a68eb51 100644 --- a/src/run/bundle.rs +++ b/src/run/bundle.rs @@ -26,7 +26,7 @@ pub enum WebBundle { Packed(PackedBundle), } -pub fn create_bundle( +pub fn create_web_bundle( metadata: &Metadata, profile: &str, bin_target: BinTarget, @@ -50,37 +50,48 @@ pub fn create_bundle( index: Index::Static(default_index(&bin_target)), }; - if packed { - let base_path = metadata - .target_directory - .join("bevy_web") - .join(profile) - .join(bin_target.bin_name); - - // Build artifacts - fs::create_dir_all(base_path.join("build"))?; - fs::copy( - linked.wasm_path, - base_path.join("build").join(&wasm_file_name), + if !packed { + return Ok(WebBundle::Linked(linked)); + } + + let base_path = metadata + .target_directory + .join("bevy_web") + .join(profile) + .join(bin_target.bin_name); + + // Build artifacts + fs::create_dir_all(base_path.join("build"))?; + fs::copy( + linked.wasm_path, + base_path.join("build").join(&wasm_file_name), + )?; + fs::copy(linked.js_path, base_path.join("build").join(&js_file_name))?; + + // Assets + if let Some(assets_path) = linked.assets_path { + fs_extra::dir::copy( + assets_path, + base_path.join("assets"), + &fs_extra::dir::CopyOptions { + overwrite: true, + ..Default::default() + }, )?; - fs::copy(linked.js_path, base_path.join("build").join(&js_file_name))?; - - // TODO: Copy assets - - let index_path = base_path.join("index.html"); - match linked.index { - Index::File(path) => { - fs::copy(path, index_path)?; - } - Index::Static(contents) => { - fs::write(index_path, contents)?; - } - } + } - Ok(WebBundle::Packed(PackedBundle { path: base_path })) - } else { - Ok(WebBundle::Linked(linked)) + // Index + let index_path = base_path.join("index.html"); + match linked.index { + Index::File(path) => { + fs::copy(path, index_path)?; + } + Index::Static(contents) => { + fs::write(index_path, contents)?; + } } + + Ok(WebBundle::Packed(PackedBundle { path: base_path })) } /// Create the default `index.html` if the user didn't provide one.