From 60eb453aada6b65c4857b28e48cd18afe8cedab8 Mon Sep 17 00:00:00 2001 From: Ell Date: Mon, 24 Apr 2023 15:40:00 +0500 Subject: [PATCH 1/4] Made clone_repo error clear. --- src/cli/translate/mod.rs | 4 +--- src/tools.rs | 10 ++++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/cli/translate/mod.rs b/src/cli/translate/mod.rs index 20ca724fd..3056a6c77 100644 --- a/src/cli/translate/mod.rs +++ b/src/cli/translate/mod.rs @@ -1,8 +1,6 @@ use std::process::exit; -use dioxus_rsx::{BodyNode, CallBody, Component}; -use proc_macro2::{Ident, Span}; -use syn::punctuated::Punctuated; +use dioxus_rsx::{BodyNode, CallBody}; use super::*; diff --git a/src/tools.rs b/src/tools.rs index 704104de6..37ecc4e38 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -1,6 +1,6 @@ use std::{ fs::{create_dir_all, File}, - io::{Read, Write}, + io::{Read, Write, ErrorKind}, path::{Path, PathBuf}, process::Command, }; @@ -46,7 +46,13 @@ pub fn clone_repo(dir: &Path, url: &str) -> anyhow::Result<()> { let mut cmd = Command::new("git"); let cmd = cmd.current_dir(target_dir); - let _res = cmd.arg("clone").arg(url).arg(dir_name).output()?; + let res = cmd.arg("clone").arg(url).arg(dir_name).output(); + if let Err(err) = res { + if ErrorKind::NotFound == err.kind() { + log::error!("Git program not found. Hint: Install git or check $PATH."); + return Err(err.into()); + } + } Ok(()) } From 9f238fa464189483651a741c2331375d325d44cd Mon Sep 17 00:00:00 2001 From: Ell Date: Mon, 24 Apr 2023 20:34:17 +0500 Subject: [PATCH 2/4] Minor improvments. --- src/cli/plugin/mod.rs | 10 ++++++---- src/plugin/mod.rs | 10 ++++++---- src/tools.rs | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/cli/plugin/mod.rs b/src/cli/plugin/mod.rs index 9179f5bd6..ca2937306 100644 --- a/src/cli/plugin/mod.rs +++ b/src/cli/plugin/mod.rs @@ -21,10 +21,12 @@ impl Plugin { } } Plugin::AppPath {} => { - if let Some(v) = crate::plugin::PluginManager::init_plugin_dir().to_str() { - println!("{}", v); - } else { - log::error!("Plugin path get failed."); + if let Ok(plugin_dir) = crate::plugin::PluginManager::init_plugin_dir() { + if let Some(v) = plugin_dir.to_str() { + println!("{}", v); + } else { + log::error!("Plugin path get failed."); + } } } Plugin::Add { name: _ } => { diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index c75e96444..5648b395f 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -42,7 +42,7 @@ impl PluginManager { let manager = lua.create_table().unwrap(); let name_index = lua.create_table().unwrap(); - let plugin_dir = Self::init_plugin_dir(); + let plugin_dir = Self::init_plugin_dir().unwrap(); let api = lua.create_table().unwrap(); @@ -288,15 +288,17 @@ impl PluginManager { Ok(()) } - pub fn init_plugin_dir() -> PathBuf { + pub fn init_plugin_dir() -> anyhow::Result { let app_path = app_path(); let plugin_path = app_path.join("plugins"); if !plugin_path.is_dir() { log::info!("📖 Start to init plugin library ..."); let url = "https://github.com/DioxusLabs/cli-plugin-library"; - clone_repo(&plugin_path, url).unwrap(); + if let Err(err) = clone_repo(&plugin_path, url) { + log::error!("Failed to init plugin dir, error caused by {}. ", err); + } } - plugin_path + Ok(plugin_path) } pub fn plugin_list() -> Vec { diff --git a/src/tools.rs b/src/tools.rs index 37ecc4e38..f11befaf5 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -49,7 +49,7 @@ pub fn clone_repo(dir: &Path, url: &str) -> anyhow::Result<()> { let res = cmd.arg("clone").arg(url).arg(dir_name).output(); if let Err(err) = res { if ErrorKind::NotFound == err.kind() { - log::error!("Git program not found. Hint: Install git or check $PATH."); + log::warn!("Git program not found. Hint: Install git or check $PATH."); return Err(err.into()); } } From 719015378b1f9d7771daf90b15edfa378934582f Mon Sep 17 00:00:00 2001 From: YuKun Liu Date: Tue, 23 May 2023 13:45:45 -0700 Subject: [PATCH 3/4] fix: un-wrap `Result` for `init_plugin_dir` --- src/plugin/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index 5648b395f..8c8233fbb 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -42,7 +42,7 @@ impl PluginManager { let manager = lua.create_table().unwrap(); let name_index = lua.create_table().unwrap(); - let plugin_dir = Self::init_plugin_dir().unwrap(); + let plugin_dir = Self::init_plugin_dir(); let api = lua.create_table().unwrap(); @@ -288,7 +288,7 @@ impl PluginManager { Ok(()) } - pub fn init_plugin_dir() -> anyhow::Result { + pub fn init_plugin_dir() -> PathBuf { let app_path = app_path(); let plugin_path = app_path.join("plugins"); if !plugin_path.is_dir() { @@ -298,7 +298,7 @@ impl PluginManager { log::error!("Failed to init plugin dir, error caused by {}. ", err); } } - Ok(plugin_path) + plugin_path } pub fn plugin_list() -> Vec { From aa1a45a14edaf0051cef979863c49d4ba06eab92 Mon Sep 17 00:00:00 2001 From: YuKun Liu Date: Tue, 23 May 2023 13:47:18 -0700 Subject: [PATCH 4/4] fix: format code --- src/builder.rs | 22 ++++++++-------------- src/cargo.rs | 4 +++- src/cli/cfg.rs | 2 +- src/cli/plugin/mod.rs | 11 +++++------ src/cli/serve/mod.rs | 2 +- src/config.rs | 2 +- src/server/mod.rs | 37 ++++++++++++++++++------------------- src/tools.rs | 2 +- 8 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 716903fad..d81408740 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -49,7 +49,8 @@ pub fn build(config: &CrateConfig, quiet: bool) -> Result { // [1] Build the .wasm module log::info!("🚅 Running build command..."); let cmd = subprocess::Exec::cmd("cargo"); - let cmd = cmd.cwd(&crate_dir) + let cmd = cmd + .cwd(&crate_dir) .arg("build") .arg("--target") .arg("wasm32-unknown-unknown") @@ -66,25 +67,18 @@ pub fn build(config: &CrateConfig, quiet: bool) -> Result { cmd }; - let cmd = if quiet { - cmd.arg("--quiet") - } else { - cmd - }; + let cmd = if quiet { cmd.arg("--quiet") } else { cmd }; let cmd = if config.custom_profile.is_some() { let custom_profile = config.custom_profile.as_ref().unwrap(); - cmd - .arg("--profile") - .arg(custom_profile) + cmd.arg("--profile").arg(custom_profile) } else { cmd }; let cmd = if config.features.is_some() { let features_str = config.features.as_ref().unwrap().join(" "); - cmd.arg("--features") - .arg(features_str) + cmd.arg("--features").arg(features_str) } else { cmd }; @@ -394,9 +388,9 @@ fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result> { let message = msg.message; match message.level { cargo_metadata::diagnostic::DiagnosticLevel::Error => { - return Err(anyhow::anyhow!( - message.rendered.unwrap_or("Unknown".into()) - )); + return Err(anyhow::anyhow!(message + .rendered + .unwrap_or("Unknown".into()))); } cargo_metadata::diagnostic::DiagnosticLevel::Warning => { warning_messages.push(message.clone()); diff --git a/src/cargo.rs b/src/cargo.rs index 572451ecc..d8501e8de 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -34,7 +34,9 @@ pub fn crate_root() -> Result { } None }) - .ok_or_else(|| Error::CargoError("Failed to find directory containing Cargo.toml".to_string())) + .ok_or_else(|| { + Error::CargoError("Failed to find directory containing Cargo.toml".to_string()) + }) } /// Checks if the directory contains `Cargo.toml` diff --git a/src/cli/cfg.rs b/src/cli/cfg.rs index cf78e0678..20ed7c40e 100644 --- a/src/cli/cfg.rs +++ b/src/cli/cfg.rs @@ -78,7 +78,7 @@ pub struct ConfigOptsServe { pub hot_reload: bool, /// Set cross-origin-policy to same-origin [default: false] - #[clap(name="cross-origin-policy")] + #[clap(name = "cross-origin-policy")] #[clap(long)] #[serde(default)] pub cross_origin_policy: bool, diff --git a/src/cli/plugin/mod.rs b/src/cli/plugin/mod.rs index ca2937306..d838a84a5 100644 --- a/src/cli/plugin/mod.rs +++ b/src/cli/plugin/mod.rs @@ -21,12 +21,11 @@ impl Plugin { } } Plugin::AppPath {} => { - if let Ok(plugin_dir) = crate::plugin::PluginManager::init_plugin_dir() { - if let Some(v) = plugin_dir.to_str() { - println!("{}", v); - } else { - log::error!("Plugin path get failed."); - } + let plugin_dir = crate::plugin::PluginManager::init_plugin_dir(); + if let Some(v) = plugin_dir.to_str() { + println!("{}", v); + } else { + log::error!("Plugin path get failed."); } } Plugin::Add { name: _ } => { diff --git a/src/cli/serve/mod.rs b/src/cli/serve/mod.rs index 48113c1c1..33a3ee576 100644 --- a/src/cli/serve/mod.rs +++ b/src/cli/serve/mod.rs @@ -31,7 +31,7 @@ impl Serve { if self.serve.profile.is_some() { crate_config.set_profile(self.serve.profile.unwrap()); } - + if self.serve.features.is_some() { crate_config.set_features(self.serve.features.unwrap()); } diff --git a/src/config.rs b/src/config.rs index 9dc95f739..e4dd7c416 100644 --- a/src/config.rs +++ b/src/config.rs @@ -236,7 +236,7 @@ impl CrateConfig { self } - pub fn with_cross_origin_policy(&mut self, cross_origin_policy: bool) -> &mut Self { + pub fn with_cross_origin_policy(&mut self, cross_origin_policy: bool) -> &mut Self { self.cross_origin_policy = cross_origin_policy; self } diff --git a/src/server/mod.rs b/src/server/mod.rs index e422032ab..59bab0b64 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -136,7 +136,12 @@ pub async fn hot_reload_handler( } #[allow(unused_assignments)] -pub async fn startup_hot_reload(ip: String, port: u16, config: CrateConfig, start_browser: bool) -> Result<()> { +pub async fn startup_hot_reload( + ip: String, + port: u16, + config: CrateConfig, + start_browser: bool, +) -> Result<()> { let first_build_result = crate::builder::build(&config, false)?; log::info!("🚀 Starting development server..."); @@ -272,11 +277,11 @@ pub async fn startup_hot_reload(ip: String, port: u16, config: CrateConfig, star ); let cors = CorsLayer::new() - // allow `GET` and `POST` when accessing the resource - .allow_methods([Method::GET, Method::POST]) - // allow requests from any origin - .allow_origin(Any) - .allow_headers(Any); + // allow `GET` and `POST` when accessing the resource + .allow_methods([Method::GET, Method::POST]) + // allow requests from any origin + .allow_origin(Any) + .allow_headers(Any); let (coep, coop) = if config.cross_origin_policy { ( @@ -296,10 +301,7 @@ pub async fn startup_hot_reload(ip: String, port: u16, config: CrateConfig, star HeaderName::from_static("cross-origin-embedder-policy"), coep, ) - .override_response_header( - HeaderName::from_static("cross-origin-opener-policy"), - coop, - ) + .override_response_header(HeaderName::from_static("cross-origin-opener-policy"), coop) .and_then( move |response: Response| async move { let response = if file_service_config @@ -456,11 +458,11 @@ pub async fn startup_default( PluginManager::on_serve_start(&config)?; let cors = CorsLayer::new() - // allow `GET` and `POST` when accessing the resource - .allow_methods([Method::GET, Method::POST]) - // allow requests from any origin - .allow_origin(Any) - .allow_headers(Any); + // allow `GET` and `POST` when accessing the resource + .allow_methods([Method::GET, Method::POST]) + // allow requests from any origin + .allow_origin(Any) + .allow_headers(Any); let (coep, coop) = if config.cross_origin_policy { ( @@ -480,10 +482,7 @@ pub async fn startup_default( HeaderName::from_static("cross-origin-embedder-policy"), coep, ) - .override_response_header( - HeaderName::from_static("cross-origin-opener-policy"), - coop, - ) + .override_response_header(HeaderName::from_static("cross-origin-opener-policy"), coop) .and_then( move |response: Response| async move { let response = if file_service_config diff --git a/src/tools.rs b/src/tools.rs index f11befaf5..75ed226ce 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -1,6 +1,6 @@ use std::{ fs::{create_dir_all, File}, - io::{Read, Write, ErrorKind}, + io::{ErrorKind, Read, Write}, path::{Path, PathBuf}, process::Command, };