@@ -3,13 +3,11 @@ use ctrlc;
33use notify;
44use shell_grunt2;
55use time;
6- #[ macro_use]
7- extern crate self_update;
86
97use notify:: Watcher ;
108use shell_grunt2:: lockfile;
119use shell_grunt2:: task:: { Runnable , RunningTask , Task } ;
12- use std:: path;
10+ use std:: path:: { Path , PathBuf } ;
1311use std:: process;
1412use std:: sync:: atomic:: { AtomicBool , Ordering } ;
1513use std:: sync:: mpsc;
@@ -27,14 +25,14 @@ fn update() -> Result<(), Box<dyn (::std::error::Error)>> {
2725 . show_download_progress ( true )
2826 . show_output ( false )
2927 . no_confirm ( true )
30- . current_version ( cargo_crate_version ! ( ) )
28+ . current_version ( self_update :: cargo_crate_version!( ) )
3129 . build ( ) ?
3230 . update ( ) ?;
3331 Ok ( ( ) )
3432}
3533
3634struct ReloadWatcherFile {
37- file_name : String ,
35+ file_name : PathBuf ,
3836 should_reload : Arc < AtomicBool > ,
3937}
4038
@@ -56,44 +54,51 @@ impl Runnable for ReloadWatcherFile {
5654}
5755
5856impl Task for ReloadWatcherFile {
59- fn should_run ( & self , path : & path:: Path ) -> bool {
60- if let Some ( file_name) = path. file_name ( ) {
61- return file_name. to_string_lossy ( ) == self . file_name ;
62- }
63- false
57+ fn should_run ( & self , path : & Path ) -> bool {
58+ path == self . file_name
6459 }
6560
6661 fn start_delay ( & self ) -> time:: Duration {
6762 time:: Duration :: milliseconds ( 0 )
6863 }
6964}
7065
71- fn watch_file_events ( watcher_file : & str ) {
66+ fn watch_file_events ( watcher_file : impl AsRef < Path > ) {
7267 let saw_interrupt_signal = Arc :: new ( AtomicBool :: new ( false ) ) ;
7368 let r = saw_interrupt_signal. clone ( ) ;
7469 ctrlc:: set_handler ( move || {
7570 r. store ( true , Ordering :: SeqCst ) ;
7671 } )
7772 . expect ( "Error setting Ctrl-C handler" ) ;
7873
74+ let current_dir = std:: fs:: canonicalize ( "." ) . unwrap ( ) ;
75+ let watcher_file = std:: fs:: canonicalize ( watcher_file. as_ref ( ) ) . unwrap ( ) ;
7976 loop {
80- println ! ( "Watching file system with tasks from {}" , watcher_file) ;
77+ let diff = pathdiff:: diff_paths ( & watcher_file, & current_dir) ;
78+ let disp = match & diff {
79+ Some ( diff) => diff,
80+ None => & watcher_file,
81+ } ;
82+ println ! ( "Watching file system with tasks from {}" , disp. display( ) ) ;
8183
8284 // Ideally, the RecommendedWatcher would be owned by ShellGrunt2, but whenever I try that,
8385 // the tool crashes whenever it should receive an event on the channel. So it needs to stay
8486 // outside. :(
8587 let ( events_tx, events_rx) = mpsc:: channel ( ) ;
8688 let mut watcher = notify:: watcher ( events_tx, Duration :: from_millis ( 50 ) ) . unwrap ( ) ;
8789 watcher
88- . watch ( & path:: Path :: new ( "." ) , notify:: RecursiveMode :: Recursive )
90+ . watch ( & current_dir, notify:: RecursiveMode :: Recursive )
91+ . unwrap ( ) ;
92+ watcher
93+ . watch ( & watcher_file, notify:: RecursiveMode :: Recursive )
8994 . unwrap ( ) ;
9095
9196 let should_reload = Arc :: new ( AtomicBool :: new ( false ) ) ;
9297 let mut tasks: Vec < Box < dyn Task > > = vec ! [ Box :: new( ReloadWatcherFile {
93- file_name: watcher_file. to_string ( ) ,
98+ file_name: watcher_file. clone ( ) ,
9499 should_reload: should_reload. clone( ) ,
95100 } ) ] ;
96- for task in shell_grunt2:: lua_task:: run_file ( path :: Path :: new ( watcher_file) ) {
101+ for task in shell_grunt2:: lua_task:: run_file ( & watcher_file) {
97102 tasks. push ( task) ;
98103 }
99104 let mut shell_grunt2 = shell_grunt2:: ShellGrunt2 :: new ( & tasks, events_rx) ;
@@ -113,7 +118,7 @@ fn watch_file_events(watcher_file: &str) {
113118
114119fn main ( ) {
115120 let matches = clap:: App :: new ( "shell_grunt2" )
116- . version ( cargo_crate_version ! ( ) )
121+ . version ( self_update :: cargo_crate_version!( ) )
117122 . about ( "Watches the file system and executes commands from a Lua file." )
118123 . arg (
119124 clap:: Arg :: with_name ( "file" )
0 commit comments