view libervia/web/server/tasks/implicit/task_sass.py @ 1618:5d9889f14012 default tip @

server: start major redesign - Add icons to menu items - Switch menu items representation from tuple to dictionary for future extensibility: - Include icon information - Prepare for additional data - Remove "login" from main menu, add login page URL to template data, as it is now a separate right-aligned item
author Goffi <goffi@goffi.org>
date Sat, 26 Oct 2024 23:07:01 +0200
parents 29eb1ea35869
children
line wrap: on
line source

#!/ur/bin/env python3

import json
from libervia.backend.core.log import getLogger
from libervia.backend.core import exceptions
from libervia.web.server.tasks import task


log = getLogger(__name__)

SASS_SUFFIXES = ('.sass', '.scss')


class Task(task.Task):
    """Compile .sass and .scss files found in themes browser paths"""
    AFTER = ['js_modules']

    async def prepare(self):
        # we look for any Sass file, and cancel this task if none is found
        sass_dirs = set()
        for browser_path in self.resource.browser_modules.get('themes_browser_paths', []):
            for p in browser_path.iterdir():
                if p.suffix in SASS_SUFFIXES:
                    sass_dirs.add(browser_path)
                    break

        if not sass_dirs:
            raise exceptions.CancelError("No Sass file found")

        # we have some Sass files, we need to install the compiler
        d_path = self.resource.dev_build_path
        package_path = d_path / "package.json"
        try:
            with package_path.open() as f:
                package = json.load(f)
        except FileNotFoundError:
            package = {}
        except Exception as e:
            log.error(f"Unexepected exception while parsing package.json: {e}")

        if 'sass' not in package.setdefault('dependencies', {}):
            package['dependencies']['sass'] = '^1.77'
            with package_path.open('w') as f:
                json.dump(package, f, indent=4)

        cmd = self.find_command('yarnpkg', 'yarn')
        await self.runCommand(cmd, 'install', path=str(d_path))

        self.WATCH_DIRS = list(sass_dirs)

    async def on_dir_event(self, host, filepath, flags):
        if filepath.suffix in SASS_SUFFIXES:
            await self.manager.run_task_instance(self)

    async def start(self):
        d_path = self.resource.dev_build_path
        node_sass = d_path / 'node_modules' / 'sass' / 'sass.js'
        for browser_path in self.resource.browser_modules['themes_browser_paths']:
            for p in browser_path.iterdir():
                if p.suffix not in SASS_SUFFIXES:
                    continue
                await self.runCommand(
                    str(node_sass),
                    "--load-path", str(self.build_path),
                    "--no-source-map",
                    "--style", "compressed",
                    str(p),
                    str(self.build_path/p.with_suffix(".css").name),
                    path=str(self.build_path)
                )