Skip to content

Commit 7b0f84f

Browse files
authored
feat(poetry plugin): add support for upcoming Poetry PEP 621 projects (#278)
* feat(poetry plugin): add support for upcoming Poetry PEP 621 projects * bump Poetry plugin to 1.31.0 * bump CLI to 1.20.2 * bump hatch hook to 1.2.7 * bump PDM brick hook to 1.0.9 * bump PDM workspace hook to 1.0.9 * fix(project type): raise ValueError for unknown Project type
1 parent f6b2390 commit 7b0f84f

File tree

14 files changed

+146
-28
lines changed

14 files changed

+146
-28
lines changed

bases/polylith/cli/create.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pathlib import Path
22

3-
from polylith import project, repo
3+
from polylith import project
44
from polylith.bricks import base, component
55
from polylith.commands.create import create
66
from polylith.workspace.create import create_workspace
@@ -29,16 +29,10 @@ def component_command(
2929

3030

3131
def _create_project(root: Path, options: dict):
32-
root_pyproject: dict = project.get_toml(root / repo.default_toml)
3332
name = options["package"]
3433
description = options["description"]
3534

36-
if repo.is_poetry(root_pyproject):
37-
template = project.templates.poetry_pyproject
38-
elif repo.is_hatch(root_pyproject):
39-
template = project.templates.hatch_pyproject
40-
elif repo.is_pdm(root_pyproject):
41-
template = project.templates.pdm_pyproject
35+
template = project.get_project_template(root)
4236

4337
if not template:
4438
print("Failed to guess the used Package & Dependency Management")

components/polylith/poetry/commands/create_project.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def create_project(root: Path, options: dict):
1212
package = options["package"]
1313
desc = options["description"] or ""
1414

15-
project.create_project(root, project.templates.poetry_pyproject, package, desc)
15+
template = project.get_project_template(root)
16+
project.create_project(root, template, package, desc)
1617

1718

1819
class CreateProjectCommand(Command):

components/polylith/project/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
from polylith.project import templates
22
from polylith.project.create import create_project
3-
from polylith.project.get import get_packages_for_projects, get_project_name, get_toml
3+
from polylith.project.get import (
4+
get_packages_for_projects,
5+
get_project_name,
6+
get_project_template,
7+
get_toml,
8+
)
49
from polylith.project.parser import parse_package_paths
510

611
__all__ = [
712
"create_project",
813
"get_packages_for_projects",
914
"get_project_name",
15+
"get_project_template",
1016
"get_toml",
1117
"parse_package_paths",
1218
"templates",

components/polylith/project/get.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import tomlkit
66
from polylith import configuration, repo, toml
7+
from polylith.project import templates
78

89

910
def get_project_name(toml_data) -> str:
@@ -64,3 +65,29 @@ def get_packages_for_projects(root: Path) -> List[dict]:
6465
}
6566
for d in toml_files
6667
]
68+
69+
70+
def _get_poetry_template(pyproject: dict) -> str:
71+
if repo.is_pep_621_ready(pyproject):
72+
return templates.poetry_pep621_pyproject
73+
74+
return templates.poetry_pyproject
75+
76+
77+
def guess_project_template(pyproject: dict) -> str:
78+
if repo.is_poetry(pyproject):
79+
template = _get_poetry_template(pyproject)
80+
elif repo.is_hatch(pyproject):
81+
template = templates.hatch_pyproject
82+
elif repo.is_pdm(pyproject):
83+
template = templates.pdm_pyproject
84+
else:
85+
raise ValueError("Failed to guess the type of Project")
86+
87+
return template
88+
89+
90+
def get_project_template(root: Path) -> str:
91+
root_pyproject = get_toml(root / repo.default_toml)
92+
93+
return guess_project_template(root_pyproject)

components/polylith/project/templates.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@
1616
build-backend = "poetry.core.masonry.api"
1717
"""
1818

19+
poetry_pep621_pyproject = """\
20+
[tool.poetry]
21+
packages = []
22+
23+
[project]
24+
name = "{name}"
25+
version = "0.1.0"
26+
{description}
27+
{authors}
28+
29+
requires-python = "{python_version}"
30+
31+
dependencies = []
32+
33+
[build-system]
34+
requires = ["poetry-core>=1.0.0"]
35+
build-backend = "poetry.core.masonry.api"
36+
"""
37+
1938
hatch_pyproject = """\
2039
[build-system]
2140
requires = ["hatchling", "hatch-polylith-bricks"]

components/polylith/repo/repo.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,4 @@ def is_pdm(pyproject: dict) -> bool:
8080

8181

8282
def is_pep_621_ready(pyproject: dict) -> bool:
83-
if is_poetry(pyproject):
84-
return False
85-
8683
return pyproject.get("project", {}).get("name") is not None

components/polylith/toml/core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@ def get_pep_621_optional_dependencies(data) -> List[str]:
9393
return sum(matrix, [])
9494

9595

96+
def is_poetry_without_pep_621_support(data) -> bool:
97+
return repo.is_poetry(data) and not repo.is_pep_621_ready(data)
98+
99+
96100
def parse_project_dependencies(data) -> dict:
97-
if repo.is_poetry(data):
101+
if is_poetry_without_pep_621_support(data):
98102
deps = data["tool"]["poetry"].get("dependencies", {})
99103
res: dict = reduce(parse_poetry_dependency, deps.items(), {})
100104

projects/hatch_polylith_bricks/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "hatch-polylith-bricks"
3-
version = "1.2.6"
3+
version = "1.2.7"
44
description = "Hatch build hook plugin for Polylith"
55
authors = ['David Vujic']
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

projects/pdm_polylith_bricks/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pdm-polylith-bricks"
3-
version = "1.0.8"
3+
version = "1.0.9"
44
description = "a PDM build hook for Polylith"
55
authors = ["David Vujic"]
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

projects/pdm_polylith_workspace/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pdm-polylith-workspace"
3-
version = "1.0.8"
3+
version = "1.0.9"
44
description = "a PDM build hook for a Polylith workspace"
55
homepage = "https://davidvujic.github.io/python-polylith-docs/"
66
repository = "https://github.com/davidvujic/python-polylith"

0 commit comments

Comments
 (0)