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

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

Merged
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
3 changes: 3 additions & 0 deletions packages/cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub enum Error {
#[error("Cargo Error: {0}")]
CargoError(String),

#[error("Couldn't retrieve cargo metadata")]
CargoMetadata(#[source] cargo_metadata::Error),

#[error("{0}")]
CustomError(String),

Expand Down
71 changes: 30 additions & 41 deletions packages/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,31 @@ use dioxus_cli::plugin::PluginManager;

use Commands::*;

fn get_bin(bin: Option<String>) -> Result<Option<PathBuf>> {
const ERR_MESSAGE: &str = "The `--bin` flag has to be ran in a Cargo workspace.";

if let Some(ref bin) = bin {
let manifest = cargo_toml::Manifest::from_path("./Cargo.toml")
.map_err(|_| Error::CargoError(ERR_MESSAGE.to_string()))?;

if let Some(workspace) = manifest.workspace {
for item in workspace.members.iter() {
let path = PathBuf::from(item);

if !path.exists() {
continue;
}

if !path.is_dir() {
continue;
}

if path.ends_with(bin.clone()) {
return Ok(Some(path));
}
}
} else {
return Err(Error::CargoError(ERR_MESSAGE.to_string()));
}
}

// If the bin exists but we couldn't find it
if bin.is_some() {
return Err(Error::CargoError(
"The specified bin does not exist.".to_string(),
));
}

Ok(None)
fn get_bin(bin: Option<String>) -> Result<PathBuf> {
let metadata = cargo_metadata::MetadataCommand::new()
.exec()
.map_err(Error::CargoMetadata)?;
let package = if let Some(bin) = bin {
metadata
.workspace_packages()
.into_iter()
.find(|p| p.name == bin)
.ok_or(format!("no such package: {}", bin))
.map_err(Error::CargoError)?
} else {
metadata
.root_package()
.ok_or("no root package?".into())
.map_err(Error::CargoError)?
};

let crate_dir = package
.manifest_path
.parent()
.ok_or("couldn't take parent dir".into())
.map_err(Error::CargoError)?;

Ok(crate_dir.into())
}

#[tokio::main]
Expand All @@ -55,7 +44,7 @@ async fn main() -> anyhow::Result<()> {

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}"))?
.unwrap_or_else(|| {
log::warn!("You appear to be creating a Dioxus project from scratch; we will use the default config");
Expand All @@ -72,15 +61,15 @@ async fn main() -> anyhow::Result<()> {
.map_err(|e| anyhow!("🚫 Translation of HTML into RSX failed: {}", e)),

Build(opts) => opts
.build(bin.clone())
.build(Some(bin.clone()))
.map_err(|e| anyhow!("🚫 Building project failed: {}", e)),

Clean(opts) => opts
.clean(bin.clone())
.clean(Some(bin.clone()))
.map_err(|e| anyhow!("🚫 Cleaning project failed: {}", e)),

Serve(opts) => opts
.serve(bin.clone())
.serve(Some(bin.clone()))
.await
.map_err(|e| anyhow!("🚫 Serving project failed: {}", e)),

Expand All @@ -93,7 +82,7 @@ async fn main() -> anyhow::Result<()> {
.map_err(|e| anyhow!("🚫 Configuring new project failed: {}", e)),

Bundle(opts) => opts
.bundle(bin.clone())
.bundle(Some(bin.clone()))
.map_err(|e| anyhow!("🚫 Bundling project failed: {}", e)),

#[cfg(feature = "plugin")]
Expand Down
Loading