Enforce sorting of manifests (#87020)
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from enum import IntEnum
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
from typing import Any
|
||||
from urllib.parse import urlparse
|
||||
|
||||
@@ -361,8 +363,39 @@ def validate_manifest(integration: Integration, core_components_dir: Path) -> No
|
||||
validate_version(integration)
|
||||
|
||||
|
||||
_SORT_KEYS = {"domain": ".domain", "name": ".name"}
|
||||
|
||||
|
||||
def _sort_manifest_keys(key: str) -> str:
|
||||
return _SORT_KEYS.get(key, key)
|
||||
|
||||
|
||||
def sort_manifest(integration: Integration) -> bool:
|
||||
"""Sort manifest."""
|
||||
keys = list(integration.manifest.keys())
|
||||
if (keys_sorted := sorted(keys, key=_sort_manifest_keys)) != keys:
|
||||
manifest = {key: integration.manifest[key] for key in keys_sorted}
|
||||
integration.manifest_path.write_text(json.dumps(manifest, indent=2))
|
||||
integration.add_error(
|
||||
"manifest",
|
||||
"Manifest keys have been sorted: domain, name, then alphabetical order",
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def validate(integrations: dict[str, Integration], config: Config) -> None:
|
||||
"""Handle all integrations manifests."""
|
||||
core_components_dir = config.root / "homeassistant/components"
|
||||
manifests_resorted = []
|
||||
for integration in integrations.values():
|
||||
validate_manifest(integration, core_components_dir)
|
||||
if not integration.errors:
|
||||
if sort_manifest(integration):
|
||||
manifests_resorted.append(integration.manifest_path)
|
||||
if manifests_resorted:
|
||||
subprocess.run(
|
||||
["pre-commit", "run", "--hook-stage", "manual", "prettier", "--files"]
|
||||
+ manifests_resorted,
|
||||
stdout=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user