From 1426b804afdd07d8464523cd297a48da9b8be33b Mon Sep 17 00:00:00 2001 From: TheAlan404 Date: Wed, 8 Nov 2023 20:55:04 +0300 Subject: [PATCH] fix hotreload --- src/commands/dev.rs | 3 -- src/hot_reload/mod.rs | 117 +++++++++++++++++------------------------- 2 files changed, 47 insertions(+), 73 deletions(-) diff --git a/src/commands/dev.rs b/src/commands/dev.rs index f93a8cf..9faf3c4 100644 --- a/src/commands/dev.rs +++ b/src/commands/dev.rs @@ -35,9 +35,6 @@ pub async fn run(app: App) -> Result<()> { let mut dev_session = DevSession { builder, - child: None, - command_reciever: None, - command_sender: None, jar_name: None, }; diff --git a/src/hot_reload/mod.rs b/src/hot_reload/mod.rs index 43015f9..347c6f4 100644 --- a/src/hot_reload/mod.rs +++ b/src/hot_reload/mod.rs @@ -2,9 +2,9 @@ use std::{process::Stdio, time::Duration, path::PathBuf, sync::{Mutex, Arc}}; use anyhow::Result; use console::style; -use notify_debouncer_mini::{new_debouncer, Debouncer, notify::{RecommendedWatcher, EventKind, RecursiveMode}}; +use notify_debouncer_mini::{new_debouncer, Debouncer, notify::{RecommendedWatcher, RecursiveMode}, DebounceEventResult}; use pathdiff::diff_paths; -use tokio::{io::{AsyncBufReadExt, AsyncWriteExt, BufReader}, sync::mpsc, task::JoinHandle, process::Child}; +use tokio::{io::{AsyncBufReadExt, AsyncWriteExt, BufReader}, sync::mpsc, process::Child}; use crate::core::BuildContext; @@ -15,9 +15,6 @@ pub mod pattern_serde; #[derive(Debug)] pub struct DevSession<'a> { - pub child: Option, - pub command_sender: Option>, - pub command_reciever: Option>, pub builder: BuildContext<'a>, pub jar_name: Option, } @@ -31,13 +28,6 @@ pub enum Command { Bootstrap(PathBuf), } -pub enum State { - Starting, - Stopping, - Building, - Online, -} - async fn try_read_line(opt: &mut Option>>) -> Result> { match opt { Some(lines) => Ok(lines.next_line().await?), @@ -78,6 +68,7 @@ impl<'a> DevSession<'a> { ) } + #[allow(unused_assignments)] async fn handle_commands(mut self, mut rx: mpsc::Receiver) -> Result<()> { let mp = self.builder.app.multi_progress.clone(); @@ -213,26 +204,20 @@ impl<'a> DevSession<'a> { pub fn create_hotreload_watcher( config: Arc>, - tx: mpsc::Sender, + _tx: mpsc::Sender, ) -> Result> { - Ok(new_debouncer(Duration::from_secs(1), move |e| { - if let Ok(e) = e { - for e in e { - if !matches!(e.kind, EventKind::Create(_) | EventKind::Modify(_)) { - continue; - }; - - let mut guard = config.lock().unwrap(); - - match HotReloadConfig::load_from(&guard.path) { - Ok(updated) => { - eprintln!("Updated hotreload.toml :3"); - *guard = updated; - } - Err(e) => { - eprintln!("hotreload.toml error: {e}"); - eprintln!("cannot update hotreload.toml"); - } + Ok(new_debouncer(Duration::from_secs(1), move |e: DebounceEventResult| { + if let Ok(_e) = e { + let mut guard = config.lock().unwrap(); + + match HotReloadConfig::load_from(&guard.path) { + Ok(updated) => { + eprintln!("Updated hotreload.toml :3"); + *guard = updated; + } + Err(e) => { + eprintln!("hotreload.toml error: {e}"); + eprintln!("cannot update hotreload.toml"); } } } @@ -243,45 +228,41 @@ impl<'a> DevSession<'a> { config: Arc>, tx: mpsc::Sender, ) -> Result> { - Ok(new_debouncer(Duration::from_secs(1), move |e| { + Ok(new_debouncer(Duration::from_secs(1), move |e: DebounceEventResult| { if let Ok(e) = e { for e in e { - if !matches!(e.kind, EventKind::Create(_) | EventKind::Modify(_)) { - continue; - }; + let path = e.path; - for path in e.paths { - if path.is_dir() { - return; - } + if path.is_dir() || !path.exists() { + continue; + } - tx.blocking_send(Command::Bootstrap(path.clone())).unwrap(); + tx.blocking_send(Command::Bootstrap(path.clone())).unwrap(); - let guard = config.lock().unwrap(); - let Some(file) = guard.files.iter().find(|f| { - f.path.matches_path(&path) - }).cloned() else { - return; - }; - drop(guard); + let guard = config.lock().unwrap(); + let Some(file) = guard.files.iter().find(|f| { + f.path.matches_path(&path) + }).cloned() else { + continue; + }; + drop(guard); - match &file.action { - HotReloadAction::Reload => { - tx.blocking_send(Command::SendCommand("reload confirm\n".to_owned())) - .expect("tx send err"); - } - HotReloadAction::Restart => { - tx.blocking_send(Command::SendCommand("stop\nend\n".to_owned())) - .expect("tx send err"); - tx.blocking_send(Command::WaitUntilExit) - .expect("tx send err"); - tx.blocking_send(Command::Start) - .expect("tx send err"); - } - HotReloadAction::RunCommand(cmd) => { - tx.blocking_send(Command::SendCommand(format!("{cmd}\n"))) - .expect("tx send err"); - } + match &file.action { + HotReloadAction::Reload => { + tx.blocking_send(Command::SendCommand("reload confirm\n".to_owned())) + .expect("tx send err"); + } + HotReloadAction::Restart => { + tx.blocking_send(Command::SendCommand("stop\nend\n".to_owned())) + .expect("tx send err"); + tx.blocking_send(Command::WaitUntilExit) + .expect("tx send err"); + tx.blocking_send(Command::Start) + .expect("tx send err"); + } + HotReloadAction::RunCommand(cmd) => { + tx.blocking_send(Command::SendCommand(format!("{cmd}\n"))) + .expect("tx send err"); } } } @@ -290,13 +271,9 @@ impl<'a> DevSession<'a> { } pub fn create_servertoml_watcher(tx: mpsc::Sender) -> Result> { - Ok(new_debouncer(Duration::from_secs(1), move |e| { + Ok(new_debouncer(Duration::from_secs(1), move |e: DebounceEventResult| { if let Ok(e) = e { - for e in e { - if !matches!(e.kind, EventKind::Modify(_)) { - continue; - }; - + for _e in e { tx.blocking_send(Command::SendCommand("stop\nend".to_owned())) .expect("tx send err"); tx.blocking_send(Command::WaitUntilExit)