Repository navigation
feat: add --json to preset info and extension info #4833
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
Merged
+529
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cc162a2
feat: add --json to preset info and extension info
nefayran e146b11
fix: keep preset info human-readable when called directly
nefayran 7e9aed2
fix: reject --json with --versions in preset info
nefayran 6074227
fix: give each info entry its provenance as source; empty description…
nefayran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| """Private JSON output helpers for installed preset and extension info. | ||
|
|
||
| ``preset info --json`` and ``extension info --json`` describe one installed | ||
| pack. The top-level fields are the ``list --json`` item for that pack (see | ||
| ``_installed_list_json``); its ``provides`` counts are replaced by one array | ||
| per contribution kind, so the detail view and the summary view never drift. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from ._installed_list_json import installed_list_item | ||
|
|
||
|
|
||
| def _find_installed( | ||
| records: list[dict[str, Any]], key: str, kind: str, *, match_names: bool | ||
| ) -> dict[str, Any]: | ||
| """Return the installed record for an ID, or (optionally) a unique display name. | ||
|
|
||
| The lookup mirrors the human-readable ``info`` commands: presets resolve by | ||
| ID only, extensions also accept a case-insensitive display name. | ||
| """ | ||
| for record in records: | ||
| if record["id"] == key: | ||
| return record | ||
| by_name = [] | ||
| if match_names: | ||
| by_name = [record for record in records if str(record["name"]).lower() == key.lower()] | ||
| if len(by_name) == 1: | ||
| return by_name[0] | ||
| if by_name: | ||
| raise ValueError( | ||
| f"{kind.capitalize()} name '{key}' is ambiguous; use one of the IDs: " | ||
| + ", ".join(sorted(str(record["id"]) for record in by_name)) | ||
| ) | ||
| raise ValueError(f"{kind.capitalize()} '{key}' is not installed") | ||
|
|
||
|
|
||
| def _contribution(entry: dict[str, Any], source: dict[str, str]) -> dict[str, Any]: | ||
| """Return one command, template or script entry of an installed pack. | ||
|
|
||
| ``source`` is the entry's provenance, the layer and the pack that provide | ||
| it (#4213); where the pack was installed from is the top-level ``source``. | ||
| """ | ||
| description = entry.get("description") | ||
| return { | ||
| "name": entry["name"], | ||
| # ``description:`` with no value reads as None, and the preset manifest | ||
| # does not check contribution descriptions | ||
| "description": description if isinstance(description, str) else "", | ||
| "source": dict(source), | ||
| "sourcePath": entry["file"], | ||
| } | ||
|
|
||
|
|
||
| def preset_info_item(records: list[dict[str, Any]], manager: Any, key: str) -> dict[str, Any]: | ||
| """Return the JSON object for one installed preset.""" | ||
| record = _find_installed(records, key, "preset", match_names=False) | ||
| manifest = manager.get_pack(record["id"]) | ||
| if manifest is None: | ||
| raise ValueError(f"Preset '{record['id']}' has an unreadable manifest") | ||
|
|
||
| item = installed_list_item(record, include_hooks=False) | ||
| del item["provides"] | ||
| groups: dict[str, list[dict[str, Any]]] = {"commands": [], "templates": [], "scripts": []} | ||
| provenance = {"layer": "preset", "presetId": record["id"]} | ||
| for template in manifest.templates: | ||
| entry = _contribution(template, provenance) | ||
| entry["strategy"] = template.get("strategy", "replace") | ||
| groups[f"{template['type']}s"].append(entry) | ||
| return {**item, **groups} | ||
|
|
||
|
|
||
| def extension_info_item(records: list[dict[str, Any]], manager: Any, key: str) -> dict[str, Any]: | ||
| """Return the JSON object for one installed extension.""" | ||
| from .extensions import DEFAULT_HOOK_PRIORITY, coerce_hook_entries, normalize_priority | ||
|
|
||
| record = _find_installed(records, key, "extension", match_names=True) | ||
| manifest = manager.get_extension(record["id"]) | ||
| if manifest is None: | ||
| raise ValueError(f"Extension '{record['id']}' has an unreadable manifest") | ||
|
|
||
| item = installed_list_item(record, include_hooks=True) | ||
| del item["provides"] | ||
| provenance = {"layer": "extension", "extensionId": record["id"]} | ||
| scripts = [] | ||
| for script in manifest.scripts: | ||
| entry = _contribution(script, provenance) | ||
| if "runtimes" in script: | ||
| entry["runtimes"] = list(script["runtimes"]) | ||
| scripts.append(entry) | ||
|
|
||
| # Read hooks the way hook registration does: per event, a later | ||
| # declaration for the same command replaces the earlier one, and the | ||
| # priority/optional defaults are the ones the hook executor applies. | ||
| hooks = [] | ||
| for event_name, hook_config in manifest.hooks.items(): | ||
| by_command: dict[str, dict[str, Any]] = {} | ||
| for entry in coerce_hook_entries(hook_config): | ||
| if isinstance(entry, dict) and entry.get("command"): | ||
| by_command.pop(entry["command"], None) | ||
| by_command[entry["command"]] = entry | ||
| for command, entry in by_command.items(): | ||
| hooks.append( | ||
| { | ||
| "trigger": event_name, | ||
| "targetCommand": command, | ||
| "optional": bool(entry.get("optional", True)), | ||
| "priority": normalize_priority(entry.get("priority"), DEFAULT_HOOK_PRIORITY), | ||
| } | ||
| ) | ||
|
|
||
| return { | ||
| **item, | ||
| "commands": [_contribution(command, provenance) for command in manifest.commands], | ||
| "templates": [_contribution(template, provenance) for template in manifest.templates], | ||
| "scripts": scripts, | ||
| "hooks": hooks, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.