-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_web_types.py
More file actions
107 lines (87 loc) · 3.82 KB
/
generate_web_types.py
File metadata and controls
107 lines (87 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
"""
Generate aggregated web-types.json from individual component web-types files.
This script scans the components directory for *.web-types.json files and
combines them into a single web-types.json file that IntelliJ can use for
autocompletion and validation of Jinja2 components.
"""
import json
import os
from pathlib import Path
from typing import Dict, List, Any
def find_component_web_types(components_dir: Path) -> List[Path]:
"""Find all web-types.json files in the components directory."""
return list(components_dir.glob("*.web-types.json"))
def load_component_web_type(file_path: Path) -> Dict[str, Any]:
"""Load a single component web-type definition."""
with open(file_path, 'r', encoding='utf-8') as f:
return json.load(f)
def generate_main_web_types(components: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Generate the main web-types.json structure."""
return {
"$schema": "https://raw.githubusercontent.com/JetBrains/web-types/master/schema/web-types.json",
"name": "jinja-roos-components",
"version": "1.0.0",
"description": "RVO Design System components for Jinja2 templates",
"contributions": {
"html": {
"elements": components,
"description-markup": "markdown",
"types-syntax": "typescript"
}
}
}
def add_component_defaults(component: Dict[str, Any]) -> Dict[str, Any]:
"""Add default values and structure to component definition."""
# Ensure component has required fields
if "name" not in component:
raise ValueError(f"Component missing 'name' field")
# Add default description if missing
if "description" not in component:
component["description"] = f"RVO {component['name']} component"
# Ensure attributes exist
if "attributes" not in component:
component["attributes"] = []
return component
def main():
# Setup paths
root_dir = Path(__file__).parent
components_dir = root_dir / "jinja_roos_components" / "templates" / "components"
output_file = root_dir / "web-types.json"
# Find all component web-types files
web_type_files = find_component_web_types(components_dir)
if not web_type_files:
print(f"No web-types.json files found in {components_dir}")
return
print(f"Found {len(web_type_files)} component web-type definitions")
# Load all component definitions
components = []
for file_path in sorted(web_type_files):
try:
print(f" Loading: {file_path.name}")
component = load_component_web_type(file_path)
component = add_component_defaults(component)
components.append(component)
except Exception as e:
print(f" ERROR loading {file_path.name}: {e}")
continue
# Generate main web-types structure
web_types = generate_main_web_types(components)
# Write output file
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(web_types, f, indent=2, ensure_ascii=False)
print(f"\nGenerated {output_file}")
print(f"Total components: {len(components)}")
# Also generate a package.json entry if needed
package_json_path = root_dir / "package.json"
if package_json_path.exists():
with open(package_json_path, 'r', encoding='utf-8') as f:
package_data = json.load(f)
# Add web-types reference if not present
if "web-types" not in package_data:
package_data["web-types"] = "./web-types.json"
with open(package_json_path, 'w', encoding='utf-8') as f:
json.dump(package_data, f, indent=2, ensure_ascii=False)
print(f"Updated package.json with web-types reference")
if __name__ == "__main__":
main()