|
| 1 | +#!/usr/bin/env -S uv run --python 3.10 --script |
| 2 | +# /// script |
| 3 | +# requires-python = ">=3.10,<3.14" |
| 4 | +# dependencies = [] |
| 5 | +# /// |
| 6 | +"""Post-generation patch pipeline. |
| 7 | +
|
| 8 | +Runs after Speakeasy code generation to apply durable fixes to generated code. |
| 9 | +See the terraform-provider-airbyte repo for more examples of post-generation patches. |
| 10 | +""" |
| 11 | + |
| 12 | +import re |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 16 | +VERSION_FILE = REPO_ROOT / "src" / "airbyte_api" / "_version.py" |
| 17 | + |
| 18 | + |
| 19 | +def patch_version_file() -> None: |
| 20 | + """Replace the hardcoded `__version__` in `_version.py` with a dynamic lookup. |
| 21 | +
|
| 22 | + Speakeasy generates a `_version.py` with a hardcoded `__version__` string |
| 23 | + and a static `__user_agent__`. This patch rewrites both so that the |
| 24 | + installed package version (set at build time by `uv-dynamic-versioning` |
| 25 | + from the git tag) is used instead. Generation metadata |
| 26 | + (`__openapi_doc_version__`, `__gen_version__`) is left intact. |
| 27 | + """ |
| 28 | + text = VERSION_FILE.read_text() |
| 29 | + original = text |
| 30 | + |
| 31 | + # 1. Replace the hardcoded __version__ assignment with importlib.metadata lookup. |
| 32 | + # Matches: __version__: str = "1.0.0" (any semver-ish string) |
| 33 | + text = re.sub( |
| 34 | + r'^__version__: str = ".*"$', |
| 35 | + "__version__: str = importlib.metadata.version(__title__)", |
| 36 | + text, |
| 37 | + count=1, |
| 38 | + flags=re.MULTILINE, |
| 39 | + ) |
| 40 | + |
| 41 | + # 2. Replace the static __user_agent__ string with an f-string using the |
| 42 | + # dynamic __version__. |
| 43 | + # Matches: __user_agent__: str = "speakeasy-sdk/python 1.0.0 2.911.0 1.0.0 airbyte-api" |
| 44 | + text = re.sub( |
| 45 | + r'^__user_agent__: str = "speakeasy-sdk/python .+"$', |
| 46 | + ( |
| 47 | + "__user_agent__: str = (\n" |
| 48 | + ' f"speakeasy-sdk/python {__version__} {__gen_version__}"\n' |
| 49 | + ' f" {__openapi_doc_version__} {__title__}"\n' |
| 50 | + ")" |
| 51 | + ), |
| 52 | + text, |
| 53 | + count=1, |
| 54 | + flags=re.MULTILINE, |
| 55 | + ) |
| 56 | + |
| 57 | + if text == original: |
| 58 | + print("post_generate: _version.py already patched (no changes)") |
| 59 | + return |
| 60 | + |
| 61 | + VERSION_FILE.write_text(text) |
| 62 | + print("post_generate: patched _version.py (hardcoded __version__ → importlib.metadata)") |
| 63 | + |
| 64 | + |
| 65 | +def main() -> None: |
| 66 | + patch_version_file() |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + main() |
0 commit comments