Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a plugin system #3733

Merged
merged 6 commits into from
Mar 27, 2021
Merged

Implement a plugin system #3733

merged 6 commits into from
Mar 27, 2021

Conversation

sdispater
Copy link
Member

@sdispater sdispater commented Feb 26, 2021

This PR introduces a plugin system to add commands and features to Poetry more easily.

It uses the standard entrypoints system that exists today and will make it possible to build two kinds of plugins:

  • Generic plugins that will make it possible to interact with the Poetry instance created by Poetry. This will be useful to, for instance, implement a plugin that will dynamically set the version of the project based on VCS tags and commits.
  • Application plugins that will make it possible to add commands to the Poetry CLI.

This PR also provides three new commands to manage plugins:

  • plugin add to install plugins
  • plugin remove to uninstall plugins
  • plugin show to list currently installed plugins.

Todo

  • Tests for the plugin add command
  • The plugin remove command
  • The plugin show command

Pull Request Check List

Resolves: #693

  • Added tests for changed code.
  • Updated documentation for changed code.

@sdispater sdispater added the area/plugin-api Related to plugins/plugin API label Feb 26, 2021
@sdispater sdispater added this to the 1.2 milestone Feb 26, 2021
@sdispater sdispater mentioned this pull request Feb 26, 2021
2 tasks
docs/docs/cli.md Outdated Show resolved Hide resolved
docs/docs/plugins.md Outdated Show resolved Hide resolved
Copy link

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh. This is exciting [WIP] development. Looking forward to it!

Are plugins prohibited with poetry new? Not sure why people would need them in that context, but 🤷.

pass

self._disable_plugins = (
io.input.has_parameter_option("--no-plugins") or command_name == "new"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plugins can't be used for poetry new calls?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a leftover restriction from a previous shot at implementing the plugin system.

The current implementation should not need such restriction.

I'll check and remove it if needed.

or if you wish to accomplish something with Poetry in a way that is not desired by most users.

In these cases you could consider creating a plugin to handle your specific logic.

This comment was marked as outdated.

@sdispater sdispater force-pushed the plugin-system-revamped branch 2 times, most recently from e004ba9 to 18bc2fc Compare February 28, 2021 21:21
@sdispater sdispater mentioned this pull request Mar 1, 2021
1 task
@sdispater sdispater force-pushed the plugin-system-revamped branch 4 times, most recently from a690ce7 to 65a2219 Compare March 10, 2021 11:19
docs/docs/cli.md Outdated Show resolved Hide resolved
docs/docs/cli.md Show resolved Hide resolved
@1ace
Copy link
Contributor

1ace commented Mar 10, 2021

I interpret your docs to mean "yes", but can you confirm whether it's possible with this system to have a repo-specific plugin that lives in the repo itself? or does it need to be installed in one of the sites for poetry to see it?

That would be a really useful feature I think :)

@sdispater
Copy link
Member Author

@1ace Plugins will be global and not on a per-project basis.

@sdispater sdispater force-pushed the plugin-system-revamped branch 4 times, most recently from b66107d to b29004f Compare March 19, 2021 20:53
@abn abn mentioned this pull request Mar 21, 2021
2 tasks
Comment on lines +121 to +123
!!!warning

A plugin **must not** remove or modify in any way the core commands of Poetry.

This comment was marked as resolved.

@sdispater sdispater force-pushed the plugin-system-revamped branch 3 times, most recently from d741351 to b0ff2ea Compare March 25, 2021 15:22
@sdispater
Copy link
Member Author

@abn Regarding the export command, there are two ways to go:

  • Make use of subcommands rather than a --format option.
  • Make the export command pluggable so that the --format option can be hooked into.

Once we make it an official plugin we'll see how it goes. For now, I don't have a strong preference even though I feel like keeping the option makes more sense.

As to how sub commands can be added, it's simple since subcommands are nothing more than namespaced commands. So basically, it woould look like this:

from cleo.commands.command import Command
from poetry.plugins.application_plugin import ApplicationPlugin


class ExportRequirementsCommand(Command):

    name = "export requirements.txt"

    def handle(self) -> int:
        # Do the export

        return 0


def export_command_factory():
    return ExportRequirementsCommand()


class ExportApplicationPlugin(ApplicationPlugin):
    def activate(self, application):
        application.command_loader.register_factory(
            "export requirements.txt", export_command_factory
        )

@abn abn merged commit 3738ae7 into master Mar 27, 2021
@abn abn deleted the plugin-system-revamped branch March 27, 2021 10:59
@henryiii
Copy link

Just curious, would it be possible to add an extension building system via a plugin? So poetry handles the Python files and CMake (for example) handles the compiling? I assume a little extra work would be needed to ensure wheels has the right names and such, but how close might this be to working?

@abn
Copy link
Member

abn commented Mar 27, 2021

@henryiii If I am understanding you correct, I suspect you can achieve that today using the build script without a plugin. https://github.com/sdispater/pendulum/blob/411d0aa41a5f39c5f4a2f43a3c369c2dd24787db/pyproject.toml#L51-L53

You can add the cmake call into the build.py. If this is not the case, I would suggest you open a discussion.

@henryiii
Copy link

Thanks @abn, that's good to know about. (Though, @sdispater, two notes; just to keep in mind, distutils is slated for removal in Python 3.12, and you shouldn't name a top-level file build.py, because then you can't build the project with python -m build, I've had to debug this before).

I'll probably open a discussion, because I'm interested in developing a scikit-build plugin that would integrate CMake with Poetry.

@mpeteuil
Copy link

mpeteuil commented Apr 26, 2021

I was wondering if there is a recommended naming pattern for plugins? Like poetry_plugin_X or poetry_X_plugin or something like that. I asked in the Discord and was pointed to Github, although this PR may not be the right place and one of the other plugin-related issues could be a better place for this question.

Copy link

github-actions bot commented Mar 1, 2024

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/plugin-api Related to plugins/plugin API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Discussion] Poetry to provide a decent hook-based plugin system
9 participants