|
1 | 1 | import json
|
2 | 2 | import sys
|
3 | 3 | import os
|
| 4 | +import argparse |
4 | 5 |
|
5 | 6 | # NotRequired was added in 3.11
|
6 | 7 | if sys.version_info < (3, 11):
|
@@ -128,15 +129,45 @@ def generate_yaml_schema(config: list[OptionGroup]):
|
128 | 129 |
|
129 | 130 | return json.dumps(root, indent=2, sort_keys=True)
|
130 | 131 |
|
131 |
| -if __name__ == "__main__": |
| 132 | + |
| 133 | +def main(): |
| 134 | + # Parse arguments |
| 135 | + parser = argparse.ArgumentParser(description="Generate or check the MrDocs YAML schema.") |
| 136 | + parser.add_argument('--output', type=str, help="Path to the output schema file.") |
| 137 | + parser.add_argument('--check', action='store_true', default=False, |
| 138 | + help="Check if the generated schema matches the existing file.") |
| 139 | + args = parser.parse_args() |
| 140 | + |
| 141 | + # Determine the schema path |
132 | 142 | mrdocs_root_dir = os.path.join(os.path.dirname(__file__), '..')
|
133 |
| - mrdocs_schema_path = os.path.join(mrdocs_root_dir, 'docs', 'mrdocs.schema.json') |
134 |
| - mrdocs_config_path = os.path.join(mrdocs_root_dir, 'src', 'lib', 'Lib', 'ConfigOptions.json') |
| 143 | + default_schema_path = os.path.join(mrdocs_root_dir, 'docs', 'mrdocs.schema.json') |
| 144 | + mrdocs_schema_path = args.output if args.output else default_schema_path |
| 145 | + if not os.path.isabs(mrdocs_schema_path): |
| 146 | + mrdocs_schema_path = os.path.abspath(os.path.join(mrdocs_root_dir, mrdocs_schema_path)) |
135 | 147 |
|
| 148 | + # Generate the schema |
| 149 | + mrdocs_config_path = os.path.join(mrdocs_root_dir, 'src', 'lib', 'Lib', 'ConfigOptions.json') |
136 | 150 | with open(mrdocs_config_path, 'r') as f:
|
137 | 151 | config = json.loads(f.read())
|
138 |
| - |
139 | 152 | yaml_schema = generate_yaml_schema(config)
|
140 | 153 |
|
141 |
| - with open(mrdocs_schema_path, 'w') as f: |
142 |
| - f.write(yaml_schema) |
| 154 | + if args.check: |
| 155 | + # Check if the generated schema matches the existing schema |
| 156 | + with open(mrdocs_schema_path, 'r') as f: |
| 157 | + existing_schema = f.read() |
| 158 | + if yaml_schema != existing_schema: |
| 159 | + print( |
| 160 | + "The generated schema does not match the existing schema." |
| 161 | + "Please run `util/generate-yaml-schema.py` to update the schema." |
| 162 | + ) |
| 163 | + sys.exit(1) |
| 164 | + else: |
| 165 | + print("The generated schema matches the existing schema.") |
| 166 | + else: |
| 167 | + # Write the schema to the file |
| 168 | + with open(mrdocs_schema_path, 'w') as f: |
| 169 | + f.write(yaml_schema) |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + main() |
0 commit comments