Skip to content

Commit ece7e33

Browse files
authored
fix(poly create base, component): exit with fail code if brick already exists (#431)
* fix(poly create base, component): exit with fail code if brick already exists * bump the CLI to 1.43.0 * bump the Poetry plugin to 1.49.0
1 parent ad99369 commit ece7e33

File tree

7 files changed

+52
-19
lines changed

7 files changed

+52
-19
lines changed

bases/polylith/cli/create.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@
1010
app = Typer()
1111

1212

13+
def _try_create(name: str, description: str, fn):
14+
try:
15+
create(name, description, fn)
16+
except ValueError as e:
17+
print(f"Did not create: {e}")
18+
19+
raise Exit(code=1)
20+
21+
1322
@app.command("base")
1423
def base_command(
1524
name: Annotated[str, Option(help="Name of the base.")],
1625
description: Annotated[str, Option(help="Description of the base.")] = "",
1726
):
1827
"""Creates a Polylith base."""
19-
create(name, description, base.create_base)
28+
_try_create(name, description, base.create_base)
2029

2130

2231
@app.command("component")
@@ -25,7 +34,7 @@ def component_command(
2534
description: Annotated[str, Option(help="Description of the component.")] = "",
2635
):
2736
"""Creates a Polylith component."""
28-
create(name, description, component.create_component)
37+
_try_create(name, description, component.create_component)
2938

3039

3140
def _create_project(root: Path, options: dict):

components/polylith/bricks/brick.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
from pathlib import Path
22

3-
from polylith import configuration
3+
from polylith import configuration, info
44
from polylith.dirs import create_dir
55
from polylith.files import create_file
66
from polylith.interface import create_interface
77
from polylith.readme import create_brick_readme
88

99

10+
def validate(root: Path, namespace: str, name: str) -> None:
11+
bases = info.get_bases(root, namespace)
12+
components = info.get_components(root, namespace)
13+
14+
if name in bases:
15+
message = f"{name} already exists in bases."
16+
raise ValueError(message)
17+
18+
if name in components:
19+
message = f"{name} already exists in components."
20+
raise ValueError(message)
21+
22+
1023
def create_brick(root: Path, options: dict) -> None:
1124
modulename = options["modulename"]
25+
package = options["package"]
26+
namespace = options["namespace"]
27+
28+
validate(root, namespace, package)
29+
1230
path_kwargs = {
1331
k: v for k, v in options.items() if k in {"brick", "namespace", "package"}
1432
}

components/polylith/poetry/commands/create_base.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from cleo.helpers import option
22
from poetry.console.commands.command import Command
33
from polylith.bricks import base
4-
from polylith.commands.create import create
4+
from polylith.poetry.commands.create_brick import try_create
55

66

77
class CreateBaseCommand(Command):
@@ -20,9 +20,4 @@ class CreateBaseCommand(Command):
2020
]
2121

2222
def handle(self) -> int:
23-
name = self.option("name")
24-
description = self.option("description")
25-
26-
create(name, description, base.create_base)
27-
28-
return 0
23+
return try_create(self, base.create_base)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from poetry.console.commands.command import Command
2+
from polylith.commands.create import create
3+
4+
5+
def try_create(command: Command, fn) -> int:
6+
name = command.option("name")
7+
description = command.option("description")
8+
9+
try:
10+
create(name, description, fn)
11+
except ValueError as e:
12+
command.line_error(f"Did not create: <error>{e}</error>")
13+
14+
return 1
15+
16+
return 0

components/polylith/poetry/commands/create_component.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from cleo.helpers import option
22
from poetry.console.commands.command import Command
33
from polylith.bricks import component
4-
from polylith.commands.create import create
4+
from polylith.poetry.commands.create_brick import try_create
55

66

77
class CreateComponentCommand(Command):
@@ -20,9 +20,4 @@ class CreateComponentCommand(Command):
2020
]
2121

2222
def handle(self) -> int:
23-
name = self.option("name")
24-
description = self.option("description")
25-
26-
create(name, description, component.create_component)
27-
28-
return 0
23+
return try_create(self, component.create_component)

projects/poetry_polylith_plugin/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 = "poetry-polylith-plugin"
3-
version = "1.48.4"
3+
version = "1.49.0"
44
description = "A Poetry plugin that adds tooling support for the Polylith Architecture"
55
authors = ["David Vujic"]
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

projects/polylith_cli/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 = "polylith-cli"
3-
version = "1.42.5"
3+
version = "1.43.0"
44
description = "Python tooling support for the Polylith Architecture"
55
authors = ['David Vujic']
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

0 commit comments

Comments
 (0)