@@ -3,6 +3,7 @@ use std::ops::Deref;
33use std:: process:: { Command , Stdio } ;
44use std:: str:: FromStr ;
55use std:: sync:: Arc ;
6+ use std:: time:: { Duration , Instant } ;
67
78use anyhow:: Context ;
89use camino:: { Utf8Path , Utf8PathBuf } ;
@@ -40,6 +41,34 @@ pub struct BndBuilderApp {
4041 observers : Arc < ListOfBndBuilderObserverRc >
4142}
4243
44+ #[ derive( Debug , Clone ) ]
45+ pub enum WatchState {
46+ NoWatch ,
47+ WatchFirstRound ,
48+ WatchNextRounds { last_build : Instant } ,
49+ }
50+
51+ impl WatchState {
52+ pub fn request_watch ( & self ) -> bool {
53+ !matches ! ( self , WatchState :: NoWatch )
54+ }
55+
56+ pub fn disable_phony ( & self ) -> bool {
57+ match self {
58+ Self :: NoWatch => false ,
59+ Self :: WatchFirstRound => false ,
60+ Self :: WatchNextRounds { ..} => true
61+ }
62+ }
63+
64+ pub fn last_build ( & self ) -> Option < & Instant > {
65+ match self {
66+ Self :: WatchNextRounds { last_build} => Some ( last_build) ,
67+ _ => None
68+ }
69+ }
70+ }
71+
4372#[ derive( Debug ) ]
4473pub enum BndBuilderCommandInner {
4574 /// Print the help of the given command
@@ -68,7 +97,7 @@ pub enum BndBuilderCommandInner {
6897 /// Build the corresponding targets
6998 Build {
7099 targets : Option < Vec < Utf8PathBuf > > ,
71- watch : bool ,
100+ watch : WatchState ,
72101 current_step : usize ,
73102 builder : BndBuilder
74103 } ,
@@ -202,11 +231,12 @@ impl BndBuilderCommand {
202231 /// TODO wire parallal execution
203232 fn execute_build (
204233 init_targets : Option < Vec < Utf8PathBuf > > ,
205- watch : bool ,
234+ watch : WatchState ,
206235 mut current_step : usize ,
207236 builder : BndBuilder ,
208237 observers : Arc < ListOfBndBuilderObserverRc >
209238 ) -> Result < Option < Self > , BndBuilderError > {
239+
210240 let targets_provided = init_targets. is_some ( ) ;
211241
212242 // get the list of targets
@@ -227,7 +257,7 @@ impl BndBuilderCommand {
227257 let tgt = & targets[ current_step] ;
228258
229259 // execute if needed
230- if builder. outdated ( tgt) ? {
260+ let last_build = if builder. outdated ( & watch , tgt) ? {
231261 builder. execute ( tgt) . map_err ( |e| {
232262 if targets_provided {
233263 e
@@ -238,7 +268,10 @@ impl BndBuilderCommand {
238268 }
239269 }
240270 } ) ?;
241- }
271+ Some ( Instant :: now ( ) )
272+ } else {
273+ None
274+ } ;
242275
243276 // set up the next step if any
244277 let over = init_targets. as_ref ( ) . map ( |v| v. len ( ) - 1 ) . unwrap_or ( 0 ) == current_step;
@@ -250,14 +283,15 @@ impl BndBuilderCommand {
250283 current_step += 1 ;
251284 }
252285
253- if over && !watch {
286+ if over && !watch. request_watch ( ) {
254287 Ok ( None )
255288 }
256289 else {
290+ std:: thread:: sleep ( Duration :: from_millis ( 2000 ) ) ; // duration to wait
257291 Ok ( Some ( BndBuilderCommand {
258292 inner : BndBuilderCommandInner :: Build {
259293 targets : init_targets,
260- watch,
294+ watch : WatchState :: WatchNextRounds { last_build : last_build . unwrap_or_else ( || watch . last_build ( ) . cloned ( ) . unwrap ( ) ) } ,
261295 current_step,
262296 builder
263297 } ,
@@ -834,7 +868,7 @@ impl BndBuilderApp {
834868
835869 Ok ( BndBuilderCommandInner :: Build {
836870 targets,
837- watch : watch_requested,
871+ watch : if watch_requested { WatchState :: WatchFirstRound } else { WatchState :: NoWatch } ,
838872 current_step : 0 ,
839873 builder
840874 } )
0 commit comments