diff --git a/setup.py b/setup.py index fcef7e4281..ff3eb09f03 100644 --- a/setup.py +++ b/setup.py @@ -112,6 +112,7 @@ "tqdm>=4.62.3,<5.0", "traitlets>=5.3.0", "watchdog>=3.0,<4", + "tomli>=2.0.1; python_version<'3.11'", # ** Dependencies maintained by Ethereum Foundation ** "eth-abi>=4.1.0,<5", "eth-account>=0.8,<0.9", diff --git a/src/ape_init/_cli.py b/src/ape_init/_cli.py index a804ce064a..0c98a4423f 100644 --- a/src/ape_init/_cli.py +++ b/src/ape_init/_cli.py @@ -1,11 +1,47 @@ import shutil from pathlib import Path +from typing import List import click from ape.cli import ape_cli_context from ape.utils import github_client +try: + import tomllib # type: ignore[import] +# backwards compatibility +except ModuleNotFoundError: + import tomli as tomllib + + +def read_dependencies_from_toml(file_path, cli_ctx) -> List[str]: + + with open("./pyproject.toml", "rb") as file: + try: + data = tomllib.load(file) + except FileNotFoundError: + cli_ctx.logger.warning( + f"Unable to populate contnets from pyproject.toml file. {file_path} doesn't exists." + ) + + except tomllib.TOMLDecodeError: + cli_ctx.logger.warning(f"Error reading {file_path} file.") + + # Extract the 'tool.poetry.dependencies' section + dependencies = data.get("tool", {}).get("poetry", {}).get("dependencies", {}) + + # Check each dependency to see if it starts with 'ape-', because we know these are the ape plugins + ape_plugins = [dep[4:] for dep in dependencies if dep.startswith("ape-")] + + return ape_plugins + + +def write_ape_config_yml(dependencies: List[str], file_to_write: Path): + dependency_text = "plugins:\n" + "\n".join( + [f" - name: {dependency}" for dependency in dependencies] + ) + file_to_write.write_text(dependency_text) + @click.command(short_help="Initalize an ape project") @ape_cli_context() @@ -53,9 +89,17 @@ def cli(cli_ctx, github): git_ignore_path.write_text(body.lstrip()) ape_config = project_folder / "ape-config.yaml" - if ape_config.exists(): + if ape_config.is_file(): cli_ctx.logger.warning(f"'{ape_config}' exists") else: project_name = click.prompt("Please enter project name") ape_config.write_text(f"name: {project_name}\n") cli_ctx.logger.success(f"{project_name} is written in ape-config.yaml") + + pyproject_toml = project_folder / "pyproject.toml" + if pyproject_toml.is_file(): + dependencies = read_dependencies_from_toml(pyproject_toml, cli_ctx) + write_ape_config_yml(dependencies, ape_config) + cli_ctx.logger.success( + f"Generated {ape_config} based on the currect pyproject.toml file" + )