|
| 1 | +import dataclasses |
| 2 | +import pathlib |
| 3 | +import sys |
| 4 | +import typing |
| 5 | + |
| 6 | +if sys.version_info >= (3, 12): |
| 7 | + from typing import override |
| 8 | +else: |
| 9 | + from typing_extensions import override |
| 10 | + |
| 11 | +from finecode_extension_api import code_action, textstyler |
| 12 | + |
| 13 | + |
| 14 | +@dataclasses.dataclass |
| 15 | +class BuildRunPayload(code_action.RunActionPayload): |
| 16 | + # path to package root dir, where usually pyproject.toml is located. Note, that |
| 17 | + # packages can have different layouts. Use service (TODO) to get package source |
| 18 | + # directory path and avoid need to handle all possible cases by yourself. |
| 19 | + package_root_path: pathlib.Path |
| 20 | + build_type: typing.Literal['release'] | typing.Literal['debug'] = 'release' |
| 21 | + # TODO: entrypoint |
| 22 | + # TODO: package type |
| 23 | + # TODO: target platform? (including version etc) |
| 24 | + |
| 25 | + |
| 26 | +class BuildRunContext(code_action.RunActionContext): |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + run_id: int, |
| 30 | + ) -> None: |
| 31 | + super().__init__(run_id=run_id) |
| 32 | + |
| 33 | + |
| 34 | +@dataclasses.dataclass |
| 35 | +class BuildRunResult(code_action.RunActionResult): |
| 36 | + # files/directories which are results of build |
| 37 | + # TODO: for better abstraction we could split build and packaging even in case of |
| 38 | + # wheel and sdist |
| 39 | + results: list[pathlib.Path] |
| 40 | + |
| 41 | + @override |
| 42 | + def update(self, other: code_action.RunActionResult) -> None: |
| 43 | + if not isinstance(other, BuildRunResult): |
| 44 | + return |
| 45 | + |
| 46 | + def to_text(self) -> str | textstyler.StyledText: |
| 47 | + return '' |
| 48 | + |
| 49 | + |
| 50 | +# general build action: any type of project should be built: library(pure and not pure python), application(both pure distributed as python package and application transformed to executable) |
| 51 | +# concrete use cases: |
| 52 | +# 1. Pure Python package built with `build` and `setuptools`, result: sdist and wheel. |
| 53 | +# 2. Python Package with mypyc. TODO |
| 54 | +# 3. Application distributed as python package: the same as use case 1. |
| 55 | +# 4. Application compiled with Nuitka to executable. |
| 56 | +# 5. Application packaged to executable with pyinstaller or similar tool. |
| 57 | +# |
| 58 | +# Customization examples: |
| 59 | +# - Recognize constructs(syntax, imports) supported only by higher versions of python and replace them by alternatives from older python. One universal wheel will become version-specific wheel. |
| 60 | +# - the same could be applied for platform-specific functionalities |
| 61 | +# - optimize implementation |
| 62 | +type BuildAction = code_action.Action[ |
| 63 | + BuildRunPayload, BuildRunContext, BuildRunResult |
| 64 | +] |
0 commit comments