From 08838dd06733503ffc8b0efb5559eb77dc1c2fa8 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Sun, 4 Feb 2024 22:09:58 +0100 Subject: [PATCH 01/14] wip(pdm-workspace): write tdd theme paths to the pth file --- bases/polylith/pdm_workspace_hooks/core.py | 12 ++++--- components/polylith/pdm/hooks/workspace.py | 38 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 components/polylith/pdm/hooks/workspace.py diff --git a/bases/polylith/pdm_workspace_hooks/core.py b/bases/polylith/pdm_workspace_hooks/core.py index d58cf3cc..095b5fe3 100644 --- a/bases/polylith/pdm_workspace_hooks/core.py +++ b/bases/polylith/pdm_workspace_hooks/core.py @@ -1,4 +1,6 @@ -import os +from pathlib import Path + +from polylith.pdm.hooks.workspace import build_initialize def pdm_build_initialize(context): @@ -8,8 +10,8 @@ def pdm_build_initialize(context): """ context.ensure_build_dir() - filepath = os.path.join(context.build_dir, "polylith_workspace.pth") - with open(filepath, "w") as f: - f.write(f"{context.config.root}/bases\n") - f.write(f"{context.config.root}/components\n") + build_dir = Path(context.build_dir) + root = Path(context.root) + + build_initialize(context.config, build_dir, root) diff --git a/components/polylith/pdm/hooks/workspace.py b/components/polylith/pdm/hooks/workspace.py new file mode 100644 index 00000000..c13b151c --- /dev/null +++ b/components/polylith/pdm/hooks/workspace.py @@ -0,0 +1,38 @@ +from pathlib import Path +from typing import Set + +from polylith import toml, workspace + +defaults = {"bases", "components"} + + +def paths_from_config(root: Path, config_data: dict) -> Set[str]: + ns = workspace.parser.get_namespace_from_config(root) + packages = toml.get_project_package_includes(ns, config_data) + + return {p["from"] for p in packages} + + +def parse_paths(root: Path, config_data: dict) -> Set[str]: + theme = workspace.parser.get_theme_from_config(root) + + paths = defaults if theme == "loose" else paths_from_config(root, config_data) + + return {(root / p).as_posix() for p in paths} + + +def write_pth_file(file_path: str, rows: Set[str]) -> None: + with open(file_path, "w") as f: + for row in rows: + f.write(f"{row}\n") + + +def build_initialize(config_data: dict, build_dir: Path, root: Path) -> None: + paths = parse_paths(root, config_data) + + if not paths: + return + + filepath = build_dir / "polylith_workspace.pth" + + write_pth_file(filepath.as_posix(), paths) From 46ab02f261b275c314cf406c2ce1328bc3a2f109 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Sun, 4 Feb 2024 23:00:54 +0100 Subject: [PATCH 02/14] wip(pdm-workspace): write tdd theme paths to the pth file --- components/polylith/pdm/hooks/workspace.py | 32 +++++++++---------- test/components/polylith/pdm/__init__.py | 0 .../polylith/pdm/test_workspace_hook.py | 31 ++++++++++++++++++ 3 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 test/components/polylith/pdm/__init__.py create mode 100644 test/components/polylith/pdm/test_workspace_hook.py diff --git a/components/polylith/pdm/hooks/workspace.py b/components/polylith/pdm/hooks/workspace.py index c13b151c..eae1586e 100644 --- a/components/polylith/pdm/hooks/workspace.py +++ b/components/polylith/pdm/hooks/workspace.py @@ -3,36 +3,36 @@ from polylith import toml, workspace -defaults = {"bases", "components"} - -def paths_from_config(root: Path, config_data: dict) -> Set[str]: - ns = workspace.parser.get_namespace_from_config(root) - packages = toml.get_project_package_includes(ns, config_data) +def paths_from_config(root: Path, ns: str, data: dict) -> Set[str]: + packages = toml.get_project_package_includes(ns, data) return {p["from"] for p in packages} -def parse_paths(root: Path, config_data: dict) -> Set[str]: - theme = workspace.parser.get_theme_from_config(root) +def parse_paths(root: Path, theme: str, ns: str, data: dict) -> Set[str]: + defaults = {"bases", "components"} - paths = defaults if theme == "loose" else paths_from_config(root, config_data) + paths = defaults if theme == "loose" else paths_from_config(root, ns, data) return {(root / p).as_posix() for p in paths} -def write_pth_file(file_path: str, rows: Set[str]) -> None: - with open(file_path, "w") as f: - for row in rows: - f.write(f"{row}\n") +def write_pth_file(build_dir: Path, paths: Set[str]) -> None: + filepath = build_dir / "polylith_workspace.pth" + + with open(filepath, "w") as f: + for p in paths: + f.write(f"{p}\n") def build_initialize(config_data: dict, build_dir: Path, root: Path) -> None: - paths = parse_paths(root, config_data) + theme = workspace.parser.get_theme_from_config(root) + ns = workspace.parser.get_namespace_from_config(root) + + paths = parse_paths(root, theme, ns, config_data) if not paths: return - filepath = build_dir / "polylith_workspace.pth" - - write_pth_file(filepath.as_posix(), paths) + write_pth_file(build_dir, paths) diff --git a/test/components/polylith/pdm/__init__.py b/test/components/polylith/pdm/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/components/polylith/pdm/test_workspace_hook.py b/test/components/polylith/pdm/test_workspace_hook.py new file mode 100644 index 00000000..4669114a --- /dev/null +++ b/test/components/polylith/pdm/test_workspace_hook.py @@ -0,0 +1,31 @@ +from polylith.pdm.hooks.workspace import parse_paths +from pathlib import Path +import tomlkit + + +root = "/some/path" +namespace = "my_namespace" + +toml_with_tdd_theme = f"""\ +[tool.polylith.bricks] +"bases/one/src/{namespace}/one" = "{namespace}/one" +"components/two/src/{namespace}/two" = "{namespace}/two" + +[build-system] +requires = ["pdm-backend"] +build-backend = "pdm.backend" +""" + + +def test_parse_paths_for_loose_theme(): + res = parse_paths(Path(root), "loose", namespace, {}) + + assert res == {f"{root}/bases", f"{root}/components"} + + +def test_parse_paths_for_tdd_theme(): + data = tomlkit.loads(toml_with_tdd_theme) + + res = parse_paths(Path(root), "tdd", namespace, data) + + assert res == {f"{root}/bases/one/src", f"{root}/components/two/src"} From 4ea26c4aebec8d9dde375e8ed242de38926e15b3 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Sun, 4 Feb 2024 23:15:20 +0100 Subject: [PATCH 03/14] wip(pdm-workspace): write tdd theme paths to the pth file --- projects/pdm_polylith_bricks/pyproject.toml | 5 +++++ projects/pdm_polylith_workspace/poetry.lock | 14 ++++++++++++-- projects/pdm_polylith_workspace/pyproject.toml | 14 +++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/projects/pdm_polylith_bricks/pyproject.toml b/projects/pdm_polylith_bricks/pyproject.toml index f899c0b0..2ce8190a 100644 --- a/projects/pdm_polylith_bricks/pyproject.toml +++ b/projects/pdm_polylith_bricks/pyproject.toml @@ -10,10 +10,15 @@ readme = "README.md" packages = [ {include = "polylith/pdm_project_hooks", from = "../../bases"}, + {include = "polylith/development",from = "../../components"}, + {include = "polylith/dirs",from = "../../components"}, + {include = "polylith/files",from = "../../components"}, {include = "polylith/parsing",from = "../../components"}, {include = "polylith/pdm",from = "../../components"}, + {include = "polylith/readme",from = "../../components"}, {include = "polylith/repo",from = "../../components"}, {include = "polylith/toml",from = "../../components"}, + {include = "polylith/workspace",from = "../../components"}, ] [tool.poetry.dependencies] diff --git a/projects/pdm_polylith_workspace/poetry.lock b/projects/pdm_polylith_workspace/poetry.lock index 52548aa4..d89d0459 100644 --- a/projects/pdm_polylith_workspace/poetry.lock +++ b/projects/pdm_polylith_workspace/poetry.lock @@ -1,7 +1,17 @@ # This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. -package = [] + +[[package]] +name = "tomlkit" +version = "0.11.8" +description = "Style preserving TOML library" +optional = false +python-versions = ">=3.7" +files = [ + {file = "tomlkit-0.11.8-py3-none-any.whl", hash = "sha256:8c726c4c202bdb148667835f68d68780b9a003a9ec34167b6c673b38eff2a171"}, + {file = "tomlkit-0.11.8.tar.gz", hash = "sha256:9330fc7faa1db67b541b28e62018c17d20be733177d290a13b24c62d1614e0c3"}, +] [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "c595a0588c25d58f3e3834ad7169126836d262b925fe6ca9b5d540dcf301d254" +content-hash = "0d1c25c8e15e0ca2a5cc97fce19b1c931a1fc9aad76a3a3df58d02504fee156f" diff --git a/projects/pdm_polylith_workspace/pyproject.toml b/projects/pdm_polylith_workspace/pyproject.toml index 53c0d783..1390ba77 100644 --- a/projects/pdm_polylith_workspace/pyproject.toml +++ b/projects/pdm_polylith_workspace/pyproject.toml @@ -8,10 +8,22 @@ authors = ["David Vujic"] license = "MIT" readme = "README.md" -packages = [{include = "polylith/pdm_workspace_hooks", from = "../../bases"}] +packages = [ + {include = "polylith/pdm_workspace_hooks", from = "../../bases"}, + {include = "polylith/development",from = "../../components"}, + {include = "polylith/dirs",from = "../../components"}, + {include = "polylith/files",from = "../../components"}, + {include = "polylith/parsing",from = "../../components"}, + {include = "polylith/pdm",from = "../../components"}, + {include = "polylith/readme",from = "../../components"}, + {include = "polylith/repo",from = "../../components"}, + {include = "polylith/toml",from = "../../components"}, + {include = "polylith/workspace",from = "../../components"}, +] [tool.poetry.dependencies] python = "^3.9" +tomlkit = "^0.11.5" [tool.poetry.plugins."pdm.build.hook"] polylith-workspace = "pdm_polylith_workspace.polylith.pdm_workspace_hooks.core" From e1d3636e44cae6d64209dab2e25849b37d9d16b3 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Mon, 5 Feb 2024 10:55:10 +0100 Subject: [PATCH 04/14] refactor(workspace): move parser code to a separate configuration component --- bases/polylith/cli/core.py | 8 +++---- components/polylith/bricks/brick.py | 8 +++---- components/polylith/bricks/component.py | 4 ++-- components/polylith/commands/create.py | 5 +++-- components/polylith/commands/diff.py | 6 +++--- components/polylith/commands/info.py | 4 ++-- components/polylith/configuration/__init__.py | 21 +++++++++++++++++++ .../parser.py => configuration/core.py} | 0 components/polylith/diff/collect.py | 4 ++-- components/polylith/libs/grouping.py | 4 ++-- components/polylith/pdm/hooks/workspace.py | 6 +++--- components/polylith/poetry/commands/check.py | 4 ++-- components/polylith/poetry/commands/libs.py | 4 ++-- components/polylith/poetry/commands/sync.py | 4 ++-- components/polylith/project/get.py | 4 ++-- components/polylith/sync/update.py | 4 ++-- components/polylith/test/tests.py | 6 +++--- components/polylith/workspace/__init__.py | 4 ++-- components/polylith/workspace/paths.py | 5 ++--- development/david.py | 16 ++++++++++++-- projects/pdm_polylith_bricks/pyproject.toml | 6 +----- .../pdm_polylith_workspace/pyproject.toml | 6 +----- .../poetry_polylith_plugin/pyproject.toml | 1 + projects/polylith_cli/pyproject.toml | 1 + pyproject.toml | 1 + 25 files changed, 82 insertions(+), 54 deletions(-) create mode 100644 components/polylith/configuration/__init__.py rename components/polylith/{workspace/parser.py => configuration/core.py} (100%) diff --git a/bases/polylith/cli/core.py b/bases/polylith/cli/core.py index d97d825d..1fb5c4d3 100644 --- a/bases/polylith/cli/core.py +++ b/bases/polylith/cli/core.py @@ -1,6 +1,6 @@ from pathlib import Path -from polylith import commands, info, repo, workspace +from polylith import commands, configuration, info, repo from polylith.cli import create, options from typer import Exit, Option, Typer from typing_extensions import Annotated @@ -30,7 +30,7 @@ def check_command( ): """Validates the Polylith workspace.""" root = repo.get_workspace_root(Path.cwd()) - ns = workspace.parser.get_namespace_from_config(root) + ns = configuration.get_namespace_from_config(root) all_projects_data = info.get_projects_data(root, ns) only_projects_data = [p for p in all_projects_data if info.is_project(p)] @@ -68,7 +68,7 @@ def libs_command( ): """Show third-party libraries used in the workspace.""" root = repo.get_workspace_root(Path.cwd()) - ns = workspace.parser.get_namespace_from_config(root) + ns = configuration.get_namespace_from_config(root) projects_data = info.get_projects_data(root, ns) @@ -94,7 +94,7 @@ def sync_command( ): """Update pyproject.toml with missing bricks.""" root = repo.get_workspace_root(Path.cwd()) - ns = workspace.parser.get_namespace_from_config(root) + ns = configuration.get_namespace_from_config(root) projects_data = info.get_projects_data(root, ns) diff --git a/components/polylith/bricks/brick.py b/components/polylith/bricks/brick.py index 597fdbe4..f3da7dce 100644 --- a/components/polylith/bricks/brick.py +++ b/components/polylith/bricks/brick.py @@ -1,10 +1,10 @@ from pathlib import Path +from polylith import configuration from polylith.dirs import create_dir from polylith.files import create_file from polylith.interface import create_interface from polylith.readme import create_brick_readme -from polylith.workspace import parser def create_brick(root: Path, options: dict) -> None: @@ -13,8 +13,8 @@ def create_brick(root: Path, options: dict) -> None: k: v for k, v in options.items() if k in {"brick", "namespace", "package"} } - brick_structure = parser.get_brick_structure_from_config(root) - resources_structure = parser.get_resources_structure_from_config(root) + brick_structure = configuration.get_brick_structure_from_config(root) + resources_structure = configuration.get_resources_structure_from_config(root) brick_path = brick_structure.format(**path_kwargs) resources_path = resources_structure.format(**path_kwargs) @@ -23,5 +23,5 @@ def create_brick(root: Path, options: dict) -> None: create_file(d, f"{modulename}.py") create_interface(d, options) - if parser.is_readme_generation_enabled(root): + if configuration.is_readme_generation_enabled(root): create_brick_readme(root / resources_path, options) diff --git a/components/polylith/bricks/component.py b/components/polylith/bricks/component.py index deed2078..128602f2 100644 --- a/components/polylith/bricks/component.py +++ b/components/polylith/bricks/component.py @@ -1,7 +1,7 @@ from pathlib import Path from typing import List -from polylith import workspace +from polylith import configuration from polylith.bricks.brick import create_brick from polylith.repo import components_dir from polylith.test import create_test @@ -20,7 +20,7 @@ def is_brick_dir(p: Path) -> bool: def get_component_dirs(root: Path, top_dir, ns) -> list: - theme = workspace.parser.get_theme_from_config(root) + theme = configuration.get_theme_from_config(root) dirs = top_dir if theme == "tdd" else f"{top_dir}/{ns}" component_dir = root / dirs diff --git a/components/polylith/commands/create.py b/components/polylith/commands/create.py index a43973c7..5c0bb401 100644 --- a/components/polylith/commands/create.py +++ b/components/polylith/commands/create.py @@ -1,11 +1,12 @@ from pathlib import Path from typing import Union -from polylith import repo, workspace + +from polylith import configuration, repo def create(name: Union[str, None], description: Union[str, None], fn): root = repo.get_workspace_root(Path.cwd()) - namespace = workspace.parser.get_namespace_from_config(root) + namespace = configuration.get_namespace_from_config(root) if not name: raise ValueError("Please add a name by using --name") diff --git a/components/polylith/commands/diff.py b/components/polylith/commands/diff.py index 766b17b2..478223dd 100644 --- a/components/polylith/commands/diff.py +++ b/components/polylith/commands/diff.py @@ -1,11 +1,11 @@ from pathlib import Path - from typing import Union -from polylith import diff, info, repo, workspace + +from polylith import configuration, diff, info, repo def print_views(root: Path, tag: str, short: bool, only_bricks: bool) -> None: - ns = workspace.parser.get_namespace_from_config(root) + ns = configuration.get_namespace_from_config(root) files = diff.collect.get_files(tag) bases = diff.collect.get_changed_bases(files, ns) components = diff.collect.get_changed_components(files, ns) diff --git a/components/polylith/commands/info.py b/components/polylith/commands/info.py index a41fd931..85baef2e 100644 --- a/components/polylith/commands/info.py +++ b/components/polylith/commands/info.py @@ -1,12 +1,12 @@ from pathlib import Path -from polylith import info, repo, workspace +from polylith import configuration, info, repo def run(short: bool): root = repo.get_workspace_root(Path.cwd()) - ns = workspace.parser.get_namespace_from_config(root) + ns = configuration.get_namespace_from_config(root) bases = info.get_bases(root, ns) components = info.get_components(root, ns) projects_data = info.get_bricks_in_projects(root, components, bases, ns) diff --git a/components/polylith/configuration/__init__.py b/components/polylith/configuration/__init__.py new file mode 100644 index 00000000..9bbcaa45 --- /dev/null +++ b/components/polylith/configuration/__init__.py @@ -0,0 +1,21 @@ +from polylith.configuration.core import ( + get_brick_structure_from_config, + get_namespace_from_config, + get_resources_structure_from_config, + get_tag_pattern_from_config, + get_tests_structure_from_config, + get_theme_from_config, + is_readme_generation_enabled, + is_test_generation_enabled, +) + +__all__ = [ + "get_brick_structure_from_config", + "get_namespace_from_config", + "get_resources_structure_from_config", + "get_tag_pattern_from_config", + "get_tests_structure_from_config", + "get_theme_from_config", + "is_readme_generation_enabled", + "is_test_generation_enabled", +] diff --git a/components/polylith/workspace/parser.py b/components/polylith/configuration/core.py similarity index 100% rename from components/polylith/workspace/parser.py rename to components/polylith/configuration/core.py diff --git a/components/polylith/diff/collect.py b/components/polylith/diff/collect.py index 2257054a..e3e79de0 100644 --- a/components/polylith/diff/collect.py +++ b/components/polylith/diff/collect.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import List, Set, Union -from polylith import repo, workspace +from polylith import configuration, repo def _parse_folder_parts(folder: str, changed_file: Path) -> str: @@ -42,7 +42,7 @@ def get_changed_projects(changed_files: List[Path]) -> list: def get_latest_tag(root: Path, key: Union[str, None]) -> Union[str, None]: - tag_pattern = workspace.parser.get_tag_pattern_from_config(root, key) + tag_pattern = configuration.get_tag_pattern_from_config(root, key) res = subprocess.run( ["git", "tag", "-l", "--sort=-committerdate", f"{tag_pattern}"], diff --git a/components/polylith/libs/grouping.py b/components/polylith/libs/grouping.py index f0db7b90..4b563210 100644 --- a/components/polylith/libs/grouping.py +++ b/components/polylith/libs/grouping.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import Set -from polylith import workspace +from polylith import configuration from polylith.imports import extract_top_ns, fetch_all_imports from polylith.libs.stdlib import standard_libs @@ -42,7 +42,7 @@ def extract_third_party_imports(all_imports: dict, top_ns: str) -> dict: def get_third_party_imports(root: Path, paths: Set[Path]) -> dict: - top_ns = workspace.parser.get_namespace_from_config(root) + top_ns = configuration.get_namespace_from_config(root) all_imports = fetch_all_imports(paths) diff --git a/components/polylith/pdm/hooks/workspace.py b/components/polylith/pdm/hooks/workspace.py index eae1586e..9da1d182 100644 --- a/components/polylith/pdm/hooks/workspace.py +++ b/components/polylith/pdm/hooks/workspace.py @@ -1,7 +1,7 @@ from pathlib import Path from typing import Set -from polylith import toml, workspace +from polylith import configuration, toml def paths_from_config(root: Path, ns: str, data: dict) -> Set[str]: @@ -27,8 +27,8 @@ def write_pth_file(build_dir: Path, paths: Set[str]) -> None: def build_initialize(config_data: dict, build_dir: Path, root: Path) -> None: - theme = workspace.parser.get_theme_from_config(root) - ns = workspace.parser.get_namespace_from_config(root) + theme = configuration.get_theme_from_config(root) + ns = configuration.get_namespace_from_config(root) paths = parse_paths(root, theme, ns, config_data) diff --git a/components/polylith/poetry/commands/check.py b/components/polylith/poetry/commands/check.py index f503ca27..07886450 100644 --- a/components/polylith/poetry/commands/check.py +++ b/components/polylith/poetry/commands/check.py @@ -2,7 +2,7 @@ from cleo.helpers import option from poetry.console.commands.command import Command -from polylith import commands, info, repo, workspace +from polylith import commands, configuration, info, repo from polylith.poetry import internals command_options = [ @@ -52,7 +52,7 @@ def print_report(self, root: Path, ns: str, project_data: dict) -> bool: def handle(self) -> int: directory = self.option("directory") root = repo.get_workspace_root(Path.cwd()) - ns = workspace.parser.get_namespace_from_config(root) + ns = configuration.get_namespace_from_config(root) all_projects_data = info.get_projects_data(root, ns) only_projects_data = [p for p in all_projects_data if info.is_project(p)] diff --git a/components/polylith/poetry/commands/libs.py b/components/polylith/poetry/commands/libs.py index 97ba70e5..b63182f0 100644 --- a/components/polylith/poetry/commands/libs.py +++ b/components/polylith/poetry/commands/libs.py @@ -1,7 +1,7 @@ from pathlib import Path from poetry.console.commands.command import Command -from polylith import commands, info, repo, workspace +from polylith import commands, configuration, info, repo from polylith.poetry.commands.check import command_options from polylith.poetry.internals import filter_projects_data, find_third_party_libs @@ -33,7 +33,7 @@ def print_report(self, root: Path, ns: str, data: dict) -> bool: def handle(self) -> int: directory = self.option("directory") root = repo.get_workspace_root(Path.cwd()) - ns = workspace.parser.get_namespace_from_config(root) + ns = configuration.get_namespace_from_config(root) all_projects_data = info.get_projects_data(root, ns) projects_data = filter_projects_data(self.poetry, directory, all_projects_data) diff --git a/components/polylith/poetry/commands/sync.py b/components/polylith/poetry/commands/sync.py index e2c6c1c7..a44434a7 100644 --- a/components/polylith/poetry/commands/sync.py +++ b/components/polylith/poetry/commands/sync.py @@ -1,7 +1,7 @@ from pathlib import Path from poetry.console.commands.command import Command -from polylith import commands, info, repo, workspace +from polylith import commands, configuration, info, repo from polylith.poetry.internals import filter_projects_data @@ -12,7 +12,7 @@ class SyncCommand(Command): def handle(self) -> int: directory = self.option("directory") root = repo.get_workspace_root(Path.cwd()) - ns = workspace.parser.get_namespace_from_config(root) + ns = configuration.get_namespace_from_config(root) all_projects_data = info.get_projects_data(root, ns) projects_data = filter_projects_data(self.poetry, directory, all_projects_data) diff --git a/components/polylith/project/get.py b/components/polylith/project/get.py index c770089b..8cb4b3b3 100644 --- a/components/polylith/project/get.py +++ b/components/polylith/project/get.py @@ -3,7 +3,7 @@ from typing import List import tomlkit -from polylith import repo, toml, workspace +from polylith import configuration, repo, toml def get_project_name(data) -> str: @@ -43,7 +43,7 @@ def get_toml_files(root: Path) -> List[dict]: def get_packages_for_projects(root: Path) -> List[dict]: toml_files = get_toml_files(root) - namespace = workspace.parser.get_namespace_from_config(root) + namespace = configuration.get_namespace_from_config(root) return [ { diff --git a/components/polylith/sync/update.py b/components/polylith/sync/update.py index 92524b30..a6e5c375 100644 --- a/components/polylith/sync/update.py +++ b/components/polylith/sync/update.py @@ -3,7 +3,7 @@ from typing import List, Union import tomlkit -from polylith import project, repo, workspace +from polylith import configuration, project, repo from tomlkit.toml_document import TOMLDocument @@ -101,7 +101,7 @@ def generate_updated_project( def to_packages(root: Path, namespace: str, diff: dict) -> List[dict]: - theme = workspace.parser.get_theme_from_config(root) + theme = configuration.get_theme_from_config(root) is_project = diff["is_project"] diff --git a/components/polylith/test/tests.py b/components/polylith/test/tests.py index 475fa46a..ca71b418 100644 --- a/components/polylith/test/tests.py +++ b/components/polylith/test/tests.py @@ -2,7 +2,7 @@ from polylith.dirs import create_dir from polylith.files import create_file -from polylith.workspace import parser +from polylith import configuration template = """\ from {namespace}.{package} import {modulename} @@ -14,7 +14,7 @@ def test_sample(): def create_test(root: Path, options: dict) -> None: - if not parser.is_test_generation_enabled(root): + if not configuration.is_test_generation_enabled(root): return brick = options["brick"] @@ -22,7 +22,7 @@ def create_test(root: Path, options: dict) -> None: package = options["package"] modulename = options["modulename"] - dirs_structure = parser.get_tests_structure_from_config(root) + dirs_structure = configuration.get_tests_structure_from_config(root) dirs = dirs_structure.format(brick=brick, namespace=namespace, package=package) d = create_dir(root, dirs) diff --git a/components/polylith/workspace/__init__.py b/components/polylith/workspace/__init__.py index 19aea7eb..f903ad53 100644 --- a/components/polylith/workspace/__init__.py +++ b/components/polylith/workspace/__init__.py @@ -1,3 +1,3 @@ -from polylith.workspace import create, parser, paths +from polylith.workspace import create, paths -__all__ = ["create", "parser", "paths"] +__all__ = ["create", "paths"] diff --git a/components/polylith/workspace/paths.py b/components/polylith/workspace/paths.py index d910748a..a33f62a0 100644 --- a/components/polylith/workspace/paths.py +++ b/components/polylith/workspace/paths.py @@ -1,8 +1,7 @@ from pathlib import Path from typing import Set -from polylith import repo -from polylith.workspace import parser +from polylith import configuration, repo def get_path(structure: str, brick: str, ns: str, package: str) -> str: @@ -14,7 +13,7 @@ def get_paths(structure: str, brick: str, ns: str, packages: Set[str]) -> Set[st def collect_paths(root: Path, ns: str, brick: str, packages: Set[str]) -> Set[Path]: - structure = parser.get_brick_structure_from_config(root) + structure = configuration.get_brick_structure_from_config(root) paths = get_paths(structure, brick, ns, packages) diff --git a/development/david.py b/development/david.py index a0821834..fb625d2d 100644 --- a/development/david.py +++ b/development/david.py @@ -1,19 +1,31 @@ from pathlib import Path from polylith import ( + alias, bricks, + check, + commands, + configuration, development, diff, dirs, + distributions, files, + hatch, + imports, info, interface, + libs, + parsing, + pdm, poetry, - poetry_plugin, project, readme, repo, + reporting, + sync, test, + toml, workspace, ) @@ -23,7 +35,7 @@ print(repo.projects_dir) root = Path.cwd() -ns = workspace.parser.get_namespace_from_config(root) +ns = configuration.get_namespace_from_config(root) tag = diff.collect.get_latest_tag(root, "release") or "" diff --git a/projects/pdm_polylith_bricks/pyproject.toml b/projects/pdm_polylith_bricks/pyproject.toml index 2ce8190a..027392f5 100644 --- a/projects/pdm_polylith_bricks/pyproject.toml +++ b/projects/pdm_polylith_bricks/pyproject.toml @@ -10,15 +10,11 @@ readme = "README.md" packages = [ {include = "polylith/pdm_project_hooks", from = "../../bases"}, - {include = "polylith/development",from = "../../components"}, - {include = "polylith/dirs",from = "../../components"}, - {include = "polylith/files",from = "../../components"}, + {include = "polylith/configuration",from = "../../components"}, {include = "polylith/parsing",from = "../../components"}, {include = "polylith/pdm",from = "../../components"}, - {include = "polylith/readme",from = "../../components"}, {include = "polylith/repo",from = "../../components"}, {include = "polylith/toml",from = "../../components"}, - {include = "polylith/workspace",from = "../../components"}, ] [tool.poetry.dependencies] diff --git a/projects/pdm_polylith_workspace/pyproject.toml b/projects/pdm_polylith_workspace/pyproject.toml index 1390ba77..e2a610d4 100644 --- a/projects/pdm_polylith_workspace/pyproject.toml +++ b/projects/pdm_polylith_workspace/pyproject.toml @@ -10,15 +10,11 @@ readme = "README.md" packages = [ {include = "polylith/pdm_workspace_hooks", from = "../../bases"}, - {include = "polylith/development",from = "../../components"}, - {include = "polylith/dirs",from = "../../components"}, - {include = "polylith/files",from = "../../components"}, + {include = "polylith/configuration",from = "../../components"}, {include = "polylith/parsing",from = "../../components"}, {include = "polylith/pdm",from = "../../components"}, - {include = "polylith/readme",from = "../../components"}, {include = "polylith/repo",from = "../../components"}, {include = "polylith/toml",from = "../../components"}, - {include = "polylith/workspace",from = "../../components"}, ] [tool.poetry.dependencies] diff --git a/projects/poetry_polylith_plugin/pyproject.toml b/projects/poetry_polylith_plugin/pyproject.toml index 84f8aab9..ec649f1c 100644 --- a/projects/poetry_polylith_plugin/pyproject.toml +++ b/projects/poetry_polylith_plugin/pyproject.toml @@ -14,6 +14,7 @@ packages = [ {include = "polylith/bricks",from = "../../components"}, {include = "polylith/check",from = "../../components"}, {include = "polylith/commands",from = "../../components"}, + {include = "polylith/configuration",from = "../../components"}, {include = "polylith/development",from = "../../components"}, {include = "polylith/diff",from = "../../components"}, {include = "polylith/dirs",from = "../../components"}, diff --git a/projects/polylith_cli/pyproject.toml b/projects/polylith_cli/pyproject.toml index 9f3fadaf..8e7494d1 100644 --- a/projects/polylith_cli/pyproject.toml +++ b/projects/polylith_cli/pyproject.toml @@ -14,6 +14,7 @@ packages = [ {include = "polylith/bricks",from = "../../components"}, {include = "polylith/check",from = "../../components"}, {include = "polylith/commands",from = "../../components"}, + {include = "polylith/configuration",from = "../../components"}, {include = "polylith/development",from = "../../components"}, {include = "polylith/diff",from = "../../components"}, {include = "polylith/dirs",from = "../../components"}, diff --git a/pyproject.toml b/pyproject.toml index 5b420396..eacca149 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,6 +38,7 @@ packages = [ {include = "polylith/toml",from = "components"}, {include = "polylith/workspace",from = "components"}, {include = "development"}, + {include = "polylith/configuration",from = "components"}, ] [tool.poetry.dependencies] From a38deba38d66da47b74f142355836e5e4083f92b Mon Sep 17 00:00:00 2001 From: David Vujic Date: Mon, 5 Feb 2024 10:57:37 +0100 Subject: [PATCH 05/14] refactor(workspace): move parser code to a separate configuration component --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index eacca149..0dbe6c73 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ packages = [ {include = "polylith/alias",from = "components"}, {include = "polylith/bricks",from = "components"}, {include = "polylith/check",from = "components"}, + {include = "polylith/configuration",from = "components"}, {include = "polylith/commands",from = "components"}, {include = "polylith/development",from = "components"}, {include = "polylith/diff",from = "components"}, @@ -38,7 +39,6 @@ packages = [ {include = "polylith/toml",from = "components"}, {include = "polylith/workspace",from = "components"}, {include = "development"}, - {include = "polylith/configuration",from = "components"}, ] [tool.poetry.dependencies] From ec63bd206dbb2b094152e1027fb590bb7c7fdda1 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Mon, 5 Feb 2024 11:00:33 +0100 Subject: [PATCH 06/14] wip(pdm-workspace): write tdd theme paths to the pth file --- components/polylith/pdm/hooks/workspace.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/polylith/pdm/hooks/workspace.py b/components/polylith/pdm/hooks/workspace.py index 9da1d182..65e43409 100644 --- a/components/polylith/pdm/hooks/workspace.py +++ b/components/polylith/pdm/hooks/workspace.py @@ -4,7 +4,7 @@ from polylith import configuration, toml -def paths_from_config(root: Path, ns: str, data: dict) -> Set[str]: +def paths_from_config(ns: str, data: dict) -> Set[str]: packages = toml.get_project_package_includes(ns, data) return {p["from"] for p in packages} @@ -13,7 +13,7 @@ def paths_from_config(root: Path, ns: str, data: dict) -> Set[str]: def parse_paths(root: Path, theme: str, ns: str, data: dict) -> Set[str]: defaults = {"bases", "components"} - paths = defaults if theme == "loose" else paths_from_config(root, ns, data) + paths = defaults if theme == "loose" else paths_from_config(ns, data) return {(root / p).as_posix() for p in paths} From 59416e8b5a152d45a859d674808929a1a67e0a3d Mon Sep 17 00:00:00 2001 From: David Vujic Date: Mon, 5 Feb 2024 13:31:09 +0100 Subject: [PATCH 07/14] refactor(workspace): move parser code to a separate configuration component --- .../polylith/configuration/__init__.py | 0 .../polylith/configuration/test_core.py | 101 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 test/components/polylith/configuration/__init__.py create mode 100644 test/components/polylith/configuration/test_core.py diff --git a/test/components/polylith/configuration/__init__.py b/test/components/polylith/configuration/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/components/polylith/configuration/test_core.py b/test/components/polylith/configuration/test_core.py new file mode 100644 index 00000000..8775143f --- /dev/null +++ b/test/components/polylith/configuration/test_core.py @@ -0,0 +1,101 @@ +from pathlib import Path +import pytest + +import tomlkit +from polylith.configuration import core + +config_template = """ +[tool.polylith] +namespace = "my_namespace" + +[tool.polylith.structure] +theme = "{theme}" + +[tool.polylith.tag.patterns] +stable = "stable-*" +release = "v[0-9]*" + +[tool.polylith.test] +enabled = true +""" + + +@pytest.fixture +def use_fake(monkeypatch): + def patch(theme: str): + config = config_template.format(theme=theme) + name = "_load_workspace_config" + + monkeypatch.setattr(core, name, lambda *args: tomlkit.loads(config)) + + return patch + + +@pytest.fixture +def use_loose(use_fake): + use_fake(theme="loose") + + +@pytest.fixture +def use_tdd(use_fake): + use_fake(theme="tdd") + + +def test_get_namespace(use_loose): + res = core.get_namespace_from_config(Path.cwd()) + + assert res == "my_namespace" + + +def test_get_tag_pattern(use_loose): + path = Path.cwd() + + stable = core.get_tag_pattern_from_config(path, "stable") + release = core.get_tag_pattern_from_config(path, "release") + + assert stable == "stable-*" + assert release == "v[0-9]*" + + +def test_is_test_generation_enabled(use_loose): + res = core.is_test_generation_enabled(Path.cwd()) + + assert res is True + + +def test_is_readme_generation_enabled(use_loose): + res = core.is_readme_generation_enabled(Path.cwd()) + + assert res is False + + +def test_get_theme(use_loose): + res = core.get_theme_from_config(Path.cwd()) + + assert res == "loose" + + +def test_get_structure_for_loose_theme(use_loose): + path = Path.cwd() + + brick_structure = core.get_brick_structure_from_config(path) + test_structure = core.get_tests_structure_from_config(path) + resources_structure = core.get_resources_structure_from_config(path) + + expected = "{brick}/{namespace}/{package}" + + assert brick_structure == expected + assert test_structure == f"test/{expected}" + assert resources_structure == expected + + +def test_get_structure_for_tdd_theme(use_tdd): + path = Path.cwd() + + brick_structure = core.get_brick_structure_from_config(path) + test_structure = core.get_tests_structure_from_config(path) + resources_structure = core.get_resources_structure_from_config(path) + + assert brick_structure == "{brick}/{package}/src/{namespace}/{package}" + assert test_structure == "{brick}/{package}/test/{namespace}/{package}" + assert resources_structure == "{brick}/{package}" From 386ecb5f6502c451a861255ca95a9939f8a0d967 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Mon, 5 Feb 2024 13:44:58 +0100 Subject: [PATCH 08/14] refactor(workspace): move parser code to a separate configuration component --- .../polylith/configuration/test_core.py | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/test/components/polylith/configuration/test_core.py b/test/components/polylith/configuration/test_core.py index 8775143f..3b2643fe 100644 --- a/test/components/polylith/configuration/test_core.py +++ b/test/components/polylith/configuration/test_core.py @@ -1,6 +1,7 @@ from pathlib import Path -import pytest +from typing import Tuple +import pytest import tomlkit from polylith.configuration import core @@ -41,61 +42,62 @@ def use_tdd(use_fake): use_fake(theme="tdd") +fake_path = Path.cwd() + + def test_get_namespace(use_loose): - res = core.get_namespace_from_config(Path.cwd()) + res = core.get_namespace_from_config(fake_path) assert res == "my_namespace" def test_get_tag_pattern(use_loose): - path = Path.cwd() - - stable = core.get_tag_pattern_from_config(path, "stable") - release = core.get_tag_pattern_from_config(path, "release") + stable = core.get_tag_pattern_from_config(fake_path, "stable") + release = core.get_tag_pattern_from_config(fake_path, "release") assert stable == "stable-*" assert release == "v[0-9]*" def test_is_test_generation_enabled(use_loose): - res = core.is_test_generation_enabled(Path.cwd()) + res = core.is_test_generation_enabled(fake_path) assert res is True def test_is_readme_generation_enabled(use_loose): - res = core.is_readme_generation_enabled(Path.cwd()) + res = core.is_readme_generation_enabled(fake_path) assert res is False def test_get_theme(use_loose): - res = core.get_theme_from_config(Path.cwd()) + res = core.get_theme_from_config(fake_path) assert res == "loose" -def test_get_structure_for_loose_theme(use_loose): - path = Path.cwd() +def _get_structure(path: Path) -> Tuple[str, str, str]: + brick = core.get_brick_structure_from_config(path) + test = core.get_tests_structure_from_config(path) + resources = core.get_resources_structure_from_config(path) + + return brick, test, resources - brick_structure = core.get_brick_structure_from_config(path) - test_structure = core.get_tests_structure_from_config(path) - resources_structure = core.get_resources_structure_from_config(path) + +def test_get_structure_for_loose_theme(use_loose): + brick, test, resources = _get_structure(fake_path) expected = "{brick}/{namespace}/{package}" - assert brick_structure == expected - assert test_structure == f"test/{expected}" - assert resources_structure == expected + assert brick == expected + assert test == f"test/{expected}" + assert resources == expected def test_get_structure_for_tdd_theme(use_tdd): - path = Path.cwd() - - brick_structure = core.get_brick_structure_from_config(path) - test_structure = core.get_tests_structure_from_config(path) - resources_structure = core.get_resources_structure_from_config(path) + brick, test, resources = _get_structure(fake_path) - assert brick_structure == "{brick}/{package}/src/{namespace}/{package}" - assert test_structure == "{brick}/{package}/test/{namespace}/{package}" - assert resources_structure == "{brick}/{package}" + assert brick == "{brick}/{package}/src/{namespace}/{package}" + assert test == "{brick}/{package}/test/{namespace}/{package}" + assert resources == "{brick}/{package}" From a9c08b9cff5cec0dfc238e3cb4e9c1b319a689a8 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Mon, 5 Feb 2024 13:57:32 +0100 Subject: [PATCH 09/14] bump pdm bricks hook to 0.1.2 --- projects/pdm_polylith_bricks/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/pdm_polylith_bricks/pyproject.toml b/projects/pdm_polylith_bricks/pyproject.toml index 027392f5..6c81758b 100644 --- a/projects/pdm_polylith_bricks/pyproject.toml +++ b/projects/pdm_polylith_bricks/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pdm-polylith-bricks" -version = "0.1.1" +version = "0.1.2" description = "a PDM build hook for Polylith" authors = ["David Vujic"] homepage = "https://davidvujic.github.io/python-polylith-docs/" From bd463f6420239bf181e6c71ce10b366500614224 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Mon, 5 Feb 2024 13:58:29 +0100 Subject: [PATCH 10/14] bump pdm workspace hook to 0.1.1 --- projects/pdm_polylith_workspace/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/pdm_polylith_workspace/pyproject.toml b/projects/pdm_polylith_workspace/pyproject.toml index e2a610d4..024670b1 100644 --- a/projects/pdm_polylith_workspace/pyproject.toml +++ b/projects/pdm_polylith_workspace/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pdm-polylith-workspace" -version = "0.1.0" +version = "0.1.1" description = "a PDM build hook for a Polylith workspace" homepage = "https://davidvujic.github.io/python-polylith-docs/" repository = "https://github.com/davidvujic/python-polylith" From fe6adee7aeea52b1d46000db16af985ceaecc887 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Mon, 5 Feb 2024 13:58:59 +0100 Subject: [PATCH 11/14] bump Poetry plugin to 1.15.1 --- projects/poetry_polylith_plugin/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/poetry_polylith_plugin/pyproject.toml b/projects/poetry_polylith_plugin/pyproject.toml index ec649f1c..c9715e55 100644 --- a/projects/poetry_polylith_plugin/pyproject.toml +++ b/projects/poetry_polylith_plugin/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "poetry-polylith-plugin" -version = "1.15.0" +version = "1.15.1" description = "A Poetry plugin that adds tooling support for the Polylith Architecture" authors = ["David Vujic"] homepage = "https://davidvujic.github.io/python-polylith-docs/" From 82b37bb9d92266f4e17ca681b0b3704978a722a8 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Mon, 5 Feb 2024 13:59:22 +0100 Subject: [PATCH 12/14] bump CLI to 1.2.1 --- projects/polylith_cli/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/polylith_cli/pyproject.toml b/projects/polylith_cli/pyproject.toml index 8e7494d1..424c0884 100644 --- a/projects/polylith_cli/pyproject.toml +++ b/projects/polylith_cli/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "polylith-cli" -version = "1.2.0" +version = "1.2.1" description = "Python tooling support for the Polylith Architecture" authors = ['David Vujic'] homepage = "https://davidvujic.github.io/python-polylith-docs/" From ac6a67a25201195038a5bb2a7274b92231582078 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Mon, 5 Feb 2024 14:07:37 +0100 Subject: [PATCH 13/14] wip(pdm-workspace): write tdd theme paths to the pth file --- bases/polylith/pdm_workspace_hooks/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bases/polylith/pdm_workspace_hooks/core.py b/bases/polylith/pdm_workspace_hooks/core.py index 095b5fe3..af51ba57 100644 --- a/bases/polylith/pdm_workspace_hooks/core.py +++ b/bases/polylith/pdm_workspace_hooks/core.py @@ -11,7 +11,8 @@ def pdm_build_initialize(context): context.ensure_build_dir() + data = context.config.data build_dir = Path(context.build_dir) root = Path(context.root) - build_initialize(context.config, build_dir, root) + build_initialize(data, build_dir, root) From 4191456deee688ce97affd6d645d52500f1b4129 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Mon, 5 Feb 2024 14:09:53 +0100 Subject: [PATCH 14/14] wip(pdm-workspace): write tdd theme paths to the pth file --- bases/polylith/pdm_workspace_hooks/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bases/polylith/pdm_workspace_hooks/core.py b/bases/polylith/pdm_workspace_hooks/core.py index af51ba57..e9df6c79 100644 --- a/bases/polylith/pdm_workspace_hooks/core.py +++ b/bases/polylith/pdm_workspace_hooks/core.py @@ -13,6 +13,6 @@ def pdm_build_initialize(context): data = context.config.data build_dir = Path(context.build_dir) - root = Path(context.root) + root = Path(context.config.root) build_initialize(data, build_dir, root)