1
+ use crate :: build;
2
+ use crate :: helpers;
1
3
use futures:: {
2
4
channel:: mpsc:: { channel, Receiver } ,
3
5
SinkExt , StreamExt ,
4
6
} ;
5
7
use notify:: { Config , Event , RecommendedWatcher , RecursiveMode , Watcher } ;
6
8
use std:: path:: Path ;
9
+ pub static FILE_TYPES : & [ & str ] = & [ "re" , "res" , "ml" , "rei" , "resi" , "mli" ] ;
7
10
8
11
fn async_watcher ( ) -> notify:: Result < ( RecommendedWatcher , Receiver < notify:: Result < Event > > ) > {
9
12
let ( mut tx, rx) = channel ( 1 ) ;
@@ -22,17 +25,32 @@ fn async_watcher() -> notify::Result<(RecommendedWatcher, Receiver<notify::Resul
22
25
Ok ( ( watcher, rx) )
23
26
}
24
27
25
- async fn async_watch < P : AsRef < Path > > ( path : P ) -> notify:: Result < ( ) > {
28
+ async fn async_watch ( path : String ) -> notify:: Result < ( ) > {
26
29
let ( mut watcher, mut rx) = async_watcher ( ) ?;
27
30
28
31
// Add a path to be watched. All files and directories at that path and
29
32
// below will be monitored for changes.
30
33
watcher. watch ( path. as_ref ( ) , RecursiveMode :: Recursive ) ?;
31
34
32
35
while let Some ( res) = rx. next ( ) . await {
33
- match res {
34
- Ok ( event) => println ! ( "changed: {:?}" , event) ,
35
- Err ( e) => println ! ( "watch error: {:?}" , e) ,
36
+ let files_to_compile = res
37
+ . iter ( )
38
+ . map ( |event| {
39
+ event
40
+ . paths
41
+ . iter ( )
42
+ . map ( |path| path. to_path_buf ( ) )
43
+ . filter ( |path| helpers:: string_ends_with_any ( path, FILE_TYPES ) )
44
+ . collect :: < Vec < PathBuf > > ( )
45
+ } )
46
+ . flatten ( )
47
+ . into_iter ( )
48
+ . collect :: < Vec < PathBuf > > ( ) ;
49
+
50
+ let delay = Duration :: from_millis ( 10 ) ;
51
+ if files_to_compile. len ( ) > 0 {
52
+ thread:: sleep ( delay) ;
53
+ build:: build ( & path) ;
36
54
}
37
55
}
38
56
@@ -41,7 +59,7 @@ async fn async_watch<P: AsRef<Path>>(path: P) -> notify::Result<()> {
41
59
42
60
pub fn start ( folder : & str ) {
43
61
futures:: executor:: block_on ( async {
44
- if let Err ( e) = async_watch ( folder) . await {
62
+ if let Err ( e) = async_watch ( folder. to_string ( ) ) . await {
45
63
println ! ( "error: {:?}" , e)
46
64
}
47
65
} ) ;
0 commit comments