Skip to content

Commit 9bc72db

Browse files
committed
Batch file changes in dev
1 parent 4993d5d commit 9bc72db

File tree

1 file changed

+74
-58
lines changed

1 file changed

+74
-58
lines changed

combine/dev.py

+74-58
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
from repaint import Repaint
1919

2020

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+
2130
class Watcher:
2231
def __init__(
2332
self,
@@ -28,22 +37,68 @@ def __init__(
2837
) -> None:
2938
self.path = path
3039
self.debug = debug
31-
self.event_handler = EventHandler(combine, repaint)
40+
self.combine = combine
41+
self.repaint = repaint
3242

3343
def watch(self) -> None:
3444
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+
]
3748

49+
reload = False
50+
rebuild = False
51+
rebuild_paths = []
52+
rebuild_all_paths = False
3853

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)
47102

48103
def should_ignore_path(self, path: str) -> bool:
49104
# Most of these are filtered already by watch
@@ -66,7 +121,7 @@ def should_ignore_path(self, path: str) -> bool:
66121

67122
return False
68123

69-
def on_any_event(self, change: Change, path: str) -> None:
124+
def process_change(self, change: Change, path: str) -> ChangeResult | None:
70125
logger.debug("Event: %s %s", change.name, path)
71126

72127
if self.should_ignore_path(path):
@@ -75,7 +130,7 @@ def on_any_event(self, change: Change, path: str) -> None:
75130

76131
if self.combine.is_in_output_path(path):
77132
_, ext = os.path.splitext(path)
78-
if ext in (".css", ".img", ".js") and self.repaint:
133+
if ext in (".css", ".js") and self.repaint:
79134
output_relative_path = os.path.relpath(path, self.combine.output_path)
80135
logger.debug("Repainting output path: %s", output_relative_path)
81136
self.repaint.reload_assets([output_relative_path])
@@ -100,36 +155,24 @@ def on_any_event(self, change: Change, path: str) -> None:
100155
)
101156

102157
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)
111159

112160
content_relative_path = self.combine.content_relative_path(
113161
os.path.abspath(path)
114162
)
115163

116164
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-
122165
if change == Change.deleted:
123166
click.secho(
124167
f"{content_relative_path} {change.name}: ",
125-
nl=False,
126168
bold=True,
127169
color=True,
128170
)
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
130175
self.reload_combine()
131-
self.rebuild_site()
132-
return
133176

134177
files = self.combine.get_related_files(content_relative_path)
135178

@@ -138,38 +181,11 @@ def on_any_event(self, change: Change, path: str) -> None:
138181

139182
click.secho(
140183
f"{content_relative_path} {change.name}: ",
141-
nl=False,
142184
bold=True,
143185
color=True,
144186
)
145187

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])
173189

174190

175191
class Server:

0 commit comments

Comments
 (0)