mtkennerly / poetry-dynamic-versioning

Plugin for Poetry to enable dynamic versioning based on VCS tags
MIT License
607 stars 35 forks source link

Add option to create version files rather than requiring templates #147

Closed jsolbrig closed 1 year ago

jsolbrig commented 1 year ago

It would be very helpful if there an option could be added that would allow creating version files rather than requiring templates. This would improve interaction with version control by removing the requirement to have dynamic files checked in and would allow pip install -e . to work correctly (see #146). I'd like to see this operate in a similar way to setuptools_scm which creates the version files rather than templating them.

It could probably be done in the current structure by adding options to the tool.poetry-dynamic-versioning.substitution table. Alternatively, it could be done under a new table such as tool.poetry-dynamic-versioning.create.

If done under a new table, I imagine it might look like this:

[tool.poetry-dynamic-versioning.create]
file = "mypackage/_version.py"
template = [
    "__version__ = \"0.0.0\"",
    "__version_tuple__ = (0, 0, 0)"
]
patterns = [
    "(^__version__\\s*(?::.*?)?=\\s*['\"])[^'\"]*(['\"])",
    { value = "(^__version_tuple__\\s*(?::.*?)?=\\s*\\()[^)]*(\\))", mode = "tuple" },
]

If you like the idea, I might be able to find the time to implement it.

mtkennerly commented 1 year ago

Thanks for this suggestion :) I think this is a good way to handle your use case in #146.

I've added support for the following:

[tool.poetry-dynamic-versioning.files."package/_version.py"]
persistent-substitution = true
initial-content = """
  __version__ = "0.0.0"
  __version_tuple__ = (0, 0, 0)
"""

It will create/reinitialize the file first, then apply the normal substitutions to it. Since */_version.py is one of the default paths for substitutions, the above section may be all you need to add.

This is now available in v1.1.0.

jsolbrig commented 12 months ago

Thank you for doing this. That was really quick turnaround!

I'll try to test this out this afternoon and see how it goes.