Hi all, I'm trying to achieve automatic version ge...
# general
p
Hi all, I'm trying to achieve automatic version generation using git status. I want to have a python wheel that contains version.txt file, created at build time. The algorithm involves using
git status
to set version to "dev" when there are any changes... And that's the tricky bit - for
git status
to work, I need whole repository content (also including .git dir). Can I achieve that using pants? What I have so far is...
Copy code
system_binary(
    name="git-binary",
    binary_name="git",
    fingerprint_args=["--version"],
)

adhoc_tool(
    name="git-status",
    runnable=":git-binary",
    execution_dependencies=[
        ":git-repo-data"  << .git files
        # how do I include other repository content?
    ],
    args=["status", "--porcelain"],
    stdout="changelog.txt",
)

experimental_wrap_as_resources(
    name="git-status-result",
    inputs=[
        ":git-status",
    ],
)

files(
    sources=[".git/**"],
    name="git-repo-data",
)
and a wheel that depends on
git-status-result
target.
I probably can't create
files
target that includes
**
because it will mean that .py files that I have in the repo are included in both
files
and
python_sources
targets...
I have tried writing plugin for this that uses
digest = await Get(Digest, PathGlobs(["**"]))
passed to
Process
running
git
and that works fine (I've followed https://www.pantsbuild.org/2.21/docs/writing-plugins/common-plugin-tasks/custom-python-artifact-kwargs to pass version to
setup_kwargs_plugin
rule that way), but I have no idea how to write the version to a file from the
setup_kwargs_plugin
rule level - I understand this can only be done from goal rule, but I don't want to create custom goal to package a wheel, it must be simpler than that 🙂
If someone can point me to right direction, that would be really helpful 🙂
the plugin that I wrote roughly looks like this:
Copy code
@rule
async def setup_kwargs_plugin(
    request: PythonLibrarySetupKwargsRequest,
    local_git_changes_result: LocalGitChangesResult,
    latest_commit_hash_result: LatestCommitHashResult,
) -> SetupKwargs:

    setup_kwargs = request.explicit_kwargs.copy()

    if local_git_changes_result.has_local_changes:
        setup_kwargs["version"] = "dev"
        return SetupKwargs(setup_kwargs, address=request.target.address)

    latest_commit_hash = latest_commit_hash_result.value
    setup_kwargs["version"] = latest_commit_hash
    return SetupKwargs(setup_kwargs, address=request.target.address)

    # Q:
    # how do I write setup_kwargs["version"] to version.py file in the sandbox?
if anyone finds this thread, I've just realized that pants can generate a version from git now... https://www.pantsbuild.org/stable/docs/using-pants/generating-version-tags-from-git