Skip to content

Commit 8e397aa

Browse files
committed
Improve mlc doc; support variations and example commands
1 parent 08dcb10 commit 8e397aa

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

automation/script/doc.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
from pathlib import PureWindowsPath, PurePosixPath
66
import copy
7+
from collections import defaultdict
78

89

910
def generate_doc(self_module, input_params):
@@ -87,6 +88,8 @@ def generate_docs(metadata, script_path, generic_inputs):
8788
script_input_mapping = metadata.get('input_mapping', {})
8889
script_default_env = metadata.get('default_env', {})
8990
script_input_description = metadata.get('input_description', {})
91+
script_variations = metadata.get('variations', {})
92+
default_version = metadata.get('default_version')
9093

9194
r = get_run_readme(
9295
tags_string,
@@ -101,6 +104,25 @@ def generate_docs(metadata, script_path, generic_inputs):
101104

102105
doc_content += run_readme
103106

107+
r = get_variations_readme(script_variations)
108+
if r['return'] > 0:
109+
return r
110+
variations_readme = r['variations_readme']
111+
doc_content += variations_readme
112+
113+
example_commands_file = os.path.join(script_path, 'example-commands.md')
114+
115+
# Read the file content if it exists
116+
if os.path.exists(example_commands_file):
117+
with open(example_commands_file, "r") as f:
118+
commands_readme = f.read()
119+
else:
120+
commands_readme = ''
121+
122+
# Append the content to doc_content
123+
doc_content += commands_readme
124+
125+
104126
readme_path = os.path.join(readme_dir, "README.md")
105127
with open(readme_path, "w") as f:
106128
f.write(doc_content)
@@ -109,6 +131,71 @@ def generate_docs(metadata, script_path, generic_inputs):
109131
return {'return': 0}
110132

111133

134+
def get_variations_readme(variations):
135+
136+
137+
# Data structures
138+
aliases = {} # alias name -> real target
139+
alias_reverse = defaultdict(list) # real target -> [aliases]
140+
bases = defaultdict(list) # variation -> list of base variations
141+
variation_groups = {} # variation -> group
142+
main_variations = {} # all actual variations to process
143+
144+
# First pass: classify and build maps
145+
for name, attrs in variations.items():
146+
if "," in name:
147+
continue # ⛔ Skip composite variations
148+
if not isinstance(attrs, dict):
149+
main_variations[name] = {}
150+
continue
151+
if "alias" in attrs:
152+
aliases[name] = attrs["alias"]
153+
alias_reverse[attrs["alias"]].append(name)
154+
else:
155+
main_variations[name] = attrs
156+
# group
157+
group = attrs.get("group", "ungrouped")
158+
if isinstance(group, list):
159+
group = group[0] if group else "ungrouped"
160+
variation_groups[name] = group
161+
# base
162+
base = attrs.get("base", [])
163+
if isinstance(base, str):
164+
base = [base]
165+
bases[name] = base
166+
167+
# Build grouped markdown output
168+
grouped_output = defaultdict(list)
169+
170+
for var in sorted(main_variations.keys()):
171+
group = variation_groups.get(var, "ungrouped")
172+
line = f"- `{var}`"
173+
174+
if var.endswith(".#"):
175+
line += " _(dynamic)_"
176+
177+
if alias_reverse.get(var):
178+
alias_str = ", ".join(sorted(alias_reverse[var]))
179+
line += f" (alias: {alias_str})"
180+
181+
if bases.get(var):
182+
base_str = ", ".join(bases[var])
183+
line += f" (base: {base_str})"
184+
185+
grouped_output[group].append(line)
186+
187+
# Write Markdown
188+
md_lines = ["## Variations\n"]
189+
190+
for group in sorted(grouped_output):
191+
md_lines.append(f"### {group.capitalize()}\n")
192+
md_lines.extend(grouped_output[group])
193+
md_lines.append("") # blank line between groups
194+
195+
return {'return': 0, 'variations_readme': "\n".join(md_lines)}
196+
197+
198+
112199
def get_run_readme(tags, input_mapping, input_description,
113200
default_env, generic_inputs):
114201
run_readme = f"""## Run Commands

0 commit comments

Comments
 (0)