Skip to content

Commit

Permalink
fix: fix and simplify work command
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobi-De committed Dec 17, 2023
1 parent db445da commit 92061cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/falco/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2023-present Tobi DEGNON <tobidegnon@proton.me>
#
# SPDX-License-Identifier: MIT
__version__ = "0.0.2"
__version__ = "0.0.3"
76 changes: 10 additions & 66 deletions src/falco/commands/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import os
import sys
from pathlib import Path
from typing import Annotated

import cappa
from dict_deep import deep_get
from dotenv import dotenv_values
from falco.utils import get_current_dir_as_project_name
from honcho.manager import Manager as HonchoManager

try:
Expand All @@ -21,52 +18,9 @@ def read_toml(file: Path) -> dict:
return tomllib.loads(file.read_text())


def _get_venv_directory() -> str | None:
patterns = ["venv", ".venv"]
for p in patterns:
path = Path(p)
if path.exists() and (path / "bin/python").exists():
return p


def _get_user_commands(file: Path) -> dict:
try:
config = read_toml(file)
return deep_get(config, "tool.cuzzy.work") or {}
except FileNotFoundError:
return {}


def _get_redis_command(django_env: dict) -> str | None:
redis_url = django_env.get("REDIS_URL", "")
if "localhost" in redis_url or "127.0.0.1" in redis_url:
redis_port = redis_url.split(":")[-1].split("/")[0]
return f"redis-server --port {redis_port}"


def _get_tailwind_command(pyproject_file: Path, project_name: str) -> str | None:
config = read_toml(pyproject_file)
dependencies = deep_get(config, "tool.poetry.dependencies") or {}
if "pytailwindcss" in dependencies:
return f"tailwindcss -i {project_name}/static/css/input.css -o {project_name}/static/css/output.css --watch"


def _update_command_with_venv(venv_dir: str | None, cmd: str) -> str:
# TODO: this could also be done with other scripts like celery or arq
return f"{venv_dir}/bin/{cmd}" if venv_dir and cmd.startswith("python") else cmd


@cappa.command(help="Run multiple processes in parallel.")
class Work:
pyproject_file: Annotated[
Path,
cappa.Arg(
default=Path("pyproject.toml"),
hidden=True,
),
]

def __call__(self, project_name: Annotated[str, cappa.Dep(get_current_dir_as_project_name)]) -> None:
def __call__(self) -> None:
"""Run multiple processes in parallel."""

django_env = {
Expand All @@ -76,26 +30,16 @@ def __call__(self, project_name: Annotated[str, cappa.Dep(get_current_dir_as_pro
"PYTHONUNBUFFERED": "true",
}

venv_dir = _get_venv_directory()

migrate_cmd = _update_command_with_venv(venv_dir, "python manage.py migrate")
runserver_cmd = _update_command_with_venv(venv_dir, "python manage.py runserver --nostatic")
commands = {
"server": f"{migrate_cmd} && {runserver_cmd}",
}

if redis_cmd := _get_redis_command(django_env):
commands["redis"] = redis_cmd

if self.pyproject_file.exists():
if tailwind_cmd := _get_tailwind_command(self.pyproject_file, self.project_name):
commands["tailwind"] = tailwind_cmd

user_commands = _get_user_commands(self.pyproject_file)

user_commands = {k: _update_command_with_venv(venv_dir, v) for k, v in user_commands.items()}
try:
pyproject_config = read_toml(Path("pyproject.toml"))
commands = pyproject_config.get("tool", {}).get("falco", {}).get("work", {})
except FileNotFoundError as e:
raise cappa.Exit(
"The pyproject.toml file could not be found. ", code=1
) from e

commands |= user_commands
if not commands:
raise cappa.Exit("No commands were found in the pyproject.toml file. ")

manager = HonchoManager()

Expand Down

0 comments on commit 92061cd

Please sign in to comment.