Skip to content

Commit

Permalink
Change bolt code to check and fix, add to precommit, re-use print_event
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed Jan 31, 2024
1 parent fad6c58 commit 8809012
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
33 changes: 11 additions & 22 deletions bolt-code/bolt/code/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import click

from bolt.cli.print import print_event

DEFAULT_RUFF_CONFIG = Path(__file__).parent / "ruff_defaults.toml"


Expand All @@ -16,35 +18,22 @@ def cli():

@cli.command()
@click.argument("path", default=".")
@click.option("--fix/--no-fix", "do_fix", default=False)
def lint(path, do_fix):
def check(path):
"""Check the given path for formatting or linting issues."""
ruff_args = []

if not user_has_ruff_config():
click.secho("Using default bolt.code ruff config", italic=True, bold=True)
click.secho("Using default bolt.code ruff config", italic=True)
ruff_args.extend(["--config", str(DEFAULT_RUFF_CONFIG)])

if do_fix:
ruff_args.append("--fix")

click.secho("Ruff check", bold=True)
print_event("Ruff check")
result = subprocess.run(["ruff", "check", path, *ruff_args])

if result.returncode != 0:
sys.exit(result.returncode)


@cli.command()
@click.argument("path", default=".")
def format(path):
ruff_args = []

if not user_has_ruff_config():
click.secho("Using default bolt.code ruff config", italic=True, bold=True)
ruff_args.extend(["--config", str(DEFAULT_RUFF_CONFIG)])

click.secho("Ruff format", bold=True)
result = subprocess.run(["ruff", "format", path, *ruff_args])
print_event("Ruff format check")
result = subprocess.run(["ruff", "format", path, "--check", *ruff_args])

if result.returncode != 0:
sys.exit(result.returncode)
Expand All @@ -57,16 +46,16 @@ def fix(path):
ruff_args = []

if not user_has_ruff_config():
click.secho("Using default bolt.code ruff config", italic=True, bold=True)
click.secho("Using default bolt.code ruff config", italic=True)
ruff_args.extend(["--config", str(DEFAULT_RUFF_CONFIG)])

click.secho("Ruff check", bold=True)
print_event("Ruff check")
result = subprocess.run(["ruff", "check", path, "--fix", *ruff_args])

if result.returncode != 0:
sys.exit(result.returncode)

click.secho("Ruff format", bold=True)
print_event("Ruff format")
result = subprocess.run(["ruff", "format", path, *ruff_args])

if result.returncode != 0:
Expand Down
15 changes: 7 additions & 8 deletions bolt-dev/bolt/dev/precommit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from importlib.util import find_spec
from pathlib import Path

from bolt.cli.print import print_event

try:
import tomllib
except ModuleNotFoundError:
Expand Down Expand Up @@ -51,6 +53,11 @@ def cli(install):
if result.returncode != 0:
sys.exit(result.returncode)

# Run this first since it's probably the most likely to fail
if find_spec("bolt.code"):
print_event("Running bolt code checks")
subprocess.check_call(["bolt", "code", "check"])

check_short("Checking .env files for changes", "bolt", "env", "check")

if Path("poetry.lock").exists():
Expand Down Expand Up @@ -99,14 +106,6 @@ def bolt_db_connected():
return result.returncode == 0


def print_event(msg, newline=True):
arrow = click.style("-->", fg=214, bold=True)
message = str(msg)
if not newline:
message += " "
click.secho(f"{arrow} {message}", nl=newline)


def check_short(message, *args):
print_event(message, newline=False)
result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Expand Down
9 changes: 9 additions & 0 deletions bolt/cli/print.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import click


def print_event(msg, newline=True):
arrow = click.style("-->", fg=214, bold=True)
message = str(msg)
if not newline:
message += " "
click.secho(f"{arrow} {message}", nl=newline)

0 comments on commit 8809012

Please sign in to comment.