From ec7086652bd233e190502717498cd57223b68c93 Mon Sep 17 00:00:00 2001 From: Imani Pelton Date: Fri, 24 Jan 2025 09:27:59 -0500 Subject: [PATCH] feat: add improved bash completion script --- pyproject.toml | 2 +- rockcraft/cli.py | 13 +++++++++++-- snap/snapcraft.yaml | 5 +++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 509557ad1..9021f69e7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ classifiers = [ dependencies = [ "craft-application~=4.6.0", "craft-archives>=2.0.0", - "craft-cli", + "craft-cli>=2.15.0", "craft-parts~=2.1.4", "craft-platforms~=0.3", "craft-providers>=2.0.4", diff --git a/rockcraft/cli.py b/rockcraft/cli.py index 7a5221e61..0ba967433 100644 --- a/rockcraft/cli.py +++ b/rockcraft/cli.py @@ -1,6 +1,6 @@ # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- # -# Copyright 2021-2022 Canonical Ltd. +# Copyright 2021-2025 Canonical Ltd. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3 as @@ -16,9 +16,10 @@ """Command-line application entry point.""" -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any from craft_application import commands as appcommands +from craft_cli import Dispatcher from . import commands from .services import RockcraftServiceFactory @@ -59,3 +60,11 @@ def _create_app() -> "Rockcraft": app.add_command_group("Lifecycle", [appcommands.RemoteBuild]) return app + + +def get_app_info() -> tuple[Dispatcher, dict[str, Any]]: + """Retrieve application info. Used by craft-cli's completion module.""" + app = _create_app() + dispatcher = app._create_dispatcher() + + return dispatcher, app.app_config diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 8f66bb5e7..d470213e7 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -25,6 +25,7 @@ platforms: apps: rockcraft: command: bin/python $SNAP/bin/rockcraft + completer: completion.sh skopeo: command: bin/skopeo @@ -126,6 +127,10 @@ parts: uv export --no-dev --no-emit-workspace --no-emit-package pywin32 --output-file uv-requirements.txt ${SNAP}/libexec/snapcraft/craftctl default + PYTHONPATH=/root/parts/$CRAFT_PART_NAME/install/lib/python3.12/site-packages \ + python3 -m craft_cli.completion $CRAFT_PROJECT_NAME rockcraft.cli:get_app_info \ + > $CRAFT_PART_INSTALL/completion.sh + version="$("${CRAFT_STAGE}/usr/bin/python3" -c "import rockcraft;print(rockcraft.__version__)")" ${SNAP}/libexec/snapcraft/craftctl set version="$version" [ -n "$(echo $version | grep "post")" ] && grade=devel || grade=stable