Watches the module for changes using [`notify`](https://crates.io/crates/notify), and rebuild it upon changes Returns the result of the first successful compilation, then calls `on_compilation_finishes` for each subsequent compilation.
(
mut self,
mut on_compilation_finishes: impl FnMut(CompileResult) + Send + 'static,
)
| 12 | /// Returns the result of the first successful compilation, then calls |
| 13 | /// `on_compilation_finishes` for each subsequent compilation. |
| 14 | pub fn watch( |
| 15 | mut self, |
| 16 | mut on_compilation_finishes: impl FnMut(CompileResult) + Send + 'static, |
| 17 | ) -> Result<CompileResult, SpirvBuilderError> { |
| 18 | self.validate_running_conditions()?; |
| 19 | if !matches!(self.print_metadata, crate::MetadataPrintout::None) { |
| 20 | return Err(SpirvBuilderError::WatchWithPrintMetadata); |
| 21 | } |
| 22 | let metadata_result = crate::invoke_rustc(&self); |
| 23 | // Load the dependencies of the thing |
| 24 | let metadata_file = if let Ok(path) = metadata_result { |
| 25 | path |
| 26 | } else { |
| 27 | let (tx, rx) = sync_channel(0); |
| 28 | // Fall back to watching from the crate root if the inital compilation fails |
| 29 | let mut watcher = |
| 30 | notify::recommended_watcher(move |event: notify::Result<Event>| match event { |
| 31 | Ok(e) => match e.kind { |
| 32 | notify::EventKind::Access(_) => (), |
| 33 | notify::EventKind::Any |
| 34 | | notify::EventKind::Create(_) |
| 35 | | notify::EventKind::Modify(_) |
| 36 | | notify::EventKind::Remove(_) |
| 37 | | notify::EventKind::Other => { |
| 38 | let _ = tx.try_send(()); |
| 39 | } |
| 40 | }, |
| 41 | Err(e) => println!("notify error: {e:?}"), |
| 42 | }) |
| 43 | .expect("Could create watcher"); |
| 44 | // This is likely to notice changes in the `target` dir, however, given that `cargo watch` doesn't seem to handle that, |
| 45 | watcher |
| 46 | .watch(&self.path_to_crate, RecursiveMode::Recursive) |
| 47 | .expect("Could watch crate root"); |
| 48 | loop { |
| 49 | rx.recv().expect("Watcher still alive"); |
| 50 | let metadata_file = crate::invoke_rustc(&self); |
| 51 | if let Ok(f) = metadata_file { |
| 52 | break f; |
| 53 | } |
| 54 | } |
| 55 | }; |
| 56 | let metadata = self.parse_metadata_file(&metadata_file)?; |
| 57 | let first_result = metadata; |
| 58 | |
| 59 | let thread = std::thread::spawn(move || { |
| 60 | let mut watched_paths = HashSet::new(); |
| 61 | let (tx, rx) = sync_channel(0); |
| 62 | let mut watcher = |
| 63 | notify::recommended_watcher(move |event: notify::Result<Event>| match event { |
| 64 | Ok(e) => match e.kind { |
| 65 | notify::EventKind::Access(_) => (), |
| 66 | notify::EventKind::Any |
| 67 | | notify::EventKind::Create(_) |
| 68 | | notify::EventKind::Modify(_) |
| 69 | | notify::EventKind::Remove(_) |
| 70 | | notify::EventKind::Other => { |
| 71 | let _ = tx.try_send(()); |
no test coverage detected