From ef55c53c34fca5c7a039903f56551988997791a5 Mon Sep 17 00:00:00 2001 From: iscas-zac <92451907+iscas-zac@users.noreply.github.com> Date: Wed, 24 May 2023 16:19:26 +0800 Subject: [PATCH 01/10] add mandatory hint to the config file. --- docs/src/configure.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/src/configure.md b/docs/src/configure.md index 95408580c..dd28d50d6 100644 --- a/docs/src/configure.md +++ b/docs/src/configure.md @@ -1,15 +1,15 @@ # Configure Project -This chapter will introduce `Dioxus.toml` and anatomy the config file. +This chapter will introduce `Dioxus.toml` and anatomize the config file. Be aware that if the config file is present in the folder, some fields must be filled out, or the CLI tool will abort. The mandatory [table headers](https://toml.io/en/v1.0.0#table) and keys will have a '✍' sign beside it. ## Structure We use `toml` to define some info for `dioxus` project. -### Application +### Application ✍ -1. ***name*** - project name & title -2. ***default_platform*** - which platform target for this project. +1. ***name*** ✍ - project name & title +2. ***default_platform*** ✍ - which platform target for this project. ``` # current support: web, desktop # default: web @@ -49,7 +49,7 @@ You can combine different tools with `dioxus`. Note : Dioxus will automatically include the generated tailwind file in the `index.html` -### Web.App +### Web.App ✍ Web platform application config: @@ -59,7 +59,7 @@ Web platform application config: title = "dioxus app | ⛺" ``` -### Web.Watcher +### Web.Watcher ✍ Web platform `dev-server` watcher config: @@ -73,7 +73,7 @@ Web platform `dev-server` watcher config: watch_path = ["src", "public"] ``` -### Web.Resource +### Web.Resource ✍ Include some `CSS Javascript` resources into html file. @@ -96,7 +96,7 @@ Include some `CSS Javascript` resources into html file. ] ``` -### Web.Resource.Dev +### Web.Resource.Dev ✍ Only include resources at `Dev` mode. From 808da6e07fb03515c4268a44f8c7c519a813bbfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6sters?= Date: Fri, 2 Jun 2023 07:31:40 +0200 Subject: [PATCH 02/10] fix: disable base_path changes for serve --- src/cli/serve/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cli/serve/mod.rs b/src/cli/serve/mod.rs index 33a3ee576..2e0477d42 100644 --- a/src/cli/serve/mod.rs +++ b/src/cli/serve/mod.rs @@ -36,6 +36,9 @@ impl Serve { crate_config.set_features(self.serve.features.unwrap()); } + // Subdirectories don't work with the server + crate_config.dioxus_config.web.app.base_path = None; + let platform = self.serve.platform.unwrap_or_else(|| { crate_config .dioxus_config From 6306e3ca5ffaf6461cbd456f96671676c7feab17 Mon Sep 17 00:00:00 2001 From: trevyn <230691+trevyn@users.noreply.github.com> Date: Sun, 4 Jun 2023 14:39:19 +0400 Subject: [PATCH 03/10] Fix spelling: "inhert" -> "inherit" --- docs/src/plugin/interface/command.md | 2 +- src/plugin/interface/command.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/src/plugin/interface/command.md b/docs/src/plugin/interface/command.md index c9b1dc48f..18cd32769 100644 --- a/docs/src/plugin/interface/command.md +++ b/docs/src/plugin/interface/command.md @@ -4,7 +4,7 @@ Type Define: ``` -Stdio: "Inhert" | "Piped" | "Null" +Stdio: "Inherit" | "Piped" | "Null" ``` ### `exec(commands: [string], stdout: Stdio, stderr: Stdio)` diff --git a/src/plugin/interface/command.rs b/src/plugin/interface/command.rs index 373689f83..641fe328a 100644 --- a/src/plugin/interface/command.rs +++ b/src/plugin/interface/command.rs @@ -3,7 +3,7 @@ use std::process::{Command, Stdio}; use mlua::{FromLua, UserData}; enum StdioFromString { - Inhert, + Inherit, Piped, Null, } @@ -12,19 +12,19 @@ impl<'lua> FromLua<'lua> for StdioFromString { if let mlua::Value::String(v) = lua_value { let v = v.to_str().unwrap(); return Ok(match v.to_lowercase().as_str() { - "inhert" => Self::Inhert, + "inherit" => Self::Inherit, "piped" => Self::Piped, "null" => Self::Null, - _ => Self::Inhert, + _ => Self::Inherit, }); } - Ok(Self::Inhert) + Ok(Self::Inherit) } } impl StdioFromString { pub fn to_stdio(self) -> Stdio { match self { - StdioFromString::Inhert => Stdio::inherit(), + StdioFromString::Inherit => Stdio::inherit(), StdioFromString::Piped => Stdio::piped(), StdioFromString::Null => Stdio::null(), } From c398372785f36cc0f9409820733f541f749b6e9d Mon Sep 17 00:00:00 2001 From: Miles Murgaw Date: Sun, 4 Jun 2023 12:44:43 -0400 Subject: [PATCH 04/10] fix: formatting a single file with --file --- src/cli/autoformat/mod.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/cli/autoformat/mod.rs b/src/cli/autoformat/mod.rs index b4e8b6f21..61d9a66ab 100644 --- a/src/cli/autoformat/mod.rs +++ b/src/cli/autoformat/mod.rs @@ -1,5 +1,5 @@ use futures::{stream::FuturesUnordered, StreamExt}; -use std::process::exit; +use std::{process::exit, fs}; use super::*; @@ -44,10 +44,28 @@ impl Autoformat { } } + // Format single file if let Some(file) = self.file { - let edits = dioxus_autofmt::fmt_file(&file); - let as_json = serde_json::to_string(&edits).unwrap(); - println!("{}", as_json); + let file_content = fs::read_to_string(&file); + + match file_content { + Ok(s) => { + let edits = dioxus_autofmt::fmt_file(&s); + let out = dioxus_autofmt::apply_formats(&s, edits); + match fs::write(&file, out) { + Ok(_) => { + println!("formatted {}", file); + } + Err(e) => { + println!("failed to write formatted content to file: {}", e); + } + } + }, + Err(e) => { + eprintln!("failed to open file: {}", e); + exit(1); + } + } } Ok(()) From e8d011b476085635cd482dba294eb6e5dac01de6 Mon Sep 17 00:00:00 2001 From: Miles Murgaw Date: Sun, 4 Jun 2023 12:48:28 -0400 Subject: [PATCH 05/10] revision: fmt --- src/cli/autoformat/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli/autoformat/mod.rs b/src/cli/autoformat/mod.rs index 61d9a66ab..9a93942f8 100644 --- a/src/cli/autoformat/mod.rs +++ b/src/cli/autoformat/mod.rs @@ -1,5 +1,5 @@ use futures::{stream::FuturesUnordered, StreamExt}; -use std::{process::exit, fs}; +use std::{fs, process::exit}; use super::*; @@ -60,7 +60,7 @@ impl Autoformat { println!("failed to write formatted content to file: {}", e); } } - }, + } Err(e) => { eprintln!("failed to open file: {}", e); exit(1); From 8cf6f60e9390d4e4d0b88528fbf0c055d481a5a0 Mon Sep 17 00:00:00 2001 From: Miles Murgaw Date: Sun, 4 Jun 2023 12:49:22 -0400 Subject: [PATCH 06/10] fix: change error println to eprintln --- src/cli/autoformat/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/autoformat/mod.rs b/src/cli/autoformat/mod.rs index 9a93942f8..2bed439cd 100644 --- a/src/cli/autoformat/mod.rs +++ b/src/cli/autoformat/mod.rs @@ -57,7 +57,7 @@ impl Autoformat { println!("formatted {}", file); } Err(e) => { - println!("failed to write formatted content to file: {}", e); + eprintln!("failed to write formatted content to file: {}", e); } } } From 9d7aeb398b395beb521a679947e1a2457d80eb5a Mon Sep 17 00:00:00 2001 From: Miles Murgaw Date: Sun, 4 Jun 2023 21:40:52 -0400 Subject: [PATCH 07/10] fix: vscode extension autofmt --- extension/src/main.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/extension/src/main.ts b/extension/src/main.ts index e9580d3bb..adaed0a7d 100644 --- a/extension/src/main.ts +++ b/extension/src/main.ts @@ -136,28 +136,26 @@ function fmtDocument(document: vscode.TextDocument) { const [editor,] = vscode.window.visibleTextEditors.filter(editor => editor.document.fileName === document.fileName); if (!editor) return; // Need an editor to apply text edits. - const text = document.getText(); const fileDir = document.fileName.slice(0, document.fileName.lastIndexOf('\\')); - - const child_proc = spawn(serverPath, ["fmt", "--file", text], { + const child_proc = spawn(serverPath, ["fmt", "--file", document.fileName], { cwd: fileDir ? fileDir : undefined, }); let result = ''; child_proc.stdout?.on('data', data => result += data); - type RsxEdit = { + /*type RsxEdit = { formatted: string, start: number, end: number - } + }*/ child_proc.on('close', () => { if (child_proc.exitCode !== 0) { vscode.window.showWarningMessage(`Errors occurred while formatting. Make sure you have the most recent Dioxus-CLI installed!\nDioxus-CLI exited with exit code ${child_proc.exitCode}\n\nData from Dioxus-CLI:\n${result}`); return; } - if (result.length === 0) return; + /*if (result.length === 0) return; // Used for error message: const originalResult = result; @@ -245,9 +243,10 @@ function fmtDocument(document: vscode.TextDocument) { undoStopAfter: false, undoStopBefore: false }); + } catch (err) { vscode.window.showWarningMessage(`Errors occurred while formatting. Make sure you have the most recent Dioxus-CLI installed!\n${err}\n\nData from Dioxus-CLI:\n${originalResult}`); - } + }*/ }); child_proc.on('error', (err) => { @@ -259,6 +258,7 @@ function fmtDocument(document: vscode.TextDocument) { } + // I'm using the approach defined in rust-analyzer here // // We ship the server as part of the extension, but we need to handle external paths and such From ae80702ed9203768023271d6085c4db5ed6cf489 Mon Sep 17 00:00:00 2001 From: erithax Date: Fri, 16 Jun 2023 17:58:38 +0200 Subject: [PATCH 08/10] fix chosing launch target it used to match in order: * first binary alphabetically * library now matches in order: 1. default-run binary 2. binary with name of crate or located at src/main.rs 3. first binary alphabetically 4. library --- src/config.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/config.rs b/src/config.rs index e4dd7c416..9c87e266e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -186,15 +186,21 @@ impl CrateConfig { let manifest = cargo_toml::Manifest::from_path(&cargo_def).unwrap(); - // We just assume they're using a 'main.rs' - // Anyway, we've already parsed the manifest, so it should be easy to change the type - let output_filename = manifest - .bin - .first() - .or(manifest.lib.as_ref()) - .and_then(|product| product.name.clone()) - .or_else(|| manifest.package.as_ref().map(|pkg| pkg.name.clone())) - .expect("No lib found from cargo metadata"); + let output_filename = { + match &manifest.package.as_ref().unwrap().default_run { + Some(default_run_target) => { + default_run_target.to_owned() + }, + None => { + manifest.bin.iter().find(|b| b.name == manifest.package.as_ref().map(|pkg| pkg.name.clone())) + .or(manifest.bin.iter().find(|b| b.path == Some("src/main.rs".to_owned()))) + .or(manifest.bin.first()) + .or(manifest.lib.as_ref()) + .and_then(|prod| prod.name.clone()) + .expect("No executable or library found from cargo metadata.") + } + } + }; let executable = ExecutableType::Binary(output_filename); let release = false; From 16ab96aa0a47f4ed7a589b4b4abe940e82225ce6 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Wed, 21 Jun 2023 16:07:21 -0700 Subject: [PATCH 09/10] Automatically insert scripts if they are not found --- src/builder.rs | 53 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index d81408740..b5d107046 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -462,7 +462,8 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String { { style_str.push_str("\n"); } - html = html.replace("{style_include}", &style_str); + + replace_or_insert_before("{style_include}", &style_str, " String { )) } - html = html.replace("{script_include}", &script_str); + replace_or_insert_before("{script_include}", &script_str, " String { ); } - html = html.replace("{app_name}", &config.application.name); - - html = match &config.web.app.base_path { - Some(path) => html.replace("{base_path}", path), - None => html.replace("{base_path}", "."), + let base_path = match &config.web.app.base_path { + Some(path) => path, + None => ".", }; + let app_name = &config.application.name; + // Check if a script already exists + if html.contains("{app_name}") && html.contains("{base_path}") { + html = html.replace("{app_name}", app_name); + + html = html.replace("{base_path}", base_path); + } else { + // If not, insert the script + html = html.replace( + " + import init from "/{base_path}/assets/dioxus/{app_name}.js"; + init("/{base_path}/assets/dioxus/{app_name}_bg.wasm").then(wasm => {{ + if (wasm.__wbindgen_start == undefined) {{ + wasm.main(); + }} + }}); + + String { .clone() .unwrap_or_else(|| "dioxus | ⛺".into()); - html.replace("{app_title}", &title) + replace_or_insert_before("{app_title}", &title, " Date: Wed, 21 Jun 2023 17:27:01 -0700 Subject: [PATCH 10/10] fix some errors not being reported --- src/builder.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index d81408740..c06fb945d 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -380,7 +380,17 @@ fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result> { ); pb.set_message("💼 Waiting to start build the project..."); - let stdout = cmd.stream_stdout()?; + struct StopSpinOnDrop(ProgressBar); + + impl Drop for StopSpinOnDrop { + fn drop(&mut self) { + self.0.finish_and_clear(); + } + } + + StopSpinOnDrop(pb.clone()); + + let stdout = cmd.detached().stream_stdout()?; let reader = std::io::BufReader::new(stdout); for message in cargo_metadata::Message::parse_stream(reader) { match message.unwrap() { @@ -388,9 +398,11 @@ 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()); @@ -407,7 +419,6 @@ fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result> { } Message::BuildFinished(finished) => { if finished.success { - pb.finish_and_clear(); log::info!("👑 Build done."); } else { std::process::exit(1);