from pathlib import Path
import shutil
from django.core.management.base import BaseCommand
from django.core.management import call_command
from django.conf import settings
OUTPUT_DIR_NAME = "dist"
def copy_files_to_build(file_paths: list[str]):
project_root = Path(settings.BASE_DIR.parent)
out_dir = project_root / OUTPUT_DIR_NAME
out_dir.mkdir(parents=True, exist_ok=True)
for file_path in file_paths:
src = project_root / file_path
dest = out_dir / file_path
if not src.exists():
raise FileNotFoundError(f"{file_path} file not found in project root")
shutil.copyfile(src, dest)
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"--skip-blog",
action="store_true",
help="Skip populate_blog command",
)
parser.add_argument(
"--skip-static",
action="store_true",
help="Skip build_stylesheets command",
)
def handle(self, *args, **options):
skip_static = options["skip_static"]
skip_blog = options["skip_blog"]
if not skip_static:
call_command("build_stylesheets")
call_command("collectstatic", interactive=False)
if not skip_blog:
call_command("populate_blog")
call_command("populate_repo")
call_command("distill-local", OUTPUT_DIR_NAME, force=True)
copy_files_to_build(
[
"_redirects",
]
)