Skip to content

Commit 643425c

Browse files
committed
feat(cli): wip: make build command build-system agnostic
1 parent 1e1e272 commit 643425c

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/poetry/console/commands/build.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
from pathlib import Path
44
from typing import TYPE_CHECKING
55
from typing import ClassVar
6+
from typing import Literal
67

78
from cleo.helpers import option
89

910
from poetry.console.commands.env_command import EnvCommand
10-
from poetry.utils.env import build_environment
1111
from poetry.utils.helpers import remove_directory
12+
from poetry.utils.isolated_build import isolated_builder
1213

1314

1415
if TYPE_CHECKING:
1516
from cleo.io.inputs.option import Option
1617

18+
BUILD_FORMATS = ["sdist", "wheel"]
19+
1720

1821
class BuildCommand(EnvCommand):
1922
name = "build"
@@ -47,49 +50,52 @@ class BuildCommand(EnvCommand):
4750
"poetry.core.masonry.builders.wheel",
4851
]
4952

50-
def _build(
51-
self,
52-
fmt: str,
53-
executable: str | Path | None = None,
54-
*,
55-
target_dir: Path | None = None,
56-
) -> None:
57-
from poetry.masonry.builders import BUILD_FORMATS
53+
def _requested_formats(self) -> list[Literal["sdist", "wheel"]]:
54+
fmt = self.option("format") or "all"
5855

5956
if fmt in BUILD_FORMATS:
60-
builders = [BUILD_FORMATS[fmt]]
57+
formats = [fmt]
6158
elif fmt == "all":
62-
builders = list(BUILD_FORMATS.values())
59+
formats = BUILD_FORMATS
6360
else:
6461
raise ValueError(f"Invalid format: {fmt}")
6562

63+
return formats # type: ignore[return-value] # TODO
64+
65+
def _build(
66+
self, fmt: Literal["wheel", "sdist"], executable: Path, target_dir: Path
67+
) -> None:
68+
config_settings = {}
69+
6670
if local_version_label := self.option("local-version"):
67-
self.poetry.package.version = self.poetry.package.version.replace(
68-
local=local_version_label
69-
)
71+
config_settings["local-version"] = local_version_label
7072

71-
for builder in builders:
72-
builder(self.poetry, executable=executable).build(target_dir)
73+
with isolated_builder(
74+
source=self.poetry.file.path.parent,
75+
distribution=fmt,
76+
python_executable=executable,
77+
) as builder:
78+
self.line(f"Building <info>{fmt}</info>")
79+
builder.build(fmt, target_dir, config_settings=config_settings)
7380

7481
def handle(self) -> int:
7582
if not self.poetry.is_package_mode:
7683
self.line_error("Building a package is not possible in non-package mode.")
7784
return 1
7885

79-
with build_environment(poetry=self.poetry, env=self.env, io=self.io) as env:
80-
fmt = self.option("format") or "all"
81-
dist_dir = Path(self.option("output"))
82-
package = self.poetry.package
83-
self.line(
84-
f"Building <c1>{package.pretty_name}</c1> (<c2>{package.version}</c2>)"
85-
)
86+
dist_dir = Path(self.option("output"))
87+
package = self.poetry.package
88+
self.line(
89+
f"Building <c1>{package.pretty_name}</c1> (<c2>{package.version}</c2>)"
90+
)
8691

87-
if not dist_dir.is_absolute():
88-
dist_dir = self.poetry.pyproject_path.parent / dist_dir
92+
if not dist_dir.is_absolute():
93+
dist_dir = self.poetry.pyproject_path.parent / dist_dir
8994

90-
if self.option("clean"):
91-
remove_directory(path=dist_dir, force=True)
95+
if self.option("clean"):
96+
remove_directory(path=dist_dir, force=True)
9297

93-
self._build(fmt, executable=env.python, target_dir=dist_dir)
98+
for fmt in self._requested_formats():
99+
self._build(fmt, executable=self.env.python, target_dir=dist_dir)
94100

95101
return 0

0 commit comments

Comments
 (0)