revision: find bin from workspace Cargo.toml

This commit is contained in:
Miles Murgaw committed 2023-07-17 14:30:05 -04:00
1 parent 9fab3cf4e3
commit 4b70b1ce96
2 files changed
+47 -5

No files matched your search

+1 -1
View File
@@ -39,7 +39,7 @@ pub struct Cli {
/// Specify bin target /// Specify bin target
#[clap(global = true, long)] #[clap(global = true, long)]
pub bin: Option<PathBuf>, pub bin: Option<String>,
} }
#[derive(Parser)] #[derive(Parser)]
+46 -4
View File
@@ -1,3 +1,5 @@
use std::path::PathBuf;
use anyhow::anyhow; use anyhow::anyhow;
use clap::Parser; use clap::Parser;
use dioxus_cli::*; use dioxus_cli::*;
@@ -7,13 +9,53 @@ use dioxus_cli::plugin::PluginManager;
use Commands::*; 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)
}
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
let args = Cli::parse(); let args = Cli::parse();
set_up_logging(); set_up_logging();
let _dioxus_config = DioxusConfig::load(args.bin.clone()) let bin = get_bin(args.bin)?;
let _dioxus_config = DioxusConfig::load(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");
@@ -30,15 +72,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(args.bin.clone()) .build(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(args.bin.clone()) .clean(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(args.bin.clone()) .serve(bin.clone())
.await .await
.map_err(|e| anyhow!("🚫 Serving project failed: {}", e)), .map_err(|e| anyhow!("🚫 Serving project failed: {}", e)),