Merge pull request #2057 from ealmloff/fix-examples-assets

Fix Manganis assets overlapping between examples
This commit is contained in:
Jonathan Kelley 2024-03-11 16:33:21 -07:00 committed by GitHub
commit 41809f9a1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 96 additions and 109 deletions

53
Cargo.lock generated
View file

@ -2201,7 +2201,7 @@ version = "0.5.0-alpha.0"
dependencies = [
"criterion 0.3.6",
"dioxus-config-macro",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-core-macro",
"dioxus-desktop",
"dioxus-fullstack",
@ -2268,7 +2268,7 @@ dependencies = [
"dioxus-autofmt",
"dioxus-check",
"dioxus-cli-config",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-hot-reload",
"dioxus-html",
"dioxus-rsx",
@ -2356,6 +2356,20 @@ dependencies = [
"tracing-subscriber",
]
[[package]]
name = "dioxus-core"
version = "0.5.0-alpha.0"
source = "git+https://github.com/DioxusLabs/dioxus#44d09fcd2de4af32c8a3a107cabdd3e6860baeeb"
dependencies = [
"futures-channel",
"futures-util",
"longest-increasing-subsequence",
"rustc-hash",
"slab",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "dioxus-core-macro"
version = "0.5.0-alpha.0"
@ -2386,7 +2400,7 @@ dependencies = [
"core-foundation",
"dioxus",
"dioxus-cli-config",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-hooks",
"dioxus-hot-reload",
"dioxus-html",
@ -2487,7 +2501,7 @@ name = "dioxus-hooks"
version = "0.5.0-alpha.0"
dependencies = [
"dioxus",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-debug-cell",
"dioxus-signals",
"futures-channel",
@ -2505,7 +2519,7 @@ name = "dioxus-hot-reload"
version = "0.5.0-alpha.0"
dependencies = [
"chrono",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-html",
"dioxus-rsx",
"execute",
@ -2522,7 +2536,7 @@ name = "dioxus-html"
version = "0.5.0-alpha.0"
dependencies = [
"async-trait",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-html-internal-macro",
"dioxus-rsx",
"enumset",
@ -2555,7 +2569,7 @@ dependencies = [
name = "dioxus-interpreter-js"
version = "0.5.0-alpha.0"
dependencies = [
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-html",
"js-sys",
"md5",
@ -2571,7 +2585,7 @@ name = "dioxus-lib"
version = "0.5.0-alpha.0"
dependencies = [
"dioxus-config-macro",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-core-macro",
"dioxus-hooks",
"dioxus-html",
@ -2586,7 +2600,7 @@ dependencies = [
"axum",
"dioxus",
"dioxus-cli-config",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-hot-reload",
"dioxus-html",
"dioxus-interpreter-js",
@ -2620,7 +2634,7 @@ dependencies = [
"anymap 1.0.0-beta.2",
"dashmap",
"dioxus",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-native-core",
"dioxus-native-core-macro",
"keyboard-types",
@ -2729,7 +2743,7 @@ dependencies = [
name = "dioxus-rsx"
version = "0.5.0-alpha.0"
dependencies = [
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"internment",
"krates",
"proc-macro2",
@ -2744,7 +2758,7 @@ name = "dioxus-signals"
version = "0.5.0-alpha.0"
dependencies = [
"dioxus",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"futures-channel",
"futures-util",
"generational-box",
@ -2768,7 +2782,7 @@ dependencies = [
"async-trait",
"chrono",
"dioxus",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-html",
"dioxus-signals",
"fern",
@ -2799,7 +2813,7 @@ dependencies = [
"criterion 0.3.6",
"crossterm 0.26.1",
"dioxus",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-hot-reload",
"dioxus-html",
"dioxus-native-core",
@ -2817,7 +2831,7 @@ dependencies = [
"async-trait",
"console_error_panic_hook",
"dioxus",
"dioxus-core",
"dioxus-core 0.5.0-alpha.0",
"dioxus-html",
"dioxus-interpreter-js",
"dioxus-ssr",
@ -5701,17 +5715,14 @@ dependencies = [
[[package]]
name = "manganis"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d11b87b5222a0eb4c7b1e7a49cc78922aa82a7fa5aa614e07ace55ed74fcdf67"
dependencies = [
"dioxus-core 0.5.0-alpha.0 (git+https://github.com/DioxusLabs/dioxus)",
"manganis-macro",
]
[[package]]
name = "manganis-cli-support"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d9c40a20048742dfbb793fb93498640e128543bcf92588b4c3b25451957522d"
dependencies = [
"anyhow",
"cargo-lock 9.0.0",
@ -5737,8 +5748,6 @@ dependencies = [
[[package]]
name = "manganis-common"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84166560d60366c4b4fe12e204f2fca78fe594382e13c02d75fab6e0d8f40184"
dependencies = [
"anyhow",
"base64",
@ -5753,8 +5762,6 @@ dependencies = [
[[package]]
name = "manganis-macro"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b78150e016312c51841521a9dc7fb5ae994c163a325eb4c2aa0186efce9532e"
dependencies = [
"manganis-common",
"proc-macro2",

View file

@ -100,11 +100,11 @@ thiserror = "1.0.40"
prettyplease = { package = "prettier-please", version = "0.2", features = [
"verbatim",
] }
manganis-cli-support = { version = "0.1.0", features = [
manganis-cli-support = { version = "0.2.0", features = [
"webp",
"html",
] }
manganis = { version = "0.1.0" }
manganis = { version = "0.2.0" }
lru = "0.12.2"
async-trait = "0.1.77"

View file

@ -370,6 +370,16 @@ pub enum ExecutableType {
Example(String),
}
impl ExecutableType {
/// Get the name of the executable if it is a binary or an example.
pub fn executable(&self) -> Option<&str> {
match self {
Self::Binary(bin) | Self::Example(bin) => Some(bin),
_ => None,
}
}
}
impl CrateConfig {
#[cfg(feature = "cli")]
pub fn new(bin: Option<PathBuf>) -> Result<Self, CrateConfigError> {

View file

@ -4,8 +4,9 @@ use crate::Result;
use dioxus_cli_config::CrateConfig;
use manganis_cli_support::{AssetManifest, AssetManifestExt};
pub fn asset_manifest(crate_config: &CrateConfig) -> AssetManifest {
pub fn asset_manifest(bin: Option<&str>, crate_config: &CrateConfig) -> AssetManifest {
AssetManifest::load_from_path(
bin,
crate_config.crate_dir.join("Cargo.toml"),
crate_config.workspace_dir.join("Cargo.lock"),
)

View file

@ -27,6 +27,7 @@ lazy_static! {
#[derive(Debug, Clone)]
pub struct BuildResult {
pub warnings: Vec<Diagnostic>,
pub executable: Option<PathBuf>,
pub elapsed_time: u128,
pub assets: Option<AssetManifest>,
}
@ -117,7 +118,7 @@ pub fn build(
.arg("build")
.arg("--target")
.arg("wasm32-unknown-unknown")
.arg("--message-format=json");
.arg("--message-format=json-render-diagnostics");
// TODO: make the initial variable mutable to simplify all the expressions
// below. Look inside the `build_desktop()` as an example.
@ -159,28 +160,11 @@ pub fn build(
// [2] Establish the output directory structure
let bindgen_outdir = out_dir.join("assets").join("dioxus");
let build_target = if config.custom_profile.is_some() {
let build_profile = config.custom_profile.as_ref().unwrap();
if build_profile == "dev" {
"debug"
} else {
build_profile
}
} else if config.release {
"release"
} else {
"debug"
};
let input_path = match executable {
ExecutableType::Binary(name) | ExecutableType::Lib(name) => target_dir
.join(format!("wasm32-unknown-unknown/{}", build_target))
.join(format!("{}.wasm", name)),
ExecutableType::Example(name) => target_dir
.join(format!("wasm32-unknown-unknown/{}/examples", build_target))
.join(format!("{}.wasm", name)),
};
let input_path = warning_messages
.output_location
.as_ref()
.unwrap()
.with_extension("wasm");
let bindgen_result = panic::catch_unwind(move || {
// [3] Bindgen the final binary for use easy linking
@ -288,8 +272,8 @@ pub fn build(
depth: 0,
};
if asset_dir.is_dir() {
for entry in std::fs::read_dir(asset_dir)? {
let path = entry?.path();
for entry in std::fs::read_dir(config.asset_dir())?.flatten() {
let path = entry.path();
if path.is_file() {
std::fs::copy(&path, out_dir.join(path.file_name().unwrap()))?;
} else {
@ -311,7 +295,7 @@ pub fn build(
}
let assets = if !skip_assets {
let assets = asset_manifest(config);
let assets = asset_manifest(executable.executable(), config);
process_assets(config, &assets)?;
Some(assets)
} else {
@ -319,7 +303,8 @@ pub fn build(
};
Ok(BuildResult {
warnings: warning_messages,
warnings: warning_messages.warnings,
executable: warning_messages.output_location,
elapsed_time: t_start.elapsed().as_millis(),
assets,
})
@ -346,7 +331,7 @@ pub fn build_desktop(
.env("CARGO_TARGET_DIR", &config.target_dir)
.cwd(&config.crate_dir)
.arg("build")
.arg("--message-format=json");
.arg("--message-format=json-render-diagnostics");
if config.release {
cmd = cmd.arg("--release");
@ -371,8 +356,6 @@ pub fn build_desktop(
cmd = cmd.arg("--target").arg(target);
}
let target_platform = config.target.as_deref().unwrap_or("");
cmd = cmd.args(&config.cargo_args);
let cmd = match &config.executable {
@ -383,34 +366,9 @@ pub fn build_desktop(
let warning_messages = prettier_build(cmd)?;
let release_type = match config.release {
true => "release",
false => "debug",
};
let file_name: String;
let mut res_path = match &config.executable {
ExecutableType::Binary(name) | ExecutableType::Lib(name) => {
file_name = name.clone();
config
.target_dir
.join(target_platform)
.join(release_type)
.join(name)
}
ExecutableType::Example(name) => {
file_name = name.clone();
config
.target_dir
.join(target_platform)
.join(release_type)
.join("examples")
.join(name)
}
};
let file_name: String = config.executable.executable().unwrap().to_string();
let target_file = if cfg!(windows) {
res_path.set_extension("exe");
format!("{}.exe", &file_name)
} else {
file_name
@ -419,7 +377,10 @@ pub fn build_desktop(
if !config.out_dir().is_dir() {
create_dir_all(config.out_dir())?;
}
copy(res_path, config.out_dir().join(target_file))?;
let output_path = config.out_dir().join(target_file);
if let Some(res_path) = &warning_messages.output_location {
copy(res_path, &output_path)?;
}
// this code will copy all public file to the output dir
if config.asset_dir().is_dir() {
@ -432,8 +393,8 @@ pub fn build_desktop(
depth: 0,
};
for entry in std::fs::read_dir(config.asset_dir())? {
let path = entry?.path();
for entry in std::fs::read_dir(config.asset_dir())?.flatten() {
let path = entry.path();
if path.is_file() {
std::fs::copy(&path, &config.out_dir().join(path.file_name().unwrap()))?;
} else {
@ -455,7 +416,7 @@ pub fn build_desktop(
}
let assets = if !skip_assets {
let assets = asset_manifest(config);
let assets = asset_manifest(config.executable.executable(), config);
// Collect assets
process_assets(config, &assets)?;
// Create the __assets_head.html file for bundling
@ -473,13 +434,19 @@ pub fn build_desktop(
println!("build desktop done");
Ok(BuildResult {
warnings: warning_messages,
warnings: warning_messages.warnings,
executable: Some(output_path),
elapsed_time: t_start.elapsed().as_millis(),
assets,
})
}
fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result<Vec<Diagnostic>> {
struct CargoBuildResult {
warnings: Vec<Diagnostic>,
output_location: Option<PathBuf>,
}
fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result<CargoBuildResult> {
let mut warning_messages: Vec<Diagnostic> = vec![];
let mut pb = ProgressBar::new_spinner();
@ -494,6 +461,7 @@ fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result<Vec<Diagnostic>> {
let stdout = cmd.detached().stream_stdout()?;
let reader = std::io::BufReader::new(stdout);
let mut output_location = None;
for message in cargo_metadata::Message::parse_stream(reader) {
match message.unwrap() {
@ -516,6 +484,9 @@ fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result<Vec<Diagnostic>> {
Message::CompilerArtifact(artifact) => {
pb.set_message(format!("⚙️ Compiling {} ", artifact.package_id));
pb.tick();
if let Some(executable) = artifact.executable {
output_location = Some(executable.into());
}
}
Message::BuildScriptExecuted(script) => {
let _package_id = script.package_id.to_string();
@ -532,7 +503,11 @@ fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result<Vec<Diagnostic>> {
}
}
}
Ok(warning_messages)
Ok(CargoBuildResult {
warnings: warning_messages,
output_location,
})
}
pub fn gen_page(config: &CrateConfig, manifest: Option<&AssetManifest>, serve: bool) -> String {

View file

@ -8,7 +8,6 @@ use crate::{
BuildResult, Result,
};
use dioxus_cli_config::CrateConfig;
use dioxus_cli_config::ExecutableType;
use dioxus_hot_reload::HotReloadMsg;
use dioxus_html::HtmlCtx;
@ -224,24 +223,19 @@ fn start_desktop(
// Only used for the fullstack platform,
let result = crate::builder::build_desktop(config, true, skip_assets, rust_flags)?;
match &config.executable {
ExecutableType::Binary(name)
| ExecutableType::Lib(name)
| ExecutableType::Example(name) => {
let mut file = config.out_dir().join(name);
if cfg!(windows) {
file.set_extension("exe");
}
let active = "DIOXUS_ACTIVE";
let child = RAIIChild(
Command::new(file.to_str().unwrap())
.env(active, "true")
.spawn()?,
);
let active = "DIOXUS_ACTIVE";
let child = RAIIChild(
Command::new(
result
.executable
.clone()
.ok_or(anyhow::anyhow!("No executable found after desktop build"))?,
)
.env(active, "true")
.spawn()?,
);
Ok((child, result))
}
}
Ok((child, result))
}
pub(crate) struct DesktopPlatform {

View file

@ -137,7 +137,7 @@ async fn setup_file_watcher<F: Fn() -> Result<BuildResult> + Send + 'static>(
&config.crate_dir.join(sub_path),
notify::RecursiveMode::Recursive,
) {
log::error!("Failed to watch path: {}", err);
log::warn!("Failed to watch path: {}", err);
}
}
Ok(watcher)

View file

@ -204,7 +204,7 @@ fn module_loader(root_id: &str, headless: bool) -> String {
/// Defaults to the current directory if no asset directory is found, which is useful for development when the app
/// isn't bundled.
fn get_asset_root_or_default() -> PathBuf {
get_asset_root().unwrap_or_else(|| Path::new(".").to_path_buf())
get_asset_root().unwrap_or_else(|| std::env::current_dir().unwrap())
}
/// Get the asset directory, following tauri/cargo-bundles directory discovery approach