Skip to content

Commit f0e54f7

Browse files
authored
feat(poly env): new command for build backends without support for multiple module roots or hooks (#403)
* feat(poly env): add command for Package & Dependency management tools without support for multiple module roots, or build hooks * bump PDM brick hook to 1.3.5 * bump PDM workspace hook to 1.3.5 * bump CLI to 1.39.0 * test(environment): rename to environment tests * fix(environment): add function to brick interface * fix(poly env): check if any projects before continuing
1 parent cd16954 commit f0e54f7

File tree

11 files changed

+89
-42
lines changed

11 files changed

+89
-42
lines changed

bases/polylith/cli/core.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import List, Union
33

44
from polylith import commands, configuration, info, repo
5-
from polylith.cli import build, create, options, test
5+
from polylith.cli import build, create, env, options, test
66
from typer import Exit, Option, Typer
77
from typing_extensions import Annotated
88

@@ -15,6 +15,12 @@
1515
no_args_is_help=True,
1616
)
1717

18+
app.add_typer(
19+
test.app,
20+
name="test",
21+
help="Commands for tests.",
22+
no_args_is_help=True,
23+
)
1824

1925
app.add_typer(
2026
build.app,
@@ -24,9 +30,9 @@
2430
)
2531

2632
app.add_typer(
27-
test.app,
28-
name="test",
29-
help="Commands for tests.",
33+
env.app,
34+
name="env",
35+
help="For Package & Dependency Management tools without support for plugins or build hooks.",
3036
no_args_is_help=True,
3137
)
3238

bases/polylith/cli/env.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sysconfig
2+
from pathlib import Path
3+
4+
from polylith import configuration, environment, info, repo
5+
from typer import Typer
6+
7+
app = Typer()
8+
9+
10+
@app.command("setup")
11+
def setup_command():
12+
"""Setup the current virtual environment, by adding the bases and components paths as module root."""
13+
root = repo.get_workspace_root(Path.cwd())
14+
ns = configuration.get_namespace_from_config(root)
15+
16+
projects_data = info.get_projects_data(root, ns)
17+
dev_project = next((p for p in projects_data if p["type"] == "development"), None)
18+
19+
if not dev_project:
20+
return
21+
22+
env_dir = Path(sysconfig.get_paths().get("purelib"))
23+
24+
environment.add_paths(dev_project, env_dir, root)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from polylith.environment.core import add_paths, parse_paths
2+
3+
__all__ = ["add_paths", "parse_paths"]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from pathlib import Path
2+
from typing import Set
3+
4+
from polylith import configuration, toml
5+
6+
7+
def paths_from_config(ns: str, data: dict) -> Set[str]:
8+
packages = toml.get_project_package_includes(ns, data)
9+
10+
return {p["from"] for p in packages}
11+
12+
13+
def parse_paths(root: Path, theme: str, ns: str, data: dict) -> Set[str]:
14+
defaults = {"bases", "components"}
15+
16+
paths = defaults if theme == "loose" else paths_from_config(ns, data)
17+
18+
return {(root / p).as_posix() for p in paths}
19+
20+
21+
def write_pth_file(target_dir: Path, paths: Set[str]) -> None:
22+
filepath = target_dir / "polylith_workspace.pth"
23+
24+
if filepath.exists():
25+
return
26+
27+
with open(filepath, "w") as f:
28+
for p in paths:
29+
f.write(f"{p}\n")
30+
31+
32+
def add_paths(config_data: dict, target_dir: Path, root: Path) -> None:
33+
theme = configuration.get_theme_from_config(root)
34+
ns = configuration.get_namespace_from_config(root)
35+
36+
paths = parse_paths(root, theme, ns, config_data)
37+
38+
if not paths:
39+
return
40+
41+
write_pth_file(target_dir, paths)
Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,7 @@
11
from pathlib import Path
2-
from typing import Set
32

4-
from polylith import configuration, toml
5-
6-
7-
def paths_from_config(ns: str, data: dict) -> Set[str]:
8-
packages = toml.get_project_package_includes(ns, data)
9-
10-
return {p["from"] for p in packages}
11-
12-
13-
def parse_paths(root: Path, theme: str, ns: str, data: dict) -> Set[str]:
14-
defaults = {"bases", "components"}
15-
16-
paths = defaults if theme == "loose" else paths_from_config(ns, data)
17-
18-
return {(root / p).as_posix() for p in paths}
19-
20-
21-
def write_pth_file(build_dir: Path, paths: Set[str]) -> None:
22-
filepath = build_dir / "polylith_workspace.pth"
23-
24-
with open(filepath, "w") as f:
25-
for p in paths:
26-
f.write(f"{p}\n")
3+
from polylith import environment
274

285

296
def build_initialize(config_data: dict, build_dir: Path, root: Path) -> None:
30-
theme = configuration.get_theme_from_config(root)
31-
ns = configuration.get_namespace_from_config(root)
32-
33-
paths = parse_paths(root, theme, ns, config_data)
34-
35-
if not paths:
36-
return
37-
38-
write_pth_file(build_dir, paths)
7+
environment.add_paths(config_data, build_dir, root)

projects/pdm_polylith_bricks/pyproject.toml

Lines changed: 2 additions & 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.3.4"
3+
version = "1.3.5"
44
description = "a PDM build hook for Polylith"
55
authors = ["David Vujic"]
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"
@@ -12,6 +12,7 @@ packages = [
1212
{include = "polylith/pdm_project_hooks", from = "../../bases"},
1313
{include = "polylith/building",from = "../../components"},
1414
{include = "polylith/configuration",from = "../../components"},
15+
{include = "polylith/environment",from = "../../components"},
1516
{include = "polylith/parsing",from = "../../components"},
1617
{include = "polylith/pdm",from = "../../components"},
1718
{include = "polylith/repo",from = "../../components"},

projects/pdm_polylith_workspace/pyproject.toml

Lines changed: 2 additions & 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.3.4"
3+
version = "1.3.5"
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"
@@ -11,6 +11,7 @@ readme = "README.md"
1111
packages = [
1212
{include = "polylith/pdm_workspace_hooks", from = "../../bases"},
1313
{include = "polylith/building",from = "../../components"},
14+
{include = "polylith/environment",from = "../../components"},
1415
{include = "polylith/configuration",from = "../../components"},
1516
{include = "polylith/parsing",from = "../../components"},
1617
{include = "polylith/pdm",from = "../../components"},

projects/polylith_cli/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "polylith-cli"
3-
version = "1.38.2"
3+
version = "1.39.0"
44
description = "Python tooling support for the Polylith Architecture"
55
authors = ['David Vujic']
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"
@@ -21,6 +21,7 @@ packages = [
2121
{ include = "polylith/diff", from = "../../components" },
2222
{ include = "polylith/dirs", from = "../../components" },
2323
{ include = "polylith/distributions", from = "../../components" },
24+
{ include = "polylith/environment", from = "../../components" },
2425
{ include = "polylith/files", from = "../../components" },
2526
{ include = "polylith/imports", from = "../../components" },
2627
{ include = "polylith/info", from = "../../components" },

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ packages = [
2323
{ include = "polylith/diff", from = "components" },
2424
{ include = "polylith/dirs", from = "components" },
2525
{ include = "polylith/distributions", from = "components" },
26+
{ include = "polylith/environment", from = "components" },
2627
{ include = "polylith/files", from = "components" },
2728
{ include = "polylith/hatch", from = "components" },
2829
{ include = "polylith/imports", from = "components" },
File renamed without changes.

0 commit comments

Comments
 (0)