Metadata-Version: 2.5
Name: nbgv-python
Version: 2.1.0
Summary: Python wrapper around the Nerdbank.GitVersioning CLI
Project-URL: Homepage, https://github.com/hcoona/three/tree/main/src/public/lib/nbgv-python
Project-URL: Repository, https://github.com/hcoona/three/tree/main/src/public/lib/nbgv-python
Project-URL: Issues, https://github.com/hcoona/three/issues
Project-URL: Documentation, https://github.com/hcoona/three/blob/main/src/public/lib/nbgv-python/README.md
Project-URL: Changelog, https://github.com/hcoona/three/blob/main/src/public/lib/nbgv-python/CHANGELOG.md
Author-email: Shuai Zhang <zhangshuai.ustc@gmail.com>
License: LGPL-3.0-or-later WITH LGPL-3.0-linking-exception
License-File: LICENSE
Keywords: hatch,nbgv,versioning
Classifier: Framework :: Hatch
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: packaging>=24.0
Description-Content-Type: text/markdown

# nbgv-python Package Overview

## Overview

- `nbgv-python` wraps the Nerdbank.GitVersioning CLI so Python projects can reuse the same versioning semantics.
- The package discovers the CLI via `NBGV_PYTHON_COMMAND`, a direct `nbgv` executable, or `dotnet tool run nbgv` as a fallback.
- A Hatch version source plugin is provided, allowing projects to declare `dynamic = ["version"]` and resolve their version at build time.

## Installation

- Install from PyPI for programmatic use or CLI forwarding:

    ```bash
    pip install nbgv-python
    ```

    Or with `uv`:

    ```bash
    uv add --dev nbgv-python
    # or install the CLI wrapper globally:
    uv tool install nbgv-python
    ```

- For Hatch-managed projects, declare `nbgv-python` in `[build-system].requires` alongside `hatchling` (see [Hatch Integration](#hatch-integration)).
- Ensure the `nbgv` CLI is available either as a global .NET tool (`dotnet tool install -g nbgv`) or via a local `.config/dotnet-tools.json` manifest.
- Optionally set `NBGV_PYTHON_COMMAND` when the executable lives outside of `PATH` or requires custom arguments.

## Hatch Integration

Add the plugin to your project configuration:

```toml
[build-system]
requires = ["hatchling", "nbgv-python"]
build-backend = "hatchling.build"

[project]
dynamic = ["version"]

[tool.hatch.version]
source = "nbgv"

[tool.hatch.version.nbgv]
version-field = "SemVer2"
```

During version resolution Hatch invokes `nbgv get-version --format json --project <absolute-project-path>` and uses the selected field for the package version. The subprocess retains the caller's working directory.
Additional configuration keys include:

- `command` (override CLI invocation)
- `working-directory` (project path passed to `--project`; relative paths resolve against the Hatch project root, and absolute paths are accepted)
- `epoch` _(default: unset)_ – when provided, this non-negative integer is prepended to the normalized version to emit a PEP 440 epoch (for example `2!1.2.3`). Omit the key in TOML to skip the epoch; the Python configuration parser also accepts `None`. TOML has no `null` value.

The plugin normalizes the chosen value to a PEP 440 compliant version, so SemVer pre-release tags such as `-beta.1` are mapped to `b1` automatically.

### Template Fields

- Extend the available template variables via the optional `template-fields` table. Supported options and defaults are:
    - `version-tuple.mode` _(default: `"nbgv"`)_ – choose how to build the tuple. Use `"nbgv"` (default) to assemble components from the Nerdbank.GitVersioning JSON payload, or `"pep440"` to parse the normalized version string.
    - `version-tuple.fields` _(default: `["VersionMajor", "VersionMinor", "BuildNumber", "PrereleaseVersionNoLeadingHyphen"]`)_ – when `mode = "nbgv"`, this ordered list determines which NBGV variables populate the tuple. Empty strings and `null` values are skipped.
    - `version-tuple.normalized-prerelease` _(default: `false`)_ – when `mode = "nbgv"`, convert the `PrereleaseVersionNoLeadingHyphen` entry to its PEP 440 shorthand (for example `beta.1` → `b1`).
    - `version-tuple.epoch` _(default: unset)_ – only applies when `mode = "pep440"`. Omit it to emit the epoch only when the source string already includes a non-zero epoch (e.g., `2!1.0.0`), set `true` to force inclusion, or `false` to suppress it. The parser represents the unset value as `None`.
    - `version-tuple.double-quote` _(default: `true`)_ – controls whether string parts in the tuple use double quotes (`""`) or single quotes (`''`).

    ```toml
    [tool.hatch.version.nbgv.template-fields.version-tuple]
    fields = [
      "VersionMajor",
      "VersionMinor",
      "BuildNumber",
      "PrereleaseVersionNoLeadingHyphen",
    ]
    double-quote = false
    ```

    ```toml
    [tool.hatch.version.nbgv.template-fields.version-tuple]
    mode = "pep440"
    epoch = true
    ```

    ```toml
    [tool.hatch.version.nbgv.template-fields.version-tuple]
    normalized-prerelease = true
    ```

    ```toml
    [tool.hatch.version.nbgv]
    epoch = 2
    ```

- The generated mapping includes `version`, `normalized_version`, `version_tuple`, and every key exposed by `nbgv get-version`.
    - `version` relays the raw value selected by `version-field` (defaults to `SemVer2`). Use this if you need the exact string returned by NBGV (e.g. `1.2.3-beta.1`).
    - `normalized_version` applies PEP 440 normalization to that value using `packaging.version.Version` so `1.2.3-beta.1` becomes `1.2.3b1`. Use this for Python `__version__` strings.
    - `version_tuple` expands into a Python tuple using either the selected NBGV variables (`mode = "nbgv"`, respecting `normalized-prerelease` when enabled) or the parsed PEP 440 segments (`mode = "pep440"`).

### Writing Version Files

- Emit a templated artifact (for example `src/pkg/_version.py`) when Hatch resolves the version, including during builds. The `write` table accepts the following keys:
    - `file` _(required)_ – relative or absolute path of the generated file.
    - `template` _(optional)_ – custom format string. When omitted, `.py` files default to `__version__ = "{normalized_version}"` when that field is available (otherwise `version` is used), while `.txt` or extension-less files default to `{version}`; other suffixes require an explicit template.
    - `encoding` _(optional, default: `"utf-8"`)_ – text encoding used when writing the file.

    ```toml
    [tool.hatch.version.nbgv.write]
    file = "src/pkg/_version.py"
    template = "__version__ = '{normalized_version}'"
    ```

- When `template` is omitted the plugin mirrors `versioningit`: `.py` files render `__version__ = "{normalized_version}"` (falling back to `version` if normalization was not provided), while `.txt` or extension-less files render `{version}`.
- The target path is resolved relative to the project root and created on demand; remember to include it in your source distribution configuration if needed.

## Python API

Retrieve version metadata directly from Python code:

```python
from nbgv_python import get_version

version = get_version()
print(version["semver2"])  # -> 1.2.3+gabcdef
```

Forward arbitrary commands to the CLI:

```python
from nbgv_python import forward

forward(["cloud", "--ci"])
```

Command discovery raises `NbgvNotFoundError` when it finds neither an override, `nbgv`, nor `dotnet`; subprocess failures with non-zero exit codes raise `NbgvCommandError`. An override is not checked for executability during discovery, and finding `dotnet` does not establish that the local `nbgv` tool is installed. `get_version()` raises `NbgvJsonError` for malformed JSON. The Hatch adapter wraps `NbgvError` failures from command discovery, version retrieval, and normalization in `RuntimeError`, retaining the original exception as its cause.

## Command-Line Usage

The console script `nbgv-python` proxies all arguments to the real CLI:

```bash
nbgv-python get-version --format json
```

The wrapper surfaces the same exit codes as the underlying tool and prints diagnostic guidance with exit code 127 when discovery raises `NbgvNotFoundError`.

## Project Records

- [Architecture overview](https://github.com/hcoona/three/blob/main/src/public/lib/nbgv-python/docs/architecture/overview.md): CLI delegation, component boundaries, and unresolved differences from the original design.
- [Hatch integration design](https://github.com/hcoona/three/blob/main/src/public/lib/nbgv-python/docs/architecture/hatch-integration.md): configuration rationale, error handling, and the existing validation basis.
- [Changelog](https://github.com/hcoona/three/blob/main/src/public/lib/nbgv-python/CHANGELOG.md): package history and compatibility changes.
- [Separate Hatch sample](https://github.com/hcoona/three/blob/main/src/sample/nbgv-hatch-demo/README.md): sample-specific configuration and limitations.
