Skip to content

Commit 9b51868

Browse files
authored
Run clean on reload when both are specified for serve. (#33)
1 parent fd1b23e commit 9b51868

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

src/render_engine_cli/cli.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
)
2828

2929
MODULE_SITE_HELP = "The module (python file) and site (the Site object) for your site in the format module:site"
30+
console = Console()
3031

3132
try:
3233
# Get the RE version for display. If it's not set it means we're working locally.
@@ -140,7 +141,7 @@ def build(module_site: str, clean: bool):
140141
module, site_name = split_module_site(module_site)
141142
site = get_site(module, site_name)
142143
if clean:
143-
remove_output_folder(Path(site.output_path))
144+
remove_output_folder(Path(site.output_path), console=console)
144145
site.render()
145146

146147

@@ -154,7 +155,7 @@ def build(module_site: str, clean: bool):
154155
@click.option(
155156
"-c",
156157
"--clean",
157-
help="Clean the output folder prior to building.",
158+
help="Clean the output folder prior to building. If `--reload` is set this will clean on each reload.",
158159
is_flag=True,
159160
default=False,
160161
)
@@ -165,7 +166,13 @@ def build(module_site: str, clean: bool):
165166
is_flag=True,
166167
default=False,
167168
)
168-
@click.option("-p", "--port", type=click.IntRange(0, 65534), help="Port to serve on", default=8000.0)
169+
@click.option(
170+
"-p",
171+
"--port",
172+
type=click.IntRange(0, 65534),
173+
help="Port to serve on",
174+
default=8000.0,
175+
)
169176
def serve(module_site: str, clean: bool, reload: bool, port: int):
170177
"""
171178
Create an HTTP server to serve the site at `localhost`.
@@ -186,7 +193,7 @@ def serve(module_site: str, clean: bool, reload: bool, port: int):
186193
site = get_site(module, site_name)
187194

188195
if clean:
189-
remove_output_folder(Path(site.output_path))
196+
remove_output_folder(Path(site.output_path), console=console)
190197
site.render()
191198

192199
server_address = ("127.0.0.1", port)
@@ -199,6 +206,7 @@ def serve(module_site: str, clean: bool, reload: bool, port: int):
199206
output_path=site.output_path,
200207
patterns=None,
201208
ignore_patterns=[r".*output\\*.+$", r"\.\\\..+$", r".*__.*$"],
209+
clean=clean,
202210
)
203211

204212
with handler:
@@ -378,7 +386,6 @@ def templates(module_site: str, theme_name: str, filter_value: str):
378386
"""
379387
module, site_name = split_module_site(module_site)
380388
site = get_site(module, site_name)
381-
console = Console()
382389

383390
if theme_name:
384391
available_themes = get_available_themes(console, site, theme_name)

src/render_engine_cli/event.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
import time
33
import traceback
44
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
5+
from pathlib import Path
56

67
import watchfiles
78
from rich.console import Console
89

9-
from render_engine_cli.utils import get_site
10-
11-
console = Console()
10+
from render_engine_cli.utils import get_site, remove_output_folder
1211

1312

1413
def spawn_server(server_address: tuple[str, int], directory: str) -> ThreadingHTTPServer:
@@ -59,6 +58,7 @@ def __init__(
5958
dirs_to_watch: str | None = None,
6059
patterns: list[str] | None = None,
6160
ignore_patterns: list[str] | None = None,
61+
clean: bool = False,
6262
*args,
6363
**kwargs,
6464
) -> None:
@@ -70,29 +70,33 @@ def __init__(
7070
self.dirs_to_watch = dirs_to_watch
7171
self.patterns = patterns
7272
self.ignore_patterns = ignore_patterns
73+
self.clean = clean
74+
self.console = Console()
7375

7476
def start_server(self) -> None:
7577
if not getattr(self, "server", False):
76-
console.print(
78+
self.console.print(
7779
f"[bold green]Spawning server on http://{self.server_address[0]}:{self.server_address[1]}[/bold green]"
7880
)
7981
self.server = spawn_server(self.server_address, self.output_path)
8082
self._thread = threading.Thread(target=self.server.serve_forever)
8183
self._thread.start()
8284

8385
def stop_server(self) -> None:
84-
console.print("[bold red]Stopping server[/bold red]")
86+
self.console.print("[bold red]Stopping server[/bold red]")
8587
self.server.shutdown()
8688
self._thread.join()
8789

8890
def rebuild(self) -> None:
89-
console.print("[bold purple]Reloading and Rebuilding site...[/bold purple]")
91+
self.console.print("[bold purple]Reloading and Rebuilding site...[/bold purple]")
9092
site = get_site(self.import_path, self.site, reload=True)
93+
if self.clean:
94+
remove_output_folder(Path(site.output_path), console=self.console)
9195
try:
9296
site.render()
9397
except Exception:
94-
console.print("[bold red]Failed to render site[/bold red]")
95-
console.print(traceback.format_exc())
98+
self.console.print("[bold red]Failed to render site[/bold red]")
99+
self.console.print(traceback.format_exc())
96100
pass
97101

98102
def stop_watcher(self) -> bool:
@@ -122,7 +126,7 @@ def watch(self) -> None:
122126
If a KeyboardInterrupt is raised, it stops the observer and server.
123127
"""
124128

125-
console.print(f"[yellow]Serving {self.output_path}[/yellow]")
129+
self.console.print(f"[yellow]Serving {self.output_path}[/yellow]")
126130
while not self.stop_watcher():
127131
try:
128132
if self.dirs_to_watch:
@@ -144,5 +148,5 @@ def __exit__(self, exc_type, exc_value, traceback) -> None:
144148
"""Stopping Context manager for the class"""
145149

146150
self.stop_server()
147-
console.print("[bold red]FIN![/bold red]")
151+
self.console.print("[bold red]FIN![/bold red]")
148152
return None

src/render_engine_cli/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ def get_site_content_paths(site: Site) -> list[Path | str | None]:
102102
return list(filter(None, base_paths))
103103

104104

105-
def remove_output_folder(output_path: Path) -> None:
105+
def remove_output_folder(output_path: Path, console: Console = None) -> None:
106106
"""Remove the output folder"""
107107

108+
if console:
109+
console.print(f"[bold yellow]Removing exisiting rendered content from {str(output_path)}")
108110
# TODO: #778 Should we check for Operating System
109111
if output_path.exists():
110112
shutil.rmtree(output_path)

0 commit comments

Comments
 (0)