Browse Source

fix: do it correctly

Miles Murgaw 1 năm trước cách đây
mục cha
commit
d710b92696

+ 2 - 2
packages/cli/src/cli/build.rs

@@ -12,8 +12,8 @@ pub struct Build {
 }
 
 impl Build {
-    pub fn build(self) -> Result<()> {
-        let mut crate_config = crate::CrateConfig::new(self.build.bin)?;
+    pub fn build(self, bin: Option<PathBuf>) -> Result<()> {
+        let mut crate_config = crate::CrateConfig::new(bin)?;
 
         // change the release state.
         crate_config.with_release(self.build.release);

+ 0 - 8
packages/cli/src/cli/cfg.rs

@@ -32,10 +32,6 @@ pub struct ConfigOptsBuild {
     /// Space separated list of features to activate
     #[clap(long)]
     pub features: Option<Vec<String>>,
-
-    /// The binary to serve
-    #[clap(long)]
-    pub bin: Option<PathBuf>,
 }
 
 #[derive(Clone, Debug, Default, Deserialize, Parser)]
@@ -90,10 +86,6 @@ pub struct ConfigOptsServe {
     /// Space separated list of features to activate
     #[clap(long)]
     pub features: Option<Vec<String>>,
-
-    /// The binary to serve
-    #[clap(long)]
-    pub bin: Option<PathBuf>,
 }
 
 /// Ensure the given value for `--public-url` is formatted correctly.

+ 2 - 2
packages/cli/src/cli/clean.rs

@@ -6,8 +6,8 @@ use super::*;
 pub struct Clean {}
 
 impl Clean {
-    pub fn clean(self) -> Result<()> {
-        let crate_config = crate::CrateConfig::new(None)?;
+    pub fn clean(self, bin: Option<PathBuf>) -> Result<()> {
+        let crate_config = crate::CrateConfig::new(bin)?;
 
         let output = Command::new("cargo")
             .arg("clean")

+ 4 - 0
packages/cli/src/cli/mod.rs

@@ -36,6 +36,10 @@ pub struct Cli {
     /// Enable verbose logging.
     #[clap(short)]
     pub v: bool,
+
+    /// Specify bin target
+    #[clap(global = true, long)]
+    pub bin: Option<PathBuf>,
 }
 
 #[derive(Parser)]

+ 2 - 2
packages/cli/src/cli/serve.rs

@@ -15,8 +15,8 @@ pub struct Serve {
 }
 
 impl Serve {
-    pub async fn serve(self) -> Result<()> {
-        let mut crate_config = crate::CrateConfig::new(self.serve.bin)?;
+    pub async fn serve(self, bin: Option<PathBuf>) -> Result<()> {
+        let mut crate_config = crate::CrateConfig::new(bin)?;
 
         // change the relase state.
         crate_config.with_hot_reload(self.serve.hot_reload);

+ 14 - 3
packages/cli/src/config.rs

@@ -17,8 +17,19 @@ fn default_plugin() -> toml::Value {
 }
 
 impl DioxusConfig {
-    pub fn load() -> crate::error::Result<Option<DioxusConfig>> {
-        let Ok(crate_dir) = crate::cargo::crate_root() else { return Ok(None); };
+    pub fn load(bin: Option<PathBuf>) -> crate::error::Result<Option<DioxusConfig>> {
+        let crate_dir = crate::cargo::crate_root();
+
+        let crate_dir = match crate_dir {
+            Ok(dir) => {
+                if let Some(bin) = bin {
+                    dir.join(bin)
+                } else {
+                    dir
+                }
+            }
+            Err(_) => return Ok(None),
+        };
 
         // we support either `Dioxus.toml` or `Cargo.toml`
         let Some(dioxus_conf_file) = acquire_dioxus_toml(crate_dir) else {
@@ -177,7 +188,7 @@ pub enum ExecutableType {
 
 impl CrateConfig {
     pub fn new(bin: Option<PathBuf>) -> Result<Self> {
-        let dioxus_config = DioxusConfig::load()?.unwrap_or_default();
+        let dioxus_config = DioxusConfig::load(bin.clone())?.unwrap_or_default();
 
         let crate_root = crate::cargo::crate_root()?;
 

+ 4 - 4
packages/cli/src/main.rs

@@ -13,7 +13,7 @@ async fn main() -> anyhow::Result<()> {
 
     set_up_logging();
 
-    let _dioxus_config = DioxusConfig::load()
+    let _dioxus_config = DioxusConfig::load(args.bin.clone())
         .map_err(|e| anyhow!("Failed to load `Dioxus.toml` because: {e}"))?
         .unwrap_or_else(|| {
             log::warn!("You appear to be creating a Dioxus project from scratch; we will use the default config");
@@ -30,15 +30,15 @@ async fn main() -> anyhow::Result<()> {
             .map_err(|e| anyhow!("🚫 Translation of HTML into RSX failed: {}", e)),
 
         Build(opts) => opts
-            .build()
+            .build(args.bin.clone())
             .map_err(|e| anyhow!("🚫 Building project failed: {}", e)),
 
         Clean(opts) => opts
-            .clean()
+            .clean(args.bin.clone())
             .map_err(|e| anyhow!("🚫 Cleaning project failed: {}", e)),
 
         Serve(opts) => opts
-            .serve()
+            .serve(args.bin.clone())
             .await
             .map_err(|e| anyhow!("🚫 Serving project failed: {}", e)),