Skip to content

Commit

Permalink
feat(compiler_daemon): add compiler daemon that detects changed files…
Browse files Browse the repository at this point in the history
… and recompiles them
  • Loading branch information
Wodann committed Oct 6, 2019
1 parent 149b19b commit 64d9e04
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/mun_compiler_daemon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "mun_compiler_daemon"
version = "0.1.0"
authors = ["Wodann <[email protected]>"]
edition = "2018"

[dependencies]
failure = "0.1.5"
mun_compiler = { path = "../mun_compiler" }
notify = "4.0.12"
34 changes: 34 additions & 0 deletions crates/mun_compiler_daemon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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),
}
}
}

0 comments on commit 64d9e04

Please sign in to comment.