mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Enforce sorting of manifests (#87020)
This commit is contained in:
parent
b93c135c8d
commit
0cfb937d07
@ -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,
|
||||
)
|
||||
|
@ -130,6 +130,7 @@ class Integration:
|
||||
|
||||
path: pathlib.Path
|
||||
_manifest: dict[str, Any] | None = None
|
||||
manifest_path: pathlib.Path | None = None
|
||||
errors: list[Error] = field(default_factory=list)
|
||||
warnings: list[Error] = field(default_factory=list)
|
||||
translated_name: bool = False
|
||||
@ -223,3 +224,4 @@ class Integration:
|
||||
return
|
||||
|
||||
self._manifest = manifest
|
||||
self.manifest_path = manifest_path
|
||||
|
Loading…
x
Reference in New Issue
Block a user