From 5109a4b745843a6bdaf2a8fe553d5dafdb46bc88 Mon Sep 17 00:00:00 2001 From: Brian May Date: Wed, 16 Apr 2025 07:51:04 +1000 Subject: [PATCH] feat: add cli support to build assets from executable (#3429) * feat: add cli support to build assets from executable See https://github.com/DioxusLabs/dioxus/discussions/3402#discussioncomment-11632372 * fix: store relative paths in executable * bring back deprecation warning --------- Co-authored-by: Jonathan Kelley --- packages/cli/src/cli/build_assets.rs | 40 ++++++++++++++++++++ packages/cli/src/cli/mod.rs | 6 +++ packages/cli/src/main.rs | 1 + packages/manganis/manganis-core/src/asset.rs | 2 +- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 packages/cli/src/cli/build_assets.rs diff --git a/packages/cli/src/cli/build_assets.rs b/packages/cli/src/cli/build_assets.rs new file mode 100644 index 000000000..b81c905ae --- /dev/null +++ b/packages/cli/src/cli/build_assets.rs @@ -0,0 +1,40 @@ +use std::{fs::create_dir_all, path::PathBuf}; + +use crate::{Result, StructuredOutput}; +use clap::Parser; +use dioxus_cli_opt::{process_file_to, AssetManifest}; +use tracing::debug; + +#[derive(Clone, Debug, Parser)] +pub struct BuildAssets { + /// The source executable to build assets for. + pub(crate) executable: PathBuf, + + /// The source directory for the assets. + pub(crate) source: PathBuf, + + /// The destination directory for the assets. + pub(crate) destination: PathBuf, +} + +impl BuildAssets { + pub async fn run(self) -> Result { + let mut manifest = AssetManifest::default(); + manifest.add_from_object_path(&self.executable)?; + + create_dir_all(&self.destination)?; + for (path, asset) in manifest.assets.iter() { + let source_path = self.source.join(path); + let destination_path = self.destination.join(asset.bundled_path()); + debug!( + "Processing asset {} --> {} {:#?}", + source_path.display(), + destination_path.display(), + asset + ); + process_file_to(asset.options(), &source_path, &destination_path)?; + } + + Ok(StructuredOutput::Success) + } +} diff --git a/packages/cli/src/cli/mod.rs b/packages/cli/src/cli/mod.rs index 02d362b71..34b88ce2c 100644 --- a/packages/cli/src/cli/mod.rs +++ b/packages/cli/src/cli/mod.rs @@ -1,5 +1,6 @@ pub(crate) mod autoformat; pub(crate) mod build; +pub(crate) mod build_assets; pub(crate) mod bundle; pub(crate) mod check; pub(crate) mod clean; @@ -90,6 +91,10 @@ pub(crate) enum Commands { #[clap(subcommand)] #[clap(name = "config")] Config(config::Config), + + /// Build the assets for a specific target. + #[clap(name = "build_assets")] + BuildAssets(build_assets::BuildAssets), } impl Display for Commands { @@ -106,6 +111,7 @@ impl Display for Commands { Commands::Check(_) => write!(f, "check"), Commands::Bundle(_) => write!(f, "bundle"), Commands::Run(_) => write!(f, "run"), + Commands::BuildAssets(_) => write!(f, "build_assets"), } } } diff --git a/packages/cli/src/main.rs b/packages/cli/src/main.rs index 1a0863feb..e8104c2fb 100644 --- a/packages/cli/src/main.rs +++ b/packages/cli/src/main.rs @@ -57,6 +57,7 @@ async fn main() { Commands::Serve(opts) => opts.serve().await, Commands::Bundle(opts) => opts.bundle().await, Commands::Run(opts) => opts.run().await, + Commands::BuildAssets(opts) => opts.run().await, }; // Provide a structured output for third party tools that can consume the output of the CLI diff --git a/packages/manganis/manganis-core/src/asset.rs b/packages/manganis/manganis-core/src/asset.rs index 28434b538..807c95a74 100644 --- a/packages/manganis/manganis-core/src/asset.rs +++ b/packages/manganis/manganis-core/src/asset.rs @@ -48,7 +48,7 @@ impl BundledAsset { /// Create a new asset but with a relative path /// /// This method is deprecated and will be removed in a future release. - #[deprecated( + #[deprecated( note = "Relative asset!() paths are not supported. Use a path like `/assets/myfile.png` instead of `./assets/myfile.png`" )] pub const fn new_relative(