18
18
from repaint import Repaint
19
19
20
20
21
+ class ChangeResult :
22
+ def __init__ (
23
+ self , * , reload : bool = False , rebuild : bool = False , rebuild_paths : list = []
24
+ ) -> None :
25
+ self .reload = reload
26
+ self .rebuild = rebuild
27
+ self .rebuild_paths = rebuild_paths
28
+
29
+
21
30
class Watcher :
22
31
def __init__ (
23
32
self ,
@@ -28,22 +37,68 @@ def __init__(
28
37
) -> None :
29
38
self .path = path
30
39
self .debug = debug
31
- self .event_handler = EventHandler (combine , repaint )
40
+ self .combine = combine
41
+ self .repaint = repaint
32
42
33
43
def watch (self ) -> None :
34
44
for changes in watch (self .path , recursive = True , debug = self .debug ):
35
- for change , path in changes :
36
- self .event_handler .on_any_event (change , path )
45
+ change_results = [
46
+ self .process_change (change , path ) for change , path in changes
47
+ ]
37
48
49
+ reload = False
50
+ rebuild = False
51
+ rebuild_paths = []
52
+ rebuild_all_paths = False
38
53
39
- class EventHandler :
40
- def __init__ (
41
- self ,
42
- combine : "Combine" ,
43
- repaint : Optional ["Repaint" ],
44
- ) -> None :
45
- self .combine = combine
46
- self .repaint = repaint
54
+ for change_result in change_results :
55
+ if not change_result :
56
+ continue
57
+
58
+ if change_result .reload :
59
+ reload = True
60
+
61
+ if change_result .rebuild :
62
+ rebuild = True
63
+
64
+ if change_result .rebuild_paths :
65
+ rebuild_paths .extend (change_result .rebuild_paths )
66
+ else :
67
+ rebuild_all_paths = True
68
+
69
+ if reload :
70
+ self .reload_combine ()
71
+
72
+ if rebuild :
73
+ self .rebuild_site ([] if rebuild_all_paths else list (set (rebuild_paths )))
74
+
75
+ def reload_combine (self ) -> None :
76
+ click .secho ("--> Reloading combine" , bold = True , color = True )
77
+ try :
78
+ self .combine .reload ()
79
+ except Exception as e :
80
+ logger .error ("Error reloading" , exc_info = e )
81
+ click .secho ("There was an error! See output above." , fg = "red" , color = True )
82
+
83
+ def rebuild_site (self , only_paths : List [str ] = []) -> None :
84
+ if len (only_paths ) == 1 :
85
+ click .secho (f"--> Rebuilding { only_paths [0 ]} " , bold = True , color = True )
86
+ elif len (only_paths ) > 1 :
87
+ click .secho (
88
+ f"--> Rebuilding { len (only_paths )} paths" , bold = True , color = True
89
+ )
90
+ else :
91
+ click .secho ("--> Rebuilding entire site" , bold = True , color = True )
92
+
93
+ try :
94
+ self .combine .build (only_paths )
95
+ if self .repaint :
96
+ self .repaint .reload ()
97
+ except BuildError :
98
+ click .secho ("Build error (see above)" , fg = "red" , color = True )
99
+ except Exception as e :
100
+ logger .error ("Error building" , exc_info = e )
101
+ click .secho ("There was an error! See output above." , fg = "red" , color = True )
47
102
48
103
def should_ignore_path (self , path : str ) -> bool :
49
104
# Most of these are filtered already by watch
@@ -66,7 +121,7 @@ def should_ignore_path(self, path: str) -> bool:
66
121
67
122
return False
68
123
69
- def on_any_event (self , change : Change , path : str ) -> None :
124
+ def process_change (self , change : Change , path : str ) -> ChangeResult | None :
70
125
logger .debug ("Event: %s %s" , change .name , path )
71
126
72
127
if self .should_ignore_path (path ):
@@ -75,7 +130,7 @@ def on_any_event(self, change: Change, path: str) -> None:
75
130
76
131
if self .combine .is_in_output_path (path ):
77
132
_ , ext = os .path .splitext (path )
78
- if ext in (".css" , ".img" , ". js" ) and self .repaint :
133
+ if ext in (".css" , ".js" ) and self .repaint :
79
134
output_relative_path = os .path .relpath (path , self .combine .output_path )
80
135
logger .debug ("Repainting output path: %s" , output_relative_path )
81
136
self .repaint .reload_assets ([output_relative_path ])
@@ -100,36 +155,24 @@ def on_any_event(self, change: Change, path: str) -> None:
100
155
)
101
156
102
157
if os .path .abspath (path ) == os .path .abspath (self .combine .config_path ):
103
- click .secho (
104
- f"{ self .combine .config_path } { change .name } : reloading combine and rebuilding site" ,
105
- bold = True ,
106
- color = True ,
107
- )
108
- self .reload_combine ()
109
- self .rebuild_site ()
110
- return
158
+ return ChangeResult (reload = True , rebuild = True )
111
159
112
160
content_relative_path = self .combine .content_relative_path (
113
161
os .path .abspath (path )
114
162
)
115
163
116
164
if content_relative_path :
117
-
118
- if change in (Change .added , Change .modified ):
119
- # Reload first, so we know about any new files
120
- self .reload_combine ()
121
-
122
165
if change == Change .deleted :
123
166
click .secho (
124
167
f"{ content_relative_path } { change .name } : " ,
125
- nl = False ,
126
168
bold = True ,
127
169
color = True ,
128
170
)
129
- click .echo ("Rebuilding entire site" )
171
+ return ChangeResult (reload = True , rebuild = True )
172
+
173
+ if change in (Change .added , Change .modified ):
174
+ # Reload first, so we know about any new files
130
175
self .reload_combine ()
131
- self .rebuild_site ()
132
- return
133
176
134
177
files = self .combine .get_related_files (content_relative_path )
135
178
@@ -138,38 +181,11 @@ def on_any_event(self, change: Change, path: str) -> None:
138
181
139
182
click .secho (
140
183
f"{ content_relative_path } { change .name } : " ,
141
- nl = False ,
142
184
bold = True ,
143
185
color = True ,
144
186
)
145
187
146
- if len (files ) == 1 :
147
- click .echo (f"Rebuilding { files [0 ].content_relative_path } " )
148
- elif not files :
149
- click .echo ("Rebuliding entire site" )
150
- else :
151
- click .echo (f"Rebuilding { len (files )} files" )
152
- self .rebuild_site (only_paths = [x .path for x in files ])
153
-
154
- def reload_combine (self ) -> None :
155
- try :
156
- self .combine .reload ()
157
- if self .repaint :
158
- self .repaint .reload ()
159
- except Exception as e :
160
- logger .error ("Error reloading" , exc_info = e )
161
- click .secho ("There was an error! See output above." , fg = "red" , color = True )
162
-
163
- def rebuild_site (self , only_paths : List [str ] = []) -> None :
164
- try :
165
- self .combine .build (only_paths )
166
- if self .repaint :
167
- self .repaint .reload ()
168
- except BuildError :
169
- click .secho ("Build error (see above)" , fg = "red" , color = True )
170
- except Exception as e :
171
- logger .error ("Error building" , exc_info = e )
172
- click .secho ("There was an error! See output above." , fg = "red" , color = True )
188
+ return ChangeResult (rebuild = True , rebuild_paths = [x .path for x in files ])
173
189
174
190
175
191
class Server :
0 commit comments