|
22 | 22 | from pathlib import Path |
23 | 23 |
|
24 | 24 | if T.TYPE_CHECKING: |
25 | | - from ._typing import StringProtocol, SizedStringProtocol |
| 25 | + from typing_extensions import Literal |
26 | 26 |
|
| 27 | + from ._typing import StringProtocol, SizedStringProtocol |
27 | 28 | from .mparser import BaseNode |
28 | 29 |
|
29 | 30 | TV_Loggable = T.Union[str, 'AnsiDecorator', StringProtocol] |
@@ -75,6 +76,7 @@ def setup_console() -> None: |
75 | 76 | pass |
76 | 77 |
|
77 | 78 | _in_ci = 'CI' in os.environ |
| 79 | +_ci_is_github = 'GITHUB_ACTIONS' in os.environ |
78 | 80 |
|
79 | 81 |
|
80 | 82 | class _Severity(enum.Enum): |
@@ -540,3 +542,30 @@ def code_line(text: str, line: str, colno: int) -> str: |
540 | 542 | :return: A formatted string of the text, line, and a caret |
541 | 543 | """ |
542 | 544 | return f'{text}\n{line}\n{" " * colno}^' |
| 545 | + |
| 546 | +@T.overload |
| 547 | +def ci_fold_file(fname: T.Union[str, os.PathLike], banner: str, force: Literal[True] = True) -> str: ... |
| 548 | + |
| 549 | +@T.overload |
| 550 | +def ci_fold_file(fname: T.Union[str, os.PathLike], banner: str, force: Literal[False] = False) -> T.Optional[str]: ... |
| 551 | + |
| 552 | +def ci_fold_file(fname: T.Union[str, os.PathLike], banner: str, force: bool = False) -> T.Optional[str]: |
| 553 | + if not _in_ci and not force: |
| 554 | + return None |
| 555 | + |
| 556 | + if _ci_is_github: |
| 557 | + header = f'::group::==== {banner} ====' |
| 558 | + footer = '::endgroup::' |
| 559 | + elif force: |
| 560 | + header = banner |
| 561 | + footer = '' |
| 562 | + elif 'MESON_FORCE_SHOW_LOGS' in os.environ: |
| 563 | + header = f'==== Forcing display of logs for {os.path.basename(fname)} ====' |
| 564 | + footer = '' |
| 565 | + else: |
| 566 | + # only github is implemented |
| 567 | + return None |
| 568 | + |
| 569 | + with open(fname, 'r', encoding='utf-8') as f: |
| 570 | + data = f.read() |
| 571 | + return f'{header}\n{data}\n{footer}\n' |
0 commit comments