|
| 1 | +import shutil |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from .core import ContextDir, Step |
| 5 | +from .dependencies import pip_dependency |
| 6 | + |
| 7 | + |
| 8 | +class ResourcePackerStep(Step): |
| 9 | + def __init__(self, source_dir: ContextDir = 'input_dir'): |
| 10 | + self.source_dir: ContextDir = source_dir |
| 11 | + |
| 12 | + def __call__(self, path: Path, output_paths: list[Path]): |
| 13 | + parent_dir = self.context[self.source_dir] |
| 14 | + data = '\n\n'.join( |
| 15 | + (parent_dir / filename.strip()).read_text('utf-8') |
| 16 | + for filename in path.open() if filename |
| 17 | + ) |
| 18 | + |
| 19 | + for o_path in output_paths: |
| 20 | + o_path.parent.mkdir(parents=True, exist_ok=True) |
| 21 | + output_paths[0].write_text(data, 'utf-8') |
| 22 | + for o_path in output_paths[1:]: |
| 23 | + shutil.copy(output_paths[0], o_path) |
| 24 | + |
| 25 | + |
| 26 | +class CSSMinifierStep(Step): |
| 27 | + @classmethod |
| 28 | + def get_dependencies(cls): |
| 29 | + return { |
| 30 | + pip_dependency('csscompressor'), |
| 31 | + } |
| 32 | + |
| 33 | + def __call__(self, path: Path, output_paths: list[Path]): |
| 34 | + import csscompressor |
| 35 | + data = csscompressor.compress(path.read_text('utf-8')) |
| 36 | + for o_path in output_paths: |
| 37 | + o_path.parent.mkdir(parents=True, exist_ok=True) |
| 38 | + output_paths[0].write_text(data, 'utf-8') |
| 39 | + for o_path in output_paths[1:]: |
| 40 | + shutil.copy(output_paths[0], o_path) |
| 41 | + |
| 42 | + |
| 43 | +class HTMLMinifierStep(Step): |
| 44 | + @classmethod |
| 45 | + def get_dependencies(cls): |
| 46 | + return { |
| 47 | + pip_dependency('minify-html-onepass', check_name='minify_html_onepass'), |
| 48 | + } |
| 49 | + |
| 50 | + def __call__(self, path: Path, output_paths: list[Path]): |
| 51 | + import minify_html_onepass |
| 52 | + data = minify_html_onepass.minify(path.read_text('utf-8')) |
| 53 | + for o_path in output_paths: |
| 54 | + o_path.parent.mkdir(parents=True, exist_ok=True) |
| 55 | + output_paths[0].write_text(data, 'utf-8') |
| 56 | + for o_path in output_paths[1:]: |
| 57 | + shutil.copy(output_paths[0], o_path) |
0 commit comments