Skip to content

Commit 9f74fb3

Browse files
committed
Run services automatically in pre-commit
1 parent 5bd474d commit 9f74fb3

File tree

2 files changed

+75
-53
lines changed

2 files changed

+75
-53
lines changed

bolt-dev/bolt/dev/precommit/cli.py

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from bolt.cli.print import print_event
88

9+
from ..services import Services
10+
911
try:
1012
import tomllib
1113
except ModuleNotFoundError:
@@ -38,60 +40,63 @@ def cli(install):
3840

3941
pyproject = Path("pyproject.toml")
4042

41-
if pyproject.exists():
42-
with open(pyproject, "rb") as f:
43-
pyproject = tomllib.load(f)
44-
for name, data in (
45-
pyproject.get("tool", {})
46-
.get("bolt", {})
47-
.get("pre-commit", {})
48-
.get("run", {})
49-
).items():
50-
cmd = data["cmd"]
51-
print_event(f"Custom: {name} -> {cmd}")
52-
result = subprocess.run(cmd, shell=True)
53-
if result.returncode != 0:
54-
sys.exit(result.returncode)
55-
56-
# Run this first since it's probably the most likely to fail
57-
if find_spec("bolt.code"):
58-
check_short("Running bolt code checks", "bolt", "code", "check")
59-
60-
if Path("poetry.lock").exists():
61-
check_short("Checking poetry.lock", "poetry", "lock", "--check")
62-
63-
if bolt_db_connected():
64-
check_short(
65-
"Running preflight checks",
66-
"bolt",
67-
"preflight",
68-
"--database",
69-
"default",
70-
)
71-
check_short("Checking Bolt migrations", "bolt", "legacy", "migrate", "--check")
72-
check_short(
73-
"Checking for Bolt models missing migrations",
74-
"bolt",
75-
"legacy",
76-
"makemigrations",
77-
"--dry-run",
78-
"--check",
79-
)
80-
else:
81-
check_short("Running Bolt checks (without database)", "bolt", "preflight")
82-
click.secho("--> Skipping migration checks", bold=True, fg="yellow")
83-
84-
print_event("Running bolt compile")
85-
result = subprocess.run(["bolt", "compile"])
86-
if result.returncode != 0:
87-
sys.exit(result.returncode)
43+
with Services():
44+
if pyproject.exists():
45+
with open(pyproject, "rb") as f:
46+
pyproject = tomllib.load(f)
47+
for name, data in (
48+
pyproject.get("tool", {})
49+
.get("bolt", {})
50+
.get("pre-commit", {})
51+
.get("run", {})
52+
).items():
53+
cmd = data["cmd"]
54+
print_event(f"Custom: {name} -> {cmd}")
55+
result = subprocess.run(cmd, shell=True)
56+
if result.returncode != 0:
57+
sys.exit(result.returncode)
58+
59+
# Run this first since it's probably the most likely to fail
60+
if find_spec("bolt.code"):
61+
check_short("Running bolt code checks", "bolt", "code", "check")
62+
63+
if Path("poetry.lock").exists():
64+
check_short("Checking poetry.lock", "poetry", "lock", "--check")
65+
66+
if bolt_db_connected():
67+
check_short(
68+
"Running preflight checks",
69+
"bolt",
70+
"preflight",
71+
"--database",
72+
"default",
73+
)
74+
check_short(
75+
"Checking Bolt migrations", "bolt", "legacy", "migrate", "--check"
76+
)
77+
check_short(
78+
"Checking for Bolt models missing migrations",
79+
"bolt",
80+
"legacy",
81+
"makemigrations",
82+
"--dry-run",
83+
"--check",
84+
)
85+
else:
86+
check_short("Running Bolt checks (without database)", "bolt", "preflight")
87+
click.secho("--> Skipping migration checks", bold=True, fg="yellow")
8888

89-
if find_spec("bolt.pytest"):
90-
print_event("Running tests")
91-
result = subprocess.run(["bolt", "test"])
89+
print_event("Running bolt compile")
90+
result = subprocess.run(["bolt", "compile"])
9291
if result.returncode != 0:
9392
sys.exit(result.returncode)
9493

94+
if find_spec("bolt.pytest"):
95+
print_event("Running tests")
96+
result = subprocess.run(["bolt", "test"])
97+
if result.returncode != 0:
98+
sys.exit(result.returncode)
99+
95100

96101
def bolt_db_connected():
97102
result = subprocess.run(

bolt-dev/bolt/dev/services.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import subprocess
33
import time
4+
from importlib.util import find_spec
45
from pathlib import Path
56

67
import click
@@ -46,15 +47,31 @@ def run(self):
4647
self.manager.loop()
4748

4849
def __enter__(self):
50+
if not self.get_services(APP_PATH.parent):
51+
# No-op if no services are defined
52+
return
53+
4954
if Pid().exists():
5055
click.secho("Services already running in `bolt dev` command", fg="yellow")
5156
return
5257

5358
print("Starting `bolt dev services`")
54-
self.subprocess = subprocess.Popen(
59+
self._subprocess = subprocess.Popen(
5560
["bolt", "dev", "services"], cwd=APP_PATH.parent
5661
)
57-
time.sleep(0.5)
62+
63+
if find_spec("bolt.db"):
64+
time.sleep(0.5) # Give it a chance to hit on the first try
65+
subprocess.check_call(["bolt", "db", "wait"], env=os.environ)
66+
else:
67+
# A bit of a hack to wait for the services to start
68+
time.sleep(3)
5869

5970
def __exit__(self, *args):
60-
self.subprocess.terminate()
71+
if not hasattr(self, "_subprocess"):
72+
return
73+
74+
self._subprocess.terminate()
75+
76+
# Flush the buffer so the output doesn't spill over
77+
self._subprocess.communicate()

0 commit comments

Comments
 (0)