6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

`poetry-dynamic-versioning`を導入して、Poetryのバージョン情報をGitHubのバージョンタグから生成するようにした

Last updated at Posted at 2023-05-08

環境

  • Python 3.11.2
  • poetry-dynamic-versioning v0.21.4
  • poetry 1.4.2

はじめに

Pythonでツールを開発しています。
poetry-dynamic-versioningを使って、GitHubのバージョンタグから、バージョン情報を生成するようにしました。その際にやったことなどをメモとして残しておきます。

poetry-dynamic-versioningの導入前の状態

pyproject.toml
[tool.poetry]
name = "awesome"
authors = ["yuji38kwmt"]
version = "0.69.1"
description = ""

packages = [
    { include = "awesome" }
]

[tool.poetry.dependencies]
python = "^3.11"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
awesome/__version__.py
version = "0.69.1"

poetry-dynamic-versioningの導入後の状態

pyproject.toml
[tool.poetry]
# ...
version = "0.0.0"  # poetry-dynamic-versioningによって書き換わる

# ...
[tool.poetry-dynamic-versioning]
enable = true
format = "{base}"

[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning"]
build-backend = "poetry_dynamic_versioning.backend"
awesome/__version__.py
version = "0.0.0"  # poetry-dynamic-versioningによって書き換わる

解説

__version__.pyのバージョンの置き換え

デフォルトでは["*.py", "*/__init__.py", "*/__version__.py", "*/_version.py"]のバージョン情報が置き換わります。

補足

最新のバージョンタグが付与されたコミットと一致していない場合

poetry buildしたときのコミットが、最新のバージョンタグが付与されたコミットと一致しない場合、0.68.0.post10.dev0+a78179fのようなバージョンになり、バージョンタグに存在しない情報も付与されます。
最初の0.68.0はバージョンタグから取得した情報(style設定ではbaseという名前で参照可能)です。.post1010は最新のバージョンタグから、離れているコミットの個数(style設定ではdistanceという名前で参照可能)です。+以降のa78179fはコミットのハッシュ値です。

poetry buildしたときのコミットが、最新のバージョンタグが付与されたコミットと一致する場合は、0.68.0というバージョンになります。

styleの設定

format,styleを指定しない場合は PEP 440に従ったバージョンになります。

style (string, default: unset): One of: pep440, semver, pvp. These are preconfigured output formats. If you set both a style and a format, then the format will be validated against the style's rules. If style is unset, the default output format will follow PEP 440, but a custom format will only be validated if style is set explicitly.1

styleには、以下の値を指定できます。styleの値によって、どのようなバージョンになるかを確認しました。

  • pep440 (PEP 440) : 0.68.0.post14.dev0+424893f
  • semver (Semantic Versioning) : 0.68.0.post14+424893f
  • pvp (Haskell Package Versioning Policy) : 0.68.0-post-14-424893f (PEP440に従っていないため、poetry buildは失敗します)

formatの設定

formatに指定できる変数の説明は、poetry-dynamic-versioningが依存しているdunamaiというライブラリのページに詳細が記載されています。

poetry buildしたときのコミットが、最新のバージョンタグが付与されたコミットと一致しない場合でも、0.68.0のようなバージョンにしたい場合は、format = "{base}"を設定すればよいです。

バージョンを生成する処理

バーションを生成の具体的な処理は、以下のコードが参考になります。

参考にしたサイト

  1. https://github.com/mtkennerly/poetry-dynamic-versioning/tree/3570ad9bf3122856be42e475702a9e8c35c7a2b5#configuration

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?