diff --git a/Cargo.lock b/Cargo.lock index ebba9c5a9..008ea6022 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,6 +76,20 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "assert_cmd" +version = "2.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9834fcc22e0874394a010230586367d4a3e9f11b560f469262678547e1d2575e" +dependencies = [ + "bstr 1.1.0", + "doc-comment", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + [[package]] name = "async-trait" version = "0.1.61" @@ -250,6 +264,18 @@ dependencies = [ "memchr", ] +[[package]] +name = "bstr" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b45ea9b00a7b3f2988e9a65ad3917e62123c38dba709b666506207be96d1790b" +dependencies = [ + "memchr", + "once_cell", + "regex-automata", + "serde", +] + [[package]] name = "bumpalo" version = "3.11.1" @@ -654,6 +680,12 @@ version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23d8666cb01533c39dde32bcbab8e227b4ed6679b2c925eba05feabea39508fb" +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + [[package]] name = "digest" version = "0.10.6" @@ -684,6 +716,7 @@ name = "dioxus-cli" version = "0.3.0" dependencies = [ "anyhow", + "assert_cmd", "atty", "axum", "binary-install", @@ -712,12 +745,14 @@ dependencies = [ "log", "mlua", "notify", + "predicates", "proc-macro2", "regex", "reqwest", "rsx-rosetta", "serde", "serde_json", + "subprocess", "syn", "tar", "thiserror", @@ -807,6 +842,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + [[package]] name = "either" version = "1.8.0" @@ -934,6 +975,15 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "float-cmp" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +dependencies = [ + "num-traits", +] + [[package]] name = "fnv" version = "1.0.7" @@ -1710,7 +1760,7 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ee2ad7a9aa69056b148d9d590344bc155d3ce0d2200e3b2838f7034f6ba33c1" dependencies = [ - "bstr", + "bstr 0.2.17", "cc", "futures-core", "futures-task", @@ -1769,6 +1819,12 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "normalize-line-endings" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" + [[package]] name = "notify" version = "5.0.0" @@ -2050,6 +2106,36 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "predicates" +version = "2.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59230a63c37f3e18569bdb90e4a89cbf5bf8b06fea0b84e65ea10cc4df47addd" +dependencies = [ + "difflib", + "float-cmp", + "itertools", + "normalize-line-endings", + "predicates-core", + "regex", +] + +[[package]] +name = "predicates-core" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f883590242d3c6fc5bf50299011695fa6590c2c70eac95ee1bdb9a733ad1a2" + +[[package]] +name = "predicates-tree" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54ff541861505aabf6ea722d2131ee980b8276e10a1297b94e896dd8b621850d" +dependencies = [ + "predicates-core", + "termtree", +] + [[package]] name = "prettyplease" version = "0.1.15" @@ -2185,6 +2271,12 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" + [[package]] name = "regex-syntax" version = "0.6.28" @@ -2573,6 +2665,16 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "subprocess" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c2e86926081dda636c546d8c5e641661049d7562a68f5488be4a1f7f66f6086" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "subtle" version = "2.4.1" @@ -2642,6 +2744,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "termtree" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95059e91184749cb66be6dc994f67f182b6d897cb3df74a5bf66b5e709295fd8" + [[package]] name = "textwrap" version = "0.16.0" @@ -3084,6 +3192,15 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + [[package]] name = "walkdir" version = "2.3.2" diff --git a/Cargo.toml b/Cargo.toml index f1e89966c..9fe15ba32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ chrono = "0.4.19" anyhow = "1.0.53" hyper = "0.14.17" indicatif = "0.17.0-rc.11" +subprocess = "0.2.9" axum = { version = "0.5.1", features = ["ws", "headers"] } tower-http = { version = "0.2.2", features = ["fs", "trace"] } diff --git a/README.md b/README.md index 4f11ce1ac..5a4f32a4b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@

📦✨ Dioxus CLI

Tooling to supercharge Dioxus projects

-**dioxus-cli** (inspired by wasm-pack and webpack) is a tool for getting Dioxus projects up and running. It handles all of the build, bundling, development and publishing to simplify web development. +**dioxus-cli** (inspired by wasm-pack and webpack) is a tool for getting Dioxus projects up and running. +It handles all build, bundling, development and publishing to simplify web development. ## Installation diff --git a/src/builder.rs b/src/builder.rs index c6e850b58..716903fad 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -48,39 +48,48 @@ pub fn build(config: &CrateConfig, quiet: bool) -> Result { // [1] Build the .wasm module log::info!("🚅 Running build command..."); - let mut cmd = Command::new("cargo"); - cmd.current_dir(&crate_dir) + let cmd = subprocess::Exec::cmd("cargo"); + let cmd = cmd.cwd(&crate_dir) .arg("build") .arg("--target") .arg("wasm32-unknown-unknown") - .arg("--message-format=json") - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::piped()); + .arg("--message-format=json"); - if config.release { - cmd.arg("--release"); - } - if config.verbose { - cmd.arg("--verbose"); - } + let cmd = if config.release { + cmd.arg("--release") + } else { + cmd + }; + let cmd = if config.verbose { + cmd.arg("--verbose") + } else { + cmd + }; - if quiet { - cmd.arg("--quiet"); - } + let cmd = if quiet { + cmd.arg("--quiet") + } else { + cmd + }; - if config.custom_profile.is_some() { + let cmd = if config.custom_profile.is_some() { let custom_profile = config.custom_profile.as_ref().unwrap(); - cmd.arg("--profile"); - cmd.arg(custom_profile); - } + cmd + .arg("--profile") + .arg(custom_profile) + } else { + cmd + }; - if config.features.is_some() { + let cmd = if config.features.is_some() { let features_str = config.features.as_ref().unwrap().join(" "); - cmd.arg("--features"); - cmd.arg(features_str); - } + cmd.arg("--features") + .arg(features_str) + } else { + cmd + }; - match executable { + let cmd = match executable { ExecutableType::Binary(name) => cmd.arg("--bin").arg(name), ExecutableType::Lib(name) => cmd.arg("--lib").arg(name), ExecutableType::Example(name) => cmd.arg("--example").arg(name), @@ -365,7 +374,7 @@ pub fn build_desktop(config: &CrateConfig, _is_serve: bool) -> Result<()> { Ok(()) } -fn prettier_build(mut cmd: Command) -> anyhow::Result> { +fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result> { let mut warning_messages: Vec = vec![]; let pb = ProgressBar::new_spinner(); @@ -377,8 +386,8 @@ fn prettier_build(mut cmd: Command) -> anyhow::Result> { ); pb.set_message("💼 Waiting to start build the project..."); - let mut command = cmd.spawn()?; - let reader = std::io::BufReader::new(command.stdout.take().unwrap()); + let stdout = cmd.stream_stdout()?; + let reader = std::io::BufReader::new(stdout); for message in cargo_metadata::Message::parse_stream(reader) { match message.unwrap() { Message::CompilerMessage(msg) => { diff --git a/src/cli/create/mod.rs b/src/cli/create/mod.rs index 228b87be9..c448e66d1 100644 --- a/src/cli/create/mod.rs +++ b/src/cli/create/mod.rs @@ -17,7 +17,7 @@ pub struct Create { impl Create { pub fn create(self) -> Result<()> { if Self::name_valid_check(self.name.clone()) { - return custom_error!("❗Unsupported project name."); + return custom_error!("❗Unsupported project name: '{}'.", &self.name); } let project_path = PathBuf::from(&self.name); @@ -26,7 +26,7 @@ impl Create { return custom_error!("🧨 Folder '{}' is initialized.", &self.name); } - log::info!("🔧 Start to create a new project '{}'.", self.name); + log::info!("🔧 Start: Creating new project '{}'.", self.name); let output = Command::new("cargo") .arg("generate") diff --git a/src/config.rs b/src/config.rs index 99c487fa2..0d9767b97 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,7 +18,7 @@ fn default_plugin() -> toml::Value { impl DioxusConfig { pub fn load() -> crate::error::Result> { - let crate_dir = crate::cargo::crate_root()?; + let Ok(crate_dir) = crate::cargo::crate_root() else { return Ok(None); }; // we support either `Dioxus.toml` or `Cargo.toml` let Some(dioxus_conf_file) = acquire_dioxus_toml(crate_dir) else { diff --git a/src/main.rs b/src/main.rs index 278fd43b6..f3c5c6e48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,9 +10,9 @@ async fn main() -> anyhow::Result<()> { set_up_logging(); let dioxus_config = DioxusConfig::load() - .map_err(|e| anyhow!("Failed to load `dioxus.toml` because: {e}"))? + .map_err(|e| anyhow!("Failed to load `Dioxus.toml` because: {e}"))? .unwrap_or_else(|| { - log::warn!("Your `dioxus.toml` could not be found. Using the default config. To set up this crate with dioxus, use `dioxus init`."); + log::warn!("You appear to be creating a Dioxus project from scratch; we will use the default config"); DioxusConfig::default() }); diff --git a/src/server/mod.rs b/src/server/mod.rs index ab1faf45a..08967a864 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -61,10 +61,11 @@ pub async fn startup(port: u16, config: CrateConfig) -> Result<()> { std::process::exit(0); }); + let ip = get_ip().unwrap_or(String::from("0.0.0.0")); if config.hot_reload { - startup_hot_reload(port, config).await? + startup_hot_reload(ip, port, config).await? } else { - startup_default(port, config).await? + startup_default(ip, port, config).await? } Ok(()) } @@ -126,7 +127,7 @@ pub async fn hot_reload_handler( } #[allow(unused_assignments)] -pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> { +pub async fn startup_hot_reload(ip: String, port: u16, config: CrateConfig) -> Result<()> { let first_build_result = crate::builder::build(&config, false)?; log::info!("🚀 Starting development server..."); @@ -165,6 +166,7 @@ pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> { .unwrap_or_else(|| vec![PathBuf::from("src")]); let watcher_config = config.clone(); + let watcher_ip = ip.clone(); let mut last_update_time = chrono::Local::now().timestamp(); let mut watcher = RecommendedWatcher::new( @@ -190,6 +192,7 @@ pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> { match build_manager.rebuild() { Ok(res) => { print_console_info( + &watcher_ip, port, &config, PrettierOptions { @@ -229,6 +232,7 @@ pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> { // start serve dev-server at 0.0.0.0:8080 print_console_info( + &ip, port, &config, PrettierOptions { @@ -298,7 +302,7 @@ pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> { Ok(()) } -pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> { +pub async fn startup_default(ip: String, port: u16, config: CrateConfig) -> Result<()> { let first_build_result = crate::builder::build(&config, false)?; log::info!("🚀 Starting development server..."); @@ -328,6 +332,7 @@ pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> { .unwrap_or_else(|| vec![PathBuf::from("src")]); let watcher_config = config.clone(); + let watcher_ip = ip.clone(); let mut watcher = notify::recommended_watcher(move |info: notify::Result| { let config = watcher_config.clone(); if let Ok(e) = info { @@ -336,6 +341,7 @@ pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> { Ok(res) => { last_update_time = chrono::Local::now().timestamp(); print_console_info( + &watcher_ip, port, &config, PrettierOptions { @@ -367,6 +373,7 @@ pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> { // start serve dev-server at 0.0.0.0 print_console_info( + &ip, port, &config, PrettierOptions { @@ -441,7 +448,7 @@ pub struct PrettierOptions { elapsed_time: u128, } -fn print_console_info(port: u16, config: &CrateConfig, options: PrettierOptions) { +fn print_console_info(ip: &String, port: u16, config: &CrateConfig, options: PrettierOptions) { if let Ok(native_clearseq) = Command::new(if cfg!(target_os = "windows") { "cls" } else { @@ -511,12 +518,7 @@ fn print_console_info(port: u16, config: &CrateConfig, options: PrettierOptions) ); println!( "\t> NetWork : {}", - format!( - "http://{}:{}/", - get_ip().unwrap_or(String::from("0.0.0.0")), - port - ) - .blue() + format!("http://{}:{}/", ip, port).blue() ); println!(""); println!("\t> Profile : {}", profile.green());