From 64d9e04c1bb23f80de653d807791bdc4465214bb Mon Sep 17 00:00:00 2001 From: Wodann Date: Fri, 4 Oct 2019 10:46:46 +0200 Subject: [PATCH] feat(compiler_daemon): add compiler daemon that detects changed files and recompiles them --- crates/mun_compiler_daemon/Cargo.toml | 10 ++++++++ crates/mun_compiler_daemon/src/lib.rs | 34 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 crates/mun_compiler_daemon/Cargo.toml create mode 100644 crates/mun_compiler_daemon/src/lib.rs diff --git a/crates/mun_compiler_daemon/Cargo.toml b/crates/mun_compiler_daemon/Cargo.toml new file mode 100644 index 000000000..a1ff3a87b --- /dev/null +++ b/crates/mun_compiler_daemon/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "mun_compiler_daemon" +version = "0.1.0" +authors = ["Wodann "] +edition = "2018" + +[dependencies] +failure = "0.1.5" +mun_compiler = { path = "../mun_compiler" } +notify = "4.0.12" diff --git a/crates/mun_compiler_daemon/src/lib.rs b/crates/mun_compiler_daemon/src/lib.rs new file mode 100644 index 000000000..019baa851 --- /dev/null +++ b/crates/mun_compiler_daemon/src/lib.rs @@ -0,0 +1,34 @@ +use std::sync::mpsc::channel; +use std::time::Duration; + +use failure::Error; +use mun_compiler::CompilerOptions; +use notify::{RecommendedWatcher, RecursiveMode, Watcher}; + +pub fn main(options: &CompilerOptions) -> Result<(), Error> { + // Need to canonicalize path to do comparisons + let input_path = options.input.canonicalize()?; + + // Compile at least once + mun_compiler::main(&options)?; + + let (tx, rx) = channel(); + + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(10))?; + watcher.watch(&input_path, RecursiveMode::NonRecursive)?; + + loop { + use notify::DebouncedEvent::*; + match rx.recv() { + Ok(Write(ref path)) => { + // TODO: Check whether file contents changed (using sha hash?) + if *path == input_path { + mun_compiler::main(&options)?; + println!("Compiled: {}", path.to_string_lossy()); + } + } + Ok(_) => (), + Err(e) => eprintln!("Watcher error: {:?}", e), + } + } +}