mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +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 __future__ import annotations
|
||||||
|
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import subprocess
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
@ -361,8 +363,39 @@ def validate_manifest(integration: Integration, core_components_dir: Path) -> No
|
|||||||
validate_version(integration)
|
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:
|
def validate(integrations: dict[str, Integration], config: Config) -> None:
|
||||||
"""Handle all integrations manifests."""
|
"""Handle all integrations manifests."""
|
||||||
core_components_dir = config.root / "homeassistant/components"
|
core_components_dir = config.root / "homeassistant/components"
|
||||||
|
manifests_resorted = []
|
||||||
for integration in integrations.values():
|
for integration in integrations.values():
|
||||||
validate_manifest(integration, core_components_dir)
|
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
|
path: pathlib.Path
|
||||||
_manifest: dict[str, Any] | None = None
|
_manifest: dict[str, Any] | None = None
|
||||||
|
manifest_path: pathlib.Path | None = None
|
||||||
errors: list[Error] = field(default_factory=list)
|
errors: list[Error] = field(default_factory=list)
|
||||||
warnings: list[Error] = field(default_factory=list)
|
warnings: list[Error] = field(default_factory=list)
|
||||||
translated_name: bool = False
|
translated_name: bool = False
|
||||||
@ -223,3 +224,4 @@ class Integration:
|
|||||||
return
|
return
|
||||||
|
|
||||||
self._manifest = manifest
|
self._manifest = manifest
|
||||||
|
self.manifest_path = manifest_path
|
||||||
|
Loading…
x
Reference in New Issue
Block a user