Created
July 13, 2024 11:01
-
-
Save brokenpip3/4597429225217b4afd811c4ea68f27e7 to your computer and use it in GitHub Desktop.
small script to generate nix module doc from code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import re | |
| import argparse | |
| def parse_nix_module(file_path): | |
| options = {} | |
| if not os.path.exists(file_path): | |
| print(f"File not found: {file_path}") | |
| return options | |
| with open(file_path, 'r') as file: | |
| content = file.read() | |
| option_pattern = re.compile(r'([a-zA-Z0-9_.-]+)\s*=\s*lib\.mkOption\s*{([^}]*)}', re.DOTALL) | |
| desc_pattern = re.compile(r'description\s*=\s*\'\'\n\s*(.*?)\s*\'\'', re.DOTALL) | |
| type_pattern = re.compile(r'type\s*=\s*lib\.types\.([a-zA-Z0-9_.-]+)') | |
| default_pattern = re.compile(r'default\s*=\s*(.*?);', re.DOTALL) | |
| for match in option_pattern.finditer(content): | |
| option_name = match.group(1).strip() | |
| option_body = match.group(2).strip() | |
| description = desc_pattern.search(option_body) | |
| option_type = type_pattern.search(option_body) | |
| default = default_pattern.search(option_body) | |
| options[option_name] = { | |
| 'description': description.group(1).strip() if description else '', | |
| 'type': option_type.group(1).strip() if option_type else '', | |
| 'default': default.group(1).strip() if default else '', | |
| } | |
| return options | |
| def generate_readme(module_name, options): | |
| readme_content = f"# {module_name} Module Options\n\n" | |
| readme_content += "| Option | Description | Type | Default |\n" | |
| readme_content += "|--------|-------------|------|---------|\n" | |
| for option, details in options.items(): | |
| readme_content += f"| `{option}` | {details['description']} | {details['type']} | {details['default']} |\n" | |
| return readme_content | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Parse a Nix module file and generate a README with all options.") | |
| parser.add_argument('module_path', type=str, help="Path to the Nix module file") | |
| parser.add_argument('-w', '--write', action='store_true', help="Write the output to README.md instead of printing") | |
| args = parser.parse_args() | |
| module_path = args.module_path | |
| module_name = os.path.basename(module_path).replace('.nix', '') | |
| options = parse_nix_module(module_path) | |
| if options: | |
| readme = generate_readme(module_name, options) | |
| if args.write: | |
| with open('README.md', 'w') as readme_file: | |
| readme_file.write(readme) | |
| print("README.md has been generated.") | |
| else: | |
| print(readme) | |
| else: | |
| print("No options found or file not found.") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment