Skip to content

Commit

Permalink
fix hotreload
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Nov 8, 2023
1 parent 3753f75 commit 1426b80
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 73 deletions.
3 changes: 0 additions & 3 deletions src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
117 changes: 47 additions & 70 deletions src/hot_reload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,9 +15,6 @@ pub mod pattern_serde;

#[derive(Debug)]
pub struct DevSession<'a> {
pub child: Option<tokio::process::Child>,
pub command_sender: Option<mpsc::Sender<Command>>,
pub command_reciever: Option<mpsc::Receiver<Command>>,
pub builder: BuildContext<'a>,
pub jar_name: Option<String>,
}
Expand All @@ -31,13 +28,6 @@ pub enum Command {
Bootstrap(PathBuf),
}

pub enum State {
Starting,
Stopping,
Building,
Online,
}

async fn try_read_line(opt: &mut Option<tokio::io::Lines<BufReader<tokio::process::ChildStdout>>>) -> Result<Option<String>> {
match opt {
Some(lines) => Ok(lines.next_line().await?),
Expand Down Expand Up @@ -78,6 +68,7 @@ impl<'a> DevSession<'a> {
)
}

#[allow(unused_assignments)]
async fn handle_commands(mut self, mut rx: mpsc::Receiver<Command>) -> Result<()> {
let mp = self.builder.app.multi_progress.clone();

Expand Down Expand Up @@ -213,26 +204,20 @@ impl<'a> DevSession<'a> {

pub fn create_hotreload_watcher(
config: Arc<Mutex<HotReloadConfig>>,
tx: mpsc::Sender<Command>,
_tx: mpsc::Sender<Command>,

Check warning on line 207 in src/hot_reload/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this argument is passed by value, but not consumed in the function body

warning: this argument is passed by value, but not consumed in the function body --> src/hot_reload/mod.rs:207:14 | 207 | _tx: mpsc::Sender<Command>, | ^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&mpsc::Sender<Command>` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
) -> Result<Debouncer<RecommendedWatcher>> {
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");
}
}
}
Expand All @@ -243,45 +228,41 @@ impl<'a> DevSession<'a> {
config: Arc<Mutex<HotReloadConfig>>,
tx: mpsc::Sender<Command>,
) -> Result<Debouncer<RecommendedWatcher>> {
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");
}
}
}
Expand All @@ -290,13 +271,9 @@ impl<'a> DevSession<'a> {
}

pub fn create_servertoml_watcher(tx: mpsc::Sender<Command>) -> Result<Debouncer<RecommendedWatcher>> {
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)
Expand Down

0 comments on commit 1426b80

Please sign in to comment.