Hatch Polylith Bricks

1.5.5 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

Configure `hatch-polylith-bricks` in your `pyproject.toml` as a Hatch build hook, set up your Polylith `workspace.toml`, and then use the `poly` command-line interface to interact with your Polylith project. The example guides through the setup and demonstrates running a basic `poly info` command.

# 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)

view raw JSON →