nat-n / poethepoet

A task runner that works well with poetry.
https://poethepoet.natn.io/
MIT License
1.48k stars 60 forks source link

poetry_hooks with switch tasks by named argument #193

Open StripedMiha opened 10 months ago

StripedMiha commented 10 months ago

Hi! I was very interested in your tool and decided to implement it into the project.

In my project, I want to add a hook with a pre-commit install setup along with executing the poetry install --with dev command. Here is my puproject.toml with dependencies divided into groups:

[tool.poetry.dependencies]
python = "^3.10"
...
poethepoet = "^0.24.4"

[tool.poetry.group.dev.dependencies]
...
pre-commit = "^3.2.0"

[tool.poetry.group.test.dependencies]
...

I have a working solution for this purpose, which is always executed when entering the poetry install command.

[tool.poe.poetry_hooks]
post_install = "pre_commit_install"

[tool.poe.tasks.pre_commit_install]
help = "pre-commit pre install"
cmd = "pre-commit install"

However, I need the hook to be executed only when entering the poetry install command with the --with dev argument.

Here is my vision of pyproject.toml, for such functionality:

[tool.poe.poetry_hooks]
post_install = "pre_commit_install"

[tool.poe.tasks.pre_commit_install]
help = "pre-commit pre install"
default = "pass"
control.expr = "with"
args = ["with"]

    [[tool.poe.tasks.pre_commit_install.switch]]
    case = "dev"
    cmd = "pre-commit install"

When I enter the command poetry install --with dev, I get the following error:

image

Please help me figure out what went wrong.

Thanks!

nat-n commented 10 months ago

Hi @StripedMiha,

However, I need the hook to be executed only when entering the poetry install command with the --with dev argument.

I'm afraid limiting poetry hooks to invocations including specific arguments is not currently supported. Your workaround using a switch task is interesting, and might even work.

I believe the error you're getting from the expr task is because an expr task content is actually interpreted as subset of python, and as it happens with is a keyword in python, so the validation fails.

You should be able to avoid this particular error by mapping the argument to a different name with something like:

control.expr = "deps_group"
args = [{ name = "deps_group", options = ["--with"] }]

Please let me know if you get it working :)

StripedMiha commented 10 months ago

@nat-n Thanks for the advice.

But I'm afraid I don't quite understand how this should work, but this option does not catch the argument correctly and gives a default response.

[tool.poe.tasks.pre_commit_install]
help = "pre-commit pre install"
control.expr = "deps_group"
args = [{ name = "deps_group", options = ["--with"] }]

    [[tool.poe.tasks.pre_commit_install.switch]]
    case = "dev"
    cmd = "pre-commit install"

    [[tool.poe.tasks.pre_commit_install.switch]]
    cmd = "echo abc"

The response from such a command now does not fall with an error, but the switcher does not catch the argument

image
nat-n commented 10 months ago

Hi @StripedMiha ,

Thanks for getting back. In that case I think what you want is not possible today, because the poetry plugin only passes the task name to poe: https://github.com/nat-n/poethepoet/blob/71fcdc788372f2b00ca3d6642ee6a4fcb4965982/poethepoet/plugin.py#L226

However in principle I suspect it could be supported because that linked function in the plugin could inspect the arguments from the present ConsoleCommandEvent and pass them to the poe task. It would also require a tweak to the logic around there to support templating in the poetry task's args if the user configures the hook with something like: post_install = "_my_task --with=${POETRY_ARGS_WITH}".

Maybe this wouldn't too difficult to implement. I'll leave this issue open as an enhancement request.