Hatch Polylith Bricks
Hatch Polylith Bricks is a Hatch build hook plugin that integrates Polylith architecture principles into Hatch-managed Python projects. It enables the use of Polylith's `poly` CLI commands for monorepo development, helping manage workspaces, components, and bases. The current version is 1.5.5, with frequent updates addressing new features and fixes.
Warnings
- gotcha Older versions (before 1.5.0, stable-94) might fail at runtime for Python 3.8 environments due to a missing `typing-extensions` dependency for Typer, which was silently dropped by Typer. This could lead to `ModuleNotFoundError`.
- gotcha The `poly check` and `poly libs` commands in older versions (before 1.5.2, stable-99) might incorrectly report missing imports or dependencies when third-party libraries have similar names to Polylith top namespaces. This could lead to misleading warnings or errors.
- gotcha Running `poly create component` or `poly create base` in versions 1.5.1 and newer (stable-98) will now exit with a non-zero fail code if a brick with the same name already exists. This changes previous behavior where it might have proceeded or warned without failing, potentially breaking automation scripts.
Install
-
pip install hatch-polylith-bricks
Imports
- PolylithBuildHook
from hatch_polylith_bricks.plugin import PolylithBuildHook
Quickstart
# 1. Ensure you have Hatch installed: `pip install hatch`
# 2. Initialize a Hatch project (if you don't have one):
# `hatch new my-polylith-project`
# `cd my-polylith-project`
print("--- Step 1: Configure pyproject.toml ---")
print("Add the following to your project's pyproject.toml:")
print("```toml")
print("[tool.hatch.build.hooks.polylith]")
print("# Optional: specify top-level namespace if different from 'project.name'")
print("# namespace = \"my_polylith_namespace\"")
print("```")
print("\n--- Step 2: Create a workspace.toml (optional but recommended) ---")
print("Create a file named workspace.toml in your project root with content like:")
print("```toml")
print("[tool.polylith]")
print("namespace = \"my_polylith_namespace\" # Replace with your actual namespace")
print("bases = [\"src/bases\"]")
print("components = [\"src/components\"]")
print("projects = [\"projects/*\"]")
print("```")
print("\n--- Step 3: Run a Polylith CLI command ---")
print("Make sure you are in the project's root directory where pyproject.toml and workspace.toml reside.")
import subprocess
try:
print("Attempting to run 'poly info'...")
# In a real Hatch project, you'd typically run: hatch run poly info
# This example assumes 'poly' is accessible from the current environment/PATH
result = subprocess.run(['poly', 'info'], capture_output=True, text=True, check=True)
print("STDOUT:\n", result.stdout)
if result.stderr:
print("STDERR:\n", result.stderr)
print("\nSuccessfully ran 'poly info'.")
except FileNotFoundError:
print("Error: 'poly' command not found.\nEnsure 'hatch-polylith-bricks' is installed and 'poly' is in your PATH,\nor run via 'hatch run poly info' if within a Hatch-managed project environment.")
except subprocess.CalledProcessError as e:
print(f"Error running 'poly info': {e}")
print("STDOUT:\n", e.stdout)
print("STDERR:\n", e.stderr)