Skip to content

Commit

Permalink
feat(cli): add Hatch library support (#152)
Browse files Browse the repository at this point in the history
* fix(hatch): lookup and update project bricks in default (force-include) and a polylith section (will require build hook)

* bump plugin to 1.14.3

* bump cli to 0.4.0
  • Loading branch information
DavidVujic authored Jan 19, 2024
1 parent bd447a1 commit 007a610
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 30 deletions.
11 changes: 7 additions & 4 deletions components/polylith/project/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def find_by_key(data: dict, key: str) -> Any:
return next((r for r in res if r), None)


def get_project_packages_from_polylith_section(data) -> dict:
bricks = data["tool"].get("polylith", {}).get("bricks")

return bricks if isinstance(bricks, dict) else {}


def get_hatch_project_packages(data) -> dict:
hatch_data = data["tool"]["hatch"]
build_data = hatch_data.get("build", {}) if isinstance(hatch_data, dict) else {}
Expand All @@ -33,10 +39,7 @@ def get_hatch_project_packages(data) -> dict:
if force_included:
return force_included

found = find_by_key(build_data, "polylith")
bricks = found.get("bricks", {}) if isinstance(found, dict) else {}

return bricks if isinstance(bricks, dict) else {}
return get_project_packages_from_polylith_section(data)


def get_project_package_includes(namespace: str, data) -> List[dict]:
Expand Down
33 changes: 23 additions & 10 deletions components/polylith/sync/update.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import reduce
from pathlib import Path
from typing import List, Union

Expand All @@ -19,23 +20,33 @@ def copy_toml_data(data: TOMLDocument) -> dict:
return copy


def generate_updated_hatch_project(
data: TOMLDocument, packages: List[dict]
) -> str:
def to_key_value_include(acc: dict, package: dict) -> dict:
brick = package["include"]
relative_path = package.get("from", "")
include = Path(relative_path, brick).as_posix()

return {**acc, **{include: brick}}


def generate_updated_hatch_project(data: TOMLDocument, packages: List[dict]) -> str:
bricks_to_add: dict = reduce(to_key_value_include, packages, {})

copy = copy_toml_data(data)

if copy["tool"].get("polylith", {}).get("bricks") is not None:
for k, v in bricks_to_add.items():
copy["tool"]["polylith"]["bricks"][k] = v

return tomlkit.dumps(copy)

if copy["tool"]["hatch"].get("build") is None:
copy["tool"]["hatch"].add("build", {"force-include": {}})

if copy["tool"]["hatch"]["build"].get("force-include") is None:
copy["tool"]["hatch"]["build"]["force-include"] = {}

for package in packages:
brick = package["include"]
relative_path = package.get("from", "")
include = Path(relative_path, brick).as_posix()

copy["tool"]["hatch"]["build"]["force-include"][include] = brick
for k, v in bricks_to_add.items():
copy["tool"]["hatch"]["build"]["force-include"][k] = v

return tomlkit.dumps(copy)

Expand All @@ -54,7 +65,9 @@ def generate_updated_poetry_project(data: TOMLDocument, packages: List[dict]) ->
return tomlkit.dumps(copy)


def generate_updated_project(data: TOMLDocument, packages: List[dict]) -> Union[str, None]:
def generate_updated_project(
data: TOMLDocument, packages: List[dict]
) -> Union[str, None]:
if repo.is_poetry(data):
return generate_updated_poetry_project(data, packages)

Expand Down
2 changes: 1 addition & 1 deletion projects/poetry_polylith_plugin/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "poetry-polylith-plugin"
version = "1.14.2"
version = "1.14.3"
description = "A Poetry plugin that adds tooling support for the Polylith Architecture"
authors = ["David Vujic"]
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Expand Down
2 changes: 1 addition & 1 deletion projects/polylith-cli/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "polylith-cli"
version = "0.3.0"
version = "0.4.0"
description = "Python tooling support for the Polylith Architecture"
authors = ['David Vujic']
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Expand Down
19 changes: 5 additions & 14 deletions test/components/polylith/project/test_project_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"""

hatch_toml_alternative = """\
[tool.hatch.build.hooks.targets.wheel.polylith.bricks]
[tool.hatch.build]
something = "something"
[tool.polylith.bricks]
"../../bases/unittest/one" = "unittest/one"
"../../components/unittest/two" = "unittest/two"
"""
Expand All @@ -28,7 +31,7 @@
"../../bases/unittest/one" = "unittest/one"
"../../components/unittest/two" = "unittest/two"
[tool.hatch.build.hooks.targets.wheel.polylith.bricks]
[tool.polylith.bricks]
"something" = "else"
"""

Expand Down Expand Up @@ -68,15 +71,3 @@ def test_get_hatch_package_includes_from_default_when_in_both():
res = project.get.get_project_package_includes(namespace, data)

assert res == expected


def test_get_hatch_package_includes_lookup_with_unexpected_format():
unexpected = """\
[tool.hatch.build.hooks.targets.wheel]
"polylith" = "this-is-unexpected"
"""
data = tomlkit.loads(unexpected)

res = project.get.get_project_package_includes(namespace, data)

assert res == []

0 comments on commit 007a610

Please sign in to comment.