Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added option to generate basic ape-config.yaml file if [tool.ape.plugins] property is specified inside pyproject.toml file [APE-1312] #1618

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 45 additions & 1 deletion src/ape_init/_cli.py
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should all happen in the config manage, not in ape-init plugin.
And then you can use the project_manager.path as the base here instead of cwd

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of writing an ape file, the ConfigManager should read the values from the pyproject.toml and treat it the same as if the config file had existed. There is not reason to configure twice, even if one is auto-generated.

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()
Expand Down Expand Up @@ -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"
)
Loading