Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cli support to build assets from executable #3429

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/cli/src/cli/build_assets.rs
Original file line number Diff line number Diff line change
@@ -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<StructuredOutput> {
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)
}
}
6 changes: 6 additions & 0 deletions packages/cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -95,6 +96,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 {
Expand All @@ -112,6 +117,7 @@ impl Display for Commands {
Commands::Bundle(_) => write!(f, "bundle"),
Commands::Run(_) => write!(f, "run"),
Commands::Doctor(_) => write!(f, "doctor"),
Commands::BuildAssets(_) => write!(f, "build_assets"),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async fn main() {
Commands::Bundle(opts) => opts.bundle().await,
Commands::Run(opts) => opts.run().await,
Commands::Doctor(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
Expand Down
3 changes: 0 additions & 3 deletions packages/manganis/manganis-core/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ impl BundledAsset {
/// Create a new asset but with a relative path
///
/// This method is deprecated and will be removed in a future release.
#[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(
absolute_source_path: &'static str,
bundled_path: &'static str,
Expand Down
10 changes: 3 additions & 7 deletions packages/manganis/manganis-macro/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@ fn resolve_path(raw: &str) -> Result<PathBuf, AssetParseError> {
});
};

// 4. Ensure the path doesn't escape the crate dir
//
// - Note: since we called canonicalize on both paths, we can safely compare the parent dirs.
// On windows, we can only compare the prefix if both paths are canonicalized (not just absolute)
// https://github.com/rust-lang/rust/issues/42869
if path == manifest_dir || !path.starts_with(manifest_dir) {
// 4. Strip the manifest dir from the path
let Ok(path) = path.strip_prefix(&manifest_dir).map(|x| x.to_path_buf()) else {
return Err(AssetParseError::InvalidPath { path });
}
};

Ok(path)
}
Expand Down