|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any, Dict, List, Sequence, Tuple |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +from robotcode.core.dataclasses import as_json |
| 7 | +from robotcode.plugin import ClickCommonConfig, ColoredOutput |
| 8 | +from robotcode.robot.config.loader import ( |
| 9 | + ConfigType, |
| 10 | + find_project_root, |
| 11 | + get_config_files_from_folder, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def print_dict(config: Dict[str, Any], format: str, color: ColoredOutput) -> None: |
| 16 | + text = None |
| 17 | + if format == "toml": |
| 18 | + try: |
| 19 | + import tomli_w |
| 20 | + |
| 21 | + text = tomli_w.dumps(config) |
| 22 | + except ImportError: |
| 23 | + click.secho("tomli-w is required to output toml.", fg="red", err=True) |
| 24 | + |
| 25 | + format = "json" |
| 26 | + |
| 27 | + if text is None: |
| 28 | + text = as_json(config, indent=True) |
| 29 | + |
| 30 | + if not text: |
| 31 | + return |
| 32 | + |
| 33 | + if color in [ColoredOutput.AUTO, ColoredOutput.YES]: |
| 34 | + try: |
| 35 | + from rich.console import Console |
| 36 | + from rich.syntax import Syntax |
| 37 | + |
| 38 | + Console().print(Syntax(text, format, background_color="default")) |
| 39 | + |
| 40 | + return |
| 41 | + except ImportError as e: |
| 42 | + if color == "yes": |
| 43 | + raise click.ClickException('Package "rich" is required to use colored output.') from e |
| 44 | + |
| 45 | + click.echo(text) |
| 46 | + |
| 47 | + return |
| 48 | + |
| 49 | + |
| 50 | +def get_config_files(common_config: ClickCommonConfig, paths: List[Path]) -> Sequence[Tuple[Path, ConfigType]]: |
| 51 | + if common_config.config_file is not None: |
| 52 | + if common_config.verbose: |
| 53 | + click.secho(f"Using config file: {common_config.config_file}", fg="bright_black") |
| 54 | + |
| 55 | + return [(common_config.config_file, ConfigType.CUSTOM_TOML)] |
| 56 | + |
| 57 | + root_folder, discovered_by = find_project_root(*(paths or [])) |
| 58 | + |
| 59 | + if root_folder is None: |
| 60 | + raise click.ClickException("Cannot detect root folder for project. 😥") |
| 61 | + |
| 62 | + if common_config.verbose: |
| 63 | + click.secho(f"Found project root at:\n {root_folder} ({discovered_by})", fg="bright_black") |
| 64 | + |
| 65 | + result = get_config_files_from_folder(root_folder) |
| 66 | + |
| 67 | + if result: |
| 68 | + if common_config.verbose: |
| 69 | + click.secho( |
| 70 | + "Found configuration files:\n " + "\n ".join(str(f[0]) for f in result), |
| 71 | + fg="bright_black", |
| 72 | + ) |
| 73 | + |
| 74 | + return result |
0 commit comments