From cba45dea5c65ae0646a600384f6721bd83332301 Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Mon, 11 Nov 2024 17:10:22 +0100 Subject: [PATCH] Add clean and messages management script and update build script --- rdmo/core/management/commands/build.py | 3 ++ rdmo/core/management/commands/clean.py | 53 +++++++++++++++++++++++ rdmo/core/management/commands/messages.py | 17 ++++++++ rdmo/core/management/settings.py | 2 + 4 files changed, 75 insertions(+) create mode 100644 rdmo/core/management/commands/clean.py create mode 100644 rdmo/core/management/commands/messages.py diff --git a/rdmo/core/management/commands/build.py b/rdmo/core/management/commands/build.py index 90746fafc..9e67ff190 100644 --- a/rdmo/core/management/commands/build.py +++ b/rdmo/core/management/commands/build.py @@ -1,10 +1,13 @@ import subprocess import sys +from django.core.management import call_command from django.core.management.base import BaseCommand class Command(BaseCommand): def handle(self, *args, **options): + call_command('npm', 'ci') + call_command('npm', 'run', 'build:prod') subprocess.call(['/bin/bash', '-c', f'{sys.executable} -m build']) diff --git a/rdmo/core/management/commands/clean.py b/rdmo/core/management/commands/clean.py new file mode 100644 index 000000000..bd7a3de43 --- /dev/null +++ b/rdmo/core/management/commands/clean.py @@ -0,0 +1,53 @@ +import os +import shutil + +from django.conf import settings +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + + def add_arguments(self, parser): + parser.add_argument('command', choices=[ + 'all', + 'dist', + 'media', + 'npm', + 'python', + 'static', + ]) + + def handle(self, *args, **options): + if options['command'] in ['all', 'dist']: + self.clean_dir('dist') + self.clean_dir('rdmo.egg-info') + if options['command'] in ['all', 'media']: + self.clean_dir(settings.MEDIA_ROOT) + if options['command'] in ['all', 'npm']: + self.clean_dir('node_modules') + if options['command'] in ['all', 'static']: + self.clean_static() + if options['command'] in ['all', 'python']: + self.clean_python() + + def clean_python(self): + for root, dirs, files in os.walk('.'): + for dir_name in dirs: + if dir_name == '__pycache__': + self.clean_dir(os.path.join(root, dir_name), quiet=True) + + def clean_static(self): + self.clean_dir(settings.STATIC_ROOT) + + for path in [ + # 'rdmo/core/static', # TODO: enable after cleanup + 'rdmo/management/static', + # 'rdmo/projects/static' # TODO: enable after cleanup + ]: + self.clean_dir(path) + + def clean_dir(self, path, quiet=False): + if path and os.path.exists(path): + shutil.rmtree(path) + if not quiet: + print(f'Directory "{path}" has been removed!') diff --git a/rdmo/core/management/commands/messages.py b/rdmo/core/management/commands/messages.py new file mode 100644 index 000000000..aa1703ca7 --- /dev/null +++ b/rdmo/core/management/commands/messages.py @@ -0,0 +1,17 @@ +import subprocess + +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + + def add_arguments(self, parser): + parser.add_argument('command', choices=['make', 'compile']) + + def handle(self, *args, **options): + if options['command'] == 'make': + subprocess.check_call(['django-admin', 'makemessages', '--all'], cwd='rdmo') + subprocess.check_call(['django-admin', 'makemessages', '--all', '-d', 'djangojs'], cwd='rdmo') + + elif options['command'] == 'compile': + subprocess.check_call(['django-admin', 'compilemessages'], cwd='rdmo') diff --git a/rdmo/core/management/settings.py b/rdmo/core/management/settings.py index 3f10d0011..aebbda76e 100644 --- a/rdmo/core/management/settings.py +++ b/rdmo/core/management/settings.py @@ -7,3 +7,5 @@ ROOT_URLCONF = '' DATABASES = {} + +STATIC_ROOT = 'static_root'