Skip to content
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

Adjust hassfest.manifest based on config.action #100577

Merged
merged 2 commits into from Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions script/hassfest/manifest.py
Expand Up @@ -366,15 +366,19 @@ def _sort_manifest_keys(key: str) -> str:
return _SORT_KEYS.get(key, key)


def sort_manifest(integration: Integration) -> bool:
def sort_manifest(integration: Integration, config: Config) -> 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))
if config.action == "generate":
integration.manifest_path.write_text(json.dumps(manifest, indent=2))
text = "have been sorted"
else:
text = "are not sorted correctly"
integration.add_error(
"manifest",
"Manifest keys have been sorted: domain, name, then alphabetical order",
f"Manifest keys {text}: domain, name, then alphabetical order",
)
return True
return False
Expand All @@ -387,9 +391,9 @@ def validate(integrations: dict[str, Integration], config: Config) -> None:
for integration in integrations.values():
validate_manifest(integration, core_components_dir)
if not integration.errors:
if sort_manifest(integration):
if sort_manifest(integration, config):
manifests_resorted.append(integration.manifest_path)
if manifests_resorted:
if config.action == "generate" and manifests_resorted:
subprocess.run(
["pre-commit", "run", "--hook-stage", "manual", "prettier", "--files"]
+ manifests_resorted,
Expand Down
4 changes: 2 additions & 2 deletions script/hassfest/model.py
Expand Up @@ -4,7 +4,7 @@
from dataclasses import dataclass, field
import json
import pathlib
from typing import Any
from typing import Any, Literal


@dataclass
Expand All @@ -26,7 +26,7 @@ class Config:

specific_integrations: list[pathlib.Path] | None
root: pathlib.Path
action: str
action: Literal["validate", "generate"]
requirements: bool
errors: list[Error] = field(default_factory=list)
cache: dict[str, Any] = field(default_factory=dict)
Expand Down