Skip to content

Commit 034ee69

Browse files
committed
Use entrypoints for dev and compile tailwind
1 parent 772a5bf commit 034ee69

File tree

5 files changed

+47
-26
lines changed

5 files changed

+47
-26
lines changed

plain-dev/plain/dev/cli.py

+28-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import platform
44
import subprocess
55
import sys
6+
from importlib.metadata import entry_points
67
from importlib.util import find_spec
78
from pathlib import Path
89

@@ -16,7 +17,9 @@
1617
from .pid import Pid
1718
from .poncho.manager import Manager as PonchoManager
1819
from .services import Services
19-
from .utils import has_pyproject_toml, plainpackage_installed
20+
from .utils import has_pyproject_toml
21+
22+
ENTRYPOINT_GROUP = "plain.dev"
2023

2124

2225
@click.group(invoke_without_command=True)
@@ -46,6 +49,24 @@ def services():
4649
Services().run()
4750

4851

52+
@cli.command()
53+
@click.option(
54+
"--list", "-l", "show_list", is_flag=True, help="List available entrypoints"
55+
)
56+
@click.argument("entrypoint", required=False)
57+
def entrypoint(show_list, entrypoint):
58+
f"""Entrypoints registered under {ENTRYPOINT_GROUP}"""
59+
if not show_list and not entrypoint:
60+
click.secho("Please provide an entrypoint name or use --list", fg="red")
61+
sys.exit(1)
62+
63+
for entry_point in entry_points().select(group=ENTRYPOINT_GROUP):
64+
if show_list:
65+
click.echo(entry_point.name)
66+
elif entrypoint == entry_point.name:
67+
entry_point.load()()
68+
69+
4970
class Dev:
5071
def __init__(self, *, port):
5172
self.poncho = PonchoManager()
@@ -82,7 +103,7 @@ def run(self):
82103

83104
# Processes for poncho to run simultaneously
84105
self.add_gunicorn()
85-
self.add_tailwind()
106+
self.add_entrypoints()
86107
self.add_pyproject_run()
87108
self.add_services()
88109

@@ -230,11 +251,11 @@ def add_gunicorn(self):
230251

231252
self.poncho.add_process("plain", runserver_cmd, env=self.plain_env)
232253

233-
def add_tailwind(self):
234-
if not plainpackage_installed("tailwind"):
235-
return
236-
237-
self.poncho.add_process("tailwind", "plain tailwind compile --watch")
254+
def add_entrypoints(self):
255+
for entry_point in entry_points().select(group=ENTRYPOINT_GROUP):
256+
self.poncho.add_process(
257+
f"plain dev entrypoint {entry_point.name}", env=self.plain_env
258+
)
238259

239260
def add_pyproject_run(self):
240261
if not has_pyproject_toml(APP_PATH.parent):

plain-dev/plain/dev/utils.py

-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
import importlib
21
from pathlib import Path
32

43

5-
def plainpackage_installed(name: str) -> bool:
6-
try:
7-
importlib.import_module(f"plain.{name}")
8-
return True
9-
except ImportError:
10-
return False
11-
12-
134
def has_pyproject_toml(target_path):
145
return (Path(target_path) / "pyproject.toml").exists()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .cli import compile
2+
3+
4+
def run_dev_compile():
5+
compile(["--watch"])
6+
7+
8+
def run_compile():
9+
compile([])

plain-tailwind/pyproject.toml

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ homepage = "https://plainframework.com"
1414
documentation = "https://plainframework.com/docs/"
1515
repository = "https://github.com/dropseed/plain"
1616

17+
[tool.poetry.plugins."plain.dev"]
18+
"tailwind" = "plain.tailwind.entrypoints:run_dev_compile"
19+
20+
[tool.poetry.plugins."plain.assets.compile"]
21+
"tailwind" = "plain.tailwind.entrypoints:run_compile"
22+
1723
[tool.poetry.dependencies]
1824
python = "^3.11"
1925
plain = "<1.0.0"

plain/plain/cli/cli.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import sys
77
import traceback
8+
from importlib.metadata import entry_points
89
from importlib.util import find_spec
910
from pathlib import Path
1011

@@ -286,17 +287,10 @@ def compile(keep_original, fingerprint, compress):
286287
)
287288
sys.exit(1)
288289

289-
# TODO make this an entrypoint instead
290-
# Compile our Tailwind CSS (including templates in plain itself)
291-
if find_spec("plain.tailwind") is not None:
292-
click.secho("Compiling Tailwind CSS", bold=True)
293-
result = subprocess.run(["plain", "tailwind", "compile", "--minify"])
290+
for entry_point in entry_points(group="plain.assets.compile"):
291+
click.secho(f"Running {entry_point.name}", bold=True)
292+
result = entry_point.load()()
294293
print()
295-
if result.returncode:
296-
click.secho(
297-
f"Error compiling Tailwind CSS (exit {result.returncode})", fg="red"
298-
)
299-
sys.exit(result.returncode)
300294

301295
# TODO also look in [tool.plain.compile.run]
302296

0 commit comments

Comments
 (0)