-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_structure.py
More file actions
130 lines (107 loc) · 4.35 KB
/
generate_structure.py
File metadata and controls
130 lines (107 loc) · 4.35 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import json
import os
from collections import defaultdict
from typing import Any, Dict, List
DOCS_DIR = "docs/docs/cli-commands"
CLI_OUTPUT_FILE = "docs/.vitepress/cliCommands.ts"
RESOURCE_OUTPUT_FILE = "docs/.vitepress/resources.ts"
def title_case(text: str) -> str:
"""Format file or folder names nicely for sidebar display."""
text = text.replace("-", " ").replace("_", " ")
return text.capitalize()
def generate_cli_commands_structure(base_dir: str) -> List[Dict[str, Any]]:
"""Recursively generate the CLI Commands structure based on markdown files."""
structure: Dict[str, Any] = {}
for root, _, files in os.walk(base_dir):
for file in files:
if file == "overview.md":
continue
if not file.endswith(".md"):
continue
relative_path = os.path.relpath(os.path.join(root, file), base_dir)
parts = relative_path.split(os.sep)
pointer = structure
for part in parts[:-1]:
pointer = pointer.setdefault(part, {})
pointer[parts[-1]] = None
def build_items(
tree: Dict[str, Any], current_path: str = "", depth: int = 0
) -> List[Dict[str, Any]]:
items: List[Dict[str, Any]] = []
for name, subtree in sorted(tree.items()):
path_no_ext = os.path.join(current_path, name).replace(".md", "")
path_url = "/" + os.path.join("cli-commands", path_no_ext).replace(
"\\", "/"
)
if subtree is None:
items.append(
{
"text": os.path.splitext(name)[0],
"link": "/docs" + path_url,
}
)
else:
item: Dict[str, Any] = {
"text": name,
"items": build_items(
subtree, os.path.join(current_path, name), depth + 1
),
}
item["collapsed"] = depth >= 1
items.append(item)
return items
return build_items(structure)
def generate_resource_structure(base_dir: str) -> List[Dict[str, Any]]:
structure: Dict[str, Dict[str, List[Dict[str, str]]]] = defaultdict(
lambda: defaultdict(list)
)
for root, _, files in os.walk(base_dir):
for file in files:
if file == "overview.md" or not file.endswith(".md"):
continue
relative_path = os.path.relpath(os.path.join(root, file), base_dir)
parts = relative_path.split(os.sep)
if len(parts) < 3:
continue
group, command, resource_file = parts[:3]
resource = os.path.splitext(resource_file)[0]
full_path = "/" + os.path.join(
"cli-commands", group, command, resource
).replace("\\", "/")
structure[group][resource].append(
{"text": command, "link": "/docs" + full_path}
)
result: List[Dict[str, Any]] = []
for group, resources in sorted(structure.items()):
group_items: List[Dict[str, Any]] = []
for resource, commands in sorted(resources.items()):
group_items.append(
{
"text": resource,
"items": sorted(commands, key=lambda x: x["text"]),
"collapsed": True,
}
)
result.append(
{"text": group, "items": group_items, "collapsed": False}
)
return result
def write_ts_file(
filename: str, variable: str, items: List[Dict[str, Any]]
) -> None:
content = (
"// This file is automatically generated. Do not edit manually.\n\n"
f"export const {variable} = " + json.dumps(items, indent=4) + ";\n"
)
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w", encoding="utf-8") as f:
f.write(content)
def main() -> None:
cli_commands = generate_cli_commands_structure(DOCS_DIR)
write_ts_file(CLI_OUTPUT_FILE, "cliCommands", cli_commands)
resources = generate_resource_structure(DOCS_DIR)
write_ts_file(RESOURCE_OUTPUT_FILE, "resources", resources)
print(f"✅ CLI Commands structure written to {CLI_OUTPUT_FILE}")
print(f"✅ Resources structure written to {RESOURCE_OUTPUT_FILE}")
if __name__ == "__main__":
main()