Related to #1547: use dioxus-cli within a workspace (wildcard-members, real package names) (#1642)

* Related to #1547: use `dioxus-cli` within a workspace

Although the `dx` CLI allows to specify a package name to chose from workspace members,
it does not support workspace members specified as glob-wildcards.
Neither it respects the effective package name, specified in the crate's `Cargo.toml`.

This PR addresses that issue:
- upon `dx build ...`, if the `--bin` CLI-argument is provided, treat the current dir as a workspace;
- search through the workspace's `members`: resolve each of them with `glob`;
- assume that any workspace member has a `Cargo.toml` in it (cargo does it, so it's okay);
- read said manifest, and check the package name in it;
- if found — there we have our sought package.

* Use cargo-metadata to find out the workspace structure

* glob is unused
This commit is contained in:
Raman Hafiyatulin authored and GitHub committed 2023-11-15 15:14:16 -06:00
1 parent 62719c309a
commit 098689083d
2 files changed
+30 -38

No files matched your search

+3
View File
@@ -29,6 +29,9 @@ pub enum Error {
#[error("Cargo Error: {0}")] #[error("Cargo Error: {0}")]
CargoError(String), CargoError(String),
#[error("Couldn't retrieve cargo metadata")]
CargoMetadata(#[source] cargo_metadata::Error),
#[error("{0}")] #[error("{0}")]
CustomError(String), CustomError(String),
+27 -38
View File
@@ -9,42 +9,31 @@ use dioxus_cli::plugin::PluginManager;
use Commands::*; use Commands::*;
fn get_bin(bin: Option<String>) -> Result<Option<PathBuf>> { fn get_bin(bin: Option<String>) -> Result<PathBuf> {
const ERR_MESSAGE: &str = "The `--bin` flag has to be ran in a Cargo workspace."; let metadata = cargo_metadata::MetadataCommand::new()
.exec()
if let Some(ref bin) = bin { .map_err(Error::CargoMetadata)?;
let manifest = cargo_toml::Manifest::from_path("./Cargo.toml") let package = if let Some(bin) = bin {
.map_err(|_| Error::CargoError(ERR_MESSAGE.to_string()))?; metadata
.workspace_packages()
if let Some(workspace) = manifest.workspace { .into_iter()
for item in workspace.members.iter() { .find(|p| p.name == bin)
let path = PathBuf::from(item); .ok_or(format!("no such package: {}", bin))
.map_err(Error::CargoError)?
if !path.exists() {
continue;
}
if !path.is_dir() {
continue;
}
if path.ends_with(bin.clone()) {
return Ok(Some(path));
}
}
} else { } else {
return Err(Error::CargoError(ERR_MESSAGE.to_string())); metadata
} .root_package()
} .ok_or("no root package?".into())
.map_err(Error::CargoError)?
};
// If the bin exists but we couldn't find it let crate_dir = package
if bin.is_some() { .manifest_path
return Err(Error::CargoError( .parent()
"The specified bin does not exist.".to_string(), .ok_or("couldn't take parent dir".into())
)); .map_err(Error::CargoError)?;
}
Ok(None) Ok(crate_dir.into())
} }
#[tokio::main] #[tokio::main]
@@ -55,7 +44,7 @@ async fn main() -> anyhow::Result<()> {
let bin = get_bin(args.bin)?; let bin = get_bin(args.bin)?;
let _dioxus_config = DioxusConfig::load(bin.clone()) let _dioxus_config = DioxusConfig::load(Some(bin.clone()))
.map_err(|e| anyhow!("Failed to load Dioxus config because: {e}"))? .map_err(|e| anyhow!("Failed to load Dioxus config because: {e}"))?
.unwrap_or_else(|| { .unwrap_or_else(|| {
log::warn!("You appear to be creating a Dioxus project from scratch; we will use the default config"); log::warn!("You appear to be creating a Dioxus project from scratch; we will use the default config");
@@ -72,15 +61,15 @@ async fn main() -> anyhow::Result<()> {
.map_err(|e| anyhow!("🚫 Translation of HTML into RSX failed: {}", e)), .map_err(|e| anyhow!("🚫 Translation of HTML into RSX failed: {}", e)),
Build(opts) => opts Build(opts) => opts
.build(bin.clone()) .build(Some(bin.clone()))
.map_err(|e| anyhow!("🚫 Building project failed: {}", e)), .map_err(|e| anyhow!("🚫 Building project failed: {}", e)),
Clean(opts) => opts Clean(opts) => opts
.clean(bin.clone()) .clean(Some(bin.clone()))
.map_err(|e| anyhow!("🚫 Cleaning project failed: {}", e)), .map_err(|e| anyhow!("🚫 Cleaning project failed: {}", e)),
Serve(opts) => opts Serve(opts) => opts
.serve(bin.clone()) .serve(Some(bin.clone()))
.await .await
.map_err(|e| anyhow!("🚫 Serving project failed: {}", e)), .map_err(|e| anyhow!("🚫 Serving project failed: {}", e)),
@@ -93,7 +82,7 @@ async fn main() -> anyhow::Result<()> {
.map_err(|e| anyhow!("🚫 Configuring new project failed: {}", e)), .map_err(|e| anyhow!("🚫 Configuring new project failed: {}", e)),
Bundle(opts) => opts Bundle(opts) => opts
.bundle(bin.clone()) .bundle(Some(bin.clone()))
.map_err(|e| anyhow!("🚫 Bundling project failed: {}", e)), .map_err(|e| anyhow!("🚫 Bundling project failed: {}", e)),
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]