Skip to content

Commit

Permalink
factory: cleanup creation from package
Browse files Browse the repository at this point in the history
This change ensures that generated toml file from packages contain all
relevant metadata and handles groups correct.
  • Loading branch information
abn committed Apr 26, 2022
1 parent 0e5f953 commit 1754502
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/poetry/console/commands/plugin/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def handle(self) -> int:
# we have about the current environment.
if not env_dir.joinpath("pyproject.toml").exists():
Factory.create_pyproject_from_package(
root_package, # type: ignore[arg-type]
root_package,
env_dir,
)

Expand Down
49 changes: 38 additions & 11 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

if TYPE_CHECKING:
from cleo.io.io import IO
from poetry.core.packages.package import Package
from tomlkit.toml_document import TOMLDocument

from poetry.repositories.legacy_repository import LegacyRepository

Expand Down Expand Up @@ -178,23 +180,28 @@ def create_legacy_repository(
)

@classmethod
def create_pyproject_from_package(cls, package: ProjectPackage, path: Path) -> None:
def create_pyproject_from_package(
cls, package: Package, path: Path | None = None
) -> TOMLDocument:
import tomlkit

from poetry.layouts.layout import POETRY_DEFAULT

pyproject = tomlkit.loads(POETRY_DEFAULT)
content = pyproject["tool"]["poetry"]
pyproject = tomlkit.document()
pyproject["tool"] = tomlkit.table()
pyproject["tool"]._is_super_table = True
content = pyproject["tool"]["poetry"] = tomlkit.table()

content["name"] = package.name
content["version"] = package.version.text
content["description"] = package.description
content["authors"] = package.authors
content["license"] = package.license.id if package.license else ""
content["classifiers"] = package.classifiers
content["readme"] = [*package.readmes]

dependency_section = content["dependencies"]
dependency_section = content["dependencies"] = tomlkit.table()
dependency_section["python"] = package.python_versions

for dep in package.requires:
for dep in package.all_requires:
constraint = tomlkit.inline_table()
if dep.is_vcs():
constraint[dep.vcs] = dep.source_url
Expand All @@ -215,8 +222,28 @@ def create_pyproject_from_package(cls, package: ProjectPackage, path: Path) -> N
if len(constraint) == 1 and "version" in constraint:
constraint = constraint["version"]

dependency_section[dep.name] = constraint
for group in dep.groups:
if group == "default":
dependency_section[dep.name] = constraint
else:
if "group" not in content:
content["group"] = tomlkit.table()
content["group"]._is_super_table = True

path.joinpath("pyproject.toml").write_text(
pyproject.as_string(), encoding="utf-8"
)
if group not in content["group"]:
content["group"][group] = tomlkit.table()
content["group"][group]._is_super_table = True

if "dependencies" not in content["group"][group]:
content["group"][group]["dependencies"] = tomlkit.table()

content["group"][group]["dependencies"][dep.name] = constraint

pyproject.add(tomlkit.nl())

if path:
path.joinpath("pyproject.toml").write_text(
pyproject.as_string(), encoding="utf-8"
)

return pyproject

0 comments on commit 1754502

Please sign in to comment.