Skip to content

Commit 68becb3

Browse files
committed
smaller refactor
1 parent dda3167 commit 68becb3

File tree

8 files changed

+130
-118
lines changed

8 files changed

+130
-118
lines changed

packages/cli/src/build/builder.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,17 @@ impl Builder {
177177
update
178178
}
179179

180-
pub(crate) fn patch_rebuild(&mut self, args: BuildArgs) {
180+
pub(crate) fn patch_rebuild(&mut self, args: BuildArgs, direct_rustc: Vec<Vec<String>>) {
181181
// Abort all the ongoing builds, cleaning up any loose artifacts and waiting to cleanly exit
182182
self.abort_all();
183183

184184
// And then start a new build, resetting our progress/stage to the beginning and replacing the old tokio task
185-
let request = BuildRequest::new(self.krate.clone(), args, self.tx.clone(), BuildMode::Thin);
185+
let request = BuildRequest::new(
186+
self.krate.clone(),
187+
args,
188+
self.tx.clone(),
189+
BuildMode::Thin { direct_rustc },
190+
);
186191
self.request = request.clone();
187192
self.stage = BuildStage::Restarting;
188193

packages/cli/src/build/bundle.rs

+34-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::prerender::pre_render_static_routes;
22
use super::templates::InfoPlistData;
3-
use crate::{BuildRequest, Platform, WasmOptConfig};
3+
use crate::{BuildMode, BuildRequest, Platform, WasmOptConfig};
44
use crate::{Result, TraceSrc};
55
use anyhow::Context;
66
use dioxus_cli_opt::{process_file_to, AssetManifest};
@@ -282,27 +282,35 @@ impl AppBundle {
282282
server_assets: Default::default(),
283283
};
284284

285-
tracing::debug!("Assembling app bundle");
286-
287-
bundle.build.status_start_bundle();
288-
bundle
289-
.write_main_executable()
290-
.await
291-
.context("Failed to write main executable")?;
292-
bundle.write_server_executable().await?;
293-
bundle
294-
.write_assets()
295-
.await
296-
.context("Failed to write assets")?;
297-
bundle.write_metadata().await?;
298-
bundle.optimize().await?;
299-
bundle.pre_render_ssg_routes().await?;
300-
bundle
301-
.assemble()
302-
.await
303-
.context("Failed to assemble app bundle")?;
304-
305-
tracing::debug!("Bundle created at {}", bundle.build.root_dir().display());
285+
match bundle.build.mode {
286+
BuildMode::Base | BuildMode::Fat => {
287+
tracing::debug!("Assembling app bundle");
288+
289+
bundle.build.status_start_bundle();
290+
bundle
291+
.write_main_executable()
292+
.await
293+
.context("Failed to write main executable")?;
294+
bundle.write_server_executable().await?;
295+
bundle
296+
.write_assets()
297+
.await
298+
.context("Failed to write assets")?;
299+
bundle.write_metadata().await?;
300+
bundle.optimize().await?;
301+
bundle.pre_render_ssg_routes().await?;
302+
bundle
303+
.assemble()
304+
.await
305+
.context("Failed to assemble app bundle")?;
306+
307+
tracing::debug!("Bundle created at {}", bundle.build.root_dir().display());
308+
}
309+
BuildMode::Thin { .. } => {
310+
tracing::debug!("Patching existing bundle");
311+
bundle.write_patch().await?;
312+
}
313+
}
306314

307315
Ok(bundle)
308316
}
@@ -533,6 +541,10 @@ impl AppBundle {
533541
Ok(())
534542
}
535543

544+
async fn write_patch(&self) -> Result<()> {
545+
Ok(())
546+
}
547+
536548
/// The item that we'll try to run directly if we need to.
537549
///
538550
/// todo(jon): we should name the app properly instead of making up the exe name. It's kinda okay for dev mode, but def not okay for prod

packages/cli/src/build/request.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub enum BuildMode {
4343
Fat,
4444

4545
/// A "thin" build generated with `rustc` directly and dx as a custom linker
46-
Thin,
46+
Thin { direct_rustc: Vec<Vec<String>> },
4747
}
4848

4949
pub struct CargoBuildResult {
@@ -86,17 +86,7 @@ impl BuildRequest {
8686
false => (self.cargo_build().await?, self.build_server().await?),
8787
};
8888

89-
// let mut app_bundle = AppBundle::new {
90-
// app,
91-
// server,
92-
// build: self,
93-
// assets: Default::default(),
94-
// server_assets: Default::default(),
95-
// };
96-
97-
// Ok(app_bundle)
98-
99-
todo!()
89+
AppBundle::new(self, app, server).await
10090
}
10191

10292
pub(crate) async fn build_server(&self) -> Result<Option<BuildArtifacts>> {
@@ -367,7 +357,7 @@ impl BuildRequest {
367357
}
368358

369359
match self.mode {
370-
BuildMode::Fat | BuildMode::Thin => cargo_args.push(format!(
360+
BuildMode::Fat | BuildMode::Thin { .. } => cargo_args.push(format!(
371361
"-Clinker={}",
372362
dunce::canonicalize(std::env::current_exe().unwrap())
373363
.unwrap()
@@ -586,7 +576,7 @@ impl BuildRequest {
586576
}
587577
.to_json(),
588578
)),
589-
BuildMode::Thin => env_vars.push((
579+
BuildMode::Thin { .. } => env_vars.push((
590580
LinkAction::ENV_VAR_NAME,
591581
LinkAction::ThinLink {
592582
platform: self.build.platform(),

packages/cli/src/cli/run.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::*;
2-
use crate::{serve::ServeUpdate, BuildArgs, Builder, DioxusCrate, Platform, Result};
2+
use crate::{serve::HandleUpdate, BuildArgs, Builder, DioxusCrate, Platform, Result};
33

44
/// Run the project with the given arguments
55
#[derive(Clone, Debug, Parser)]
@@ -38,28 +38,18 @@ impl RunArgs {
3838
// Run the app, but mostly ignore all the other messages
3939
// They won't generally be emitted
4040
loop {
41-
match runner.wait().await {
42-
ServeUpdate::StderrReceived { platform, msg } => {
41+
match runner.running.as_mut().unwrap().wait().await {
42+
HandleUpdate::StderrReceived { platform, msg } => {
4343
tracing::info!("[{platform}]: {msg}")
4444
}
45-
ServeUpdate::StdoutReceived { platform, msg } => {
45+
HandleUpdate::StdoutReceived { platform, msg } => {
4646
tracing::info!("[{platform}]: {msg}")
4747
}
48-
ServeUpdate::ProcessExited { platform, status } => {
48+
HandleUpdate::ProcessExited { platform, status } => {
4949
runner.cleanup().await;
5050
tracing::info!("[{platform}]: process exited with status: {status:?}");
5151
break;
5252
}
53-
ServeUpdate::BuildUpdate { .. } => {}
54-
ServeUpdate::TracingLog { .. } => {}
55-
ServeUpdate::Exit { .. } => break,
56-
ServeUpdate::NewConnection => {}
57-
ServeUpdate::WsMessage(_) => {}
58-
ServeUpdate::FilesChanged { .. } => {}
59-
ServeUpdate::RequestRebuild => {}
60-
ServeUpdate::Redraw => {}
61-
ServeUpdate::OpenApp => {}
62-
ServeUpdate::ToggleShouldRebuild => {}
6353
}
6454
}
6555

packages/cli/src/serve/handle.rs

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::{AppBundle, DioxusCrate, Platform, Result};
22
use anyhow::Context;
33
use dioxus_cli_opt::process_file_to;
4+
use futures_util::future::OptionFuture;
45
use std::{
56
net::SocketAddr,
67
path::{Path, PathBuf},
7-
process::Stdio,
8+
process::{ExitStatus, Stdio},
89
};
910
use tokio::{
1011
io::{AsyncBufReadExt, BufReader, Lines},
@@ -48,6 +49,25 @@ pub(crate) struct AppHandle {
4849
pub(crate) runtime_asst_dir: Option<PathBuf>,
4950
}
5051

52+
pub enum HandleUpdate {
53+
/// A running process has received a stdout.
54+
/// May or may not be a complete line - do not treat it as a line. It will include a line if it is a complete line.
55+
///
56+
/// We will poll lines and any content in a 50ms interval
57+
StdoutReceived { platform: Platform, msg: String },
58+
59+
/// A running process has received a stderr.
60+
/// May or may not be a complete line - do not treat it as a line. It will include a line if it is a complete line.
61+
///
62+
/// We will poll lines and any content in a 50ms interval
63+
StderrReceived { platform: Platform, msg: String },
64+
65+
ProcessExited {
66+
platform: Platform,
67+
status: ExitStatus,
68+
},
69+
}
70+
5171
impl AppHandle {
5272
pub async fn new(app: AppBundle) -> Result<Self> {
5373
Ok(AppHandle {
@@ -64,6 +84,44 @@ impl AppHandle {
6484
})
6585
}
6686

87+
pub(crate) async fn wait(&mut self) -> HandleUpdate {
88+
let platform = self.app.build.build.platform();
89+
use HandleUpdate::*;
90+
tokio::select! {
91+
Some(Ok(Some(msg))) = OptionFuture::from(self.app_stdout.as_mut().map(|f| f.next_line())) => {
92+
StdoutReceived { platform, msg }
93+
},
94+
Some(Ok(Some(msg))) = OptionFuture::from(self.app_stderr.as_mut().map(|f| f.next_line())) => {
95+
StderrReceived { platform, msg }
96+
},
97+
Some(status) = OptionFuture::from(self.app_child.as_mut().map(|f| f.wait())) => {
98+
match status {
99+
Ok(status) => {
100+
self.app_child = None;
101+
ProcessExited { status, platform }
102+
},
103+
Err(_err) => todo!("handle error in process joining?"),
104+
}
105+
}
106+
Some(Ok(Some(msg))) = OptionFuture::from(self.server_stdout.as_mut().map(|f| f.next_line())) => {
107+
StdoutReceived { platform: Platform::Server, msg }
108+
},
109+
Some(Ok(Some(msg))) = OptionFuture::from(self.server_stderr.as_mut().map(|f| f.next_line())) => {
110+
StderrReceived { platform: Platform::Server, msg }
111+
},
112+
Some(status) = OptionFuture::from(self.server_child.as_mut().map(|f| f.wait())) => {
113+
match status {
114+
Ok(status) => {
115+
self.server_child = None;
116+
ProcessExited { status, platform }
117+
},
118+
Err(_err) => todo!("handle error in process joining?"),
119+
}
120+
}
121+
else => futures_util::future::pending().await
122+
}
123+
}
124+
67125
pub(crate) async fn open(
68126
&mut self,
69127
devserver_ip: SocketAddr,

packages/cli/src/serve/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,17 @@ pub(crate) async fn serve_all(mut args: ServeArgs) -> Result<()> {
105105
devserver.send_hotreload(hr).await;
106106
}
107107
HotReloadKind::Patch => {
108-
builder.patch_rebuild(args.build_arguments.clone());
108+
if let Some(handle) = runner.running.as_ref() {
109+
builder.patch_rebuild(
110+
args.build_arguments.clone(),
111+
handle.app.app.direct_rustc.clone(),
112+
);
109113

110-
runner.clear_hot_reload_changes();
111-
runner.clear_cached_rsx();
114+
runner.clear_hot_reload_changes();
115+
runner.clear_cached_rsx();
112116

113-
devserver.start_patch().await
117+
devserver.start_patch().await
118+
}
114119
}
115120
HotReloadKind::Full {} => todo!(),
116121
}
@@ -170,9 +175,8 @@ pub(crate) async fn serve_all(mut args: ServeArgs) -> Result<()> {
170175
}
171176

172177
BuildUpdate::BuildReady { bundle } if bundle.build.is_patch() => {
178+
runner.patch(&bundle).await?;
173179
devserver.send_patch(bundle.app.exe.clone()).await;
174-
// if runner.patch(bundle).await.is_ok() {
175-
// }
176180
}
177181

178182
BuildUpdate::BuildReady { bundle } => {
@@ -195,7 +199,7 @@ pub(crate) async fn serve_all(mut args: ServeArgs) -> Result<()> {
195199
}
196200

197201
// If the process exited *cleanly*, we can exit
198-
ServeUpdate::ProcessExited { status, platform } => {
202+
ServeUpdate::HandleUpdate(HandleUpdate::ProcessExited { status, platform }) => {
199203
if !status.success() {
200204
tracing::error!("Application [{platform}] exited with error: {status}");
201205
} else {
@@ -207,11 +211,11 @@ pub(crate) async fn serve_all(mut args: ServeArgs) -> Result<()> {
207211
}
208212
}
209213

210-
ServeUpdate::StdoutReceived { platform, msg } => {
214+
ServeUpdate::HandleUpdate(HandleUpdate::StdoutReceived { platform, msg }) => {
211215
screen.push_stdio(platform, msg, tracing::Level::INFO);
212216
}
213217

214-
ServeUpdate::StderrReceived { platform, msg } => {
218+
ServeUpdate::HandleUpdate(HandleUpdate::StderrReceived { platform, msg }) => {
215219
screen.push_stdio(platform, msg, tracing::Level::ERROR);
216220
}
217221

packages/cli/src/serve/runner.rs

+4-33
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use syn::spanned::Spanned;
2020

2121
pub(crate) struct AppRunner {
2222
pub(crate) running: Option<AppHandle>,
23+
pub(crate) app: Option<AppBundle>,
2324
pub(crate) krate: DioxusCrate,
2425
pub(crate) ignore: Gitignore,
2526
pub(crate) applied_hot_reload_message: HotReloadMsg,
@@ -45,6 +46,7 @@ impl AppRunner {
4546
pub(crate) fn start(krate: &DioxusCrate) -> Self {
4647
let mut runner = Self {
4748
running: Default::default(),
49+
app: Default::default(),
4850
file_map: Default::default(),
4951
applied_hot_reload_message: Default::default(),
5052
ignore: krate.workspace_gitignore(),
@@ -72,39 +74,8 @@ impl AppRunner {
7274
return futures_util::future::pending().await;
7375
};
7476

75-
use ServeUpdate::*;
76-
let platform = handle.app.build.build.platform();
7777
tokio::select! {
78-
Some(Ok(Some(msg))) = OptionFuture::from(handle.app_stdout.as_mut().map(|f| f.next_line())) => {
79-
StdoutReceived { platform, msg }
80-
},
81-
Some(Ok(Some(msg))) = OptionFuture::from(handle.app_stderr.as_mut().map(|f| f.next_line())) => {
82-
StderrReceived { platform, msg }
83-
},
84-
Some(status) = OptionFuture::from(handle.app_child.as_mut().map(|f| f.wait())) => {
85-
match status {
86-
Ok(status) => {
87-
handle.app_child = None;
88-
ProcessExited { status, platform }
89-
},
90-
Err(_err) => todo!("handle error in process joining?"),
91-
}
92-
}
93-
Some(Ok(Some(msg))) = OptionFuture::from(handle.server_stdout.as_mut().map(|f| f.next_line())) => {
94-
StdoutReceived { platform: Platform::Server, msg }
95-
},
96-
Some(Ok(Some(msg))) = OptionFuture::from(handle.server_stderr.as_mut().map(|f| f.next_line())) => {
97-
StderrReceived { platform: Platform::Server, msg }
98-
},
99-
Some(status) = OptionFuture::from(handle.server_child.as_mut().map(|f| f.wait())) => {
100-
match status {
101-
Ok(status) => {
102-
handle.server_child = None;
103-
ProcessExited { status, platform }
104-
},
105-
Err(_err) => todo!("handle error in process joining?"),
106-
}
107-
}
78+
update = handle.wait() => ServeUpdate::HandleUpdate(update),
10879
else => futures_util::future::pending().await
10980
}
11081
}
@@ -456,7 +427,7 @@ impl AppRunner {
456427
_ = std::fs::create_dir_all(&cache_dir);
457428
}
458429

459-
pub async fn patch(&mut self, bundle: AppBundle) -> Result<()> {
430+
pub async fn patch(&mut self, bundle: &AppBundle) -> Result<()> {
460431
let Some(running) = self.running.as_mut() else {
461432
return Ok(());
462433
};

0 commit comments

Comments
 (0)