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

Add commands to hatch cli from plugins #805

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion backend/src/hatchling/plugin/specs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pluggy

hookspec = pluggy.HookspecMarker('hatch')
hookspec = pluggy.HookspecMarker("hatch")


@hookspec
Expand All @@ -21,3 +21,8 @@ def hatch_register_build_hook() -> None:
@hookspec
def hatch_register_metadata_hook() -> None:
"""Register new classes that adhere to the metadata hook interface."""


@hookspec
def hatch_register_commands() -> None:
"""Register new commands to cli"""
1 change: 1 addition & 0 deletions hatch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
dependencies = [
"coverage[toml]>=6.2",
"filelock>=3.7.1",
"importlib-resources",
"pytest",
"pytest-cov",
"pytest-mock",
Expand Down
3 changes: 3 additions & 0 deletions src/hatch/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from hatch.cli.dep import dep
from hatch.cli.env import env
from hatch.cli.new import new
from hatch.cli.plugin import load_plugins
from hatch.cli.project import project
from hatch.cli.publish import publish
from hatch.cli.run import run
Expand Down Expand Up @@ -200,6 +201,8 @@ def hatch(ctx: click.Context, env_name, project, color, interactive, verbose, qu
hatch.add_command(status)
hatch.add_command(version)

load_plugins(hatch)


def main(): # no cov
try:
Expand Down
16 changes: 16 additions & 0 deletions src/hatch/cli/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from itertools import chain

import pluggy
from click import Group
from hatchling.plugin import specs


def load_plugins(hatch: Group):
pm = pluggy.PluginManager("hatch")

pm.add_hookspecs(specs)
pm.load_setuptools_entrypoints("hatch")

plugins = pm.hook.hatch_register_commands()
for plugin in chain.from_iterable(plugins):
hatch.add_command(plugin)
Empty file added tests/cli/plugin/__init__.py
Empty file.
Empty file.
13 changes: 13 additions & 0 deletions tests/cli/plugin/cli-plugin/hatch_cli/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import click
from hatchling.plugin import hookimpl


@click.command(short_help="Execute a custom command")
def random():
"""Execute a custom command"""
click.echo("random")


@hookimpl
def hatch_register_commands():
return [random]
18 changes: 18 additions & 0 deletions tests/cli/plugin/cli-plugin/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "hatch-test-cli"
version = "0.0.1"
description = ""
requires-python = ">=3.7"
authors = [
{ name = "Thomas Denoréaz", email = "[email protected]" },
]
dependencies = [
"hatch>=1.2.0",
]

[project.entry-points.hatch]
cli = "hatch_cli.hooks"
15 changes: 15 additions & 0 deletions tests/cli/plugin/test_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import shutil

import importlib_resources


def test_run_command(hatch, temp_dir, helpers):
project_dir = temp_dir / "cli-plugin"
shutil.copytree(importlib_resources.files(__package__) / "cli-plugin", project_dir)

with project_dir.as_cwd():
result1 = hatch("new", "--init")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
result1 = hatch("new", "--init")
hatch("new", "--init")

Copy link
Author

Choose a reason for hiding this comment

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

This is still in draft, that part is anyway not working (cf: #805 (comment)) -> forgot to add a TODO on top

result = hatch("random")

assert result.exit_code == 0, result.output
assert result.output == 'random\n'