Skip to content

Commit cb18b87

Browse files
committed
ci: check config schema is up to date
1 parent 2bc09e6 commit cb18b87

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,13 @@ jobs:
386386
package-artifact: false
387387

388388
- name: Check YAML schema
389-
run: npx -y -p ajv-cli -- ajv compile -s docs/mrdocs.schema.json
389+
run: |
390+
# Find python
391+
python=$(command -v python3 || command -v python)
392+
# The schema in this branch is up to date
393+
"$python" ./util/generate-yaml-schema.py --check
394+
# The schema in the docs folder is valid
395+
npx -y -p ajv-cli -- ajv compile -s docs/mrdocs.schema.json
390396
391397
- name: Upload GitHub Release Artifacts
392398
if: ${{ matrix.is-main && matrix.compiler != 'clang' }}

util/generate-yaml-schema.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import sys
33
import os
4+
import argparse
45

56
# NotRequired was added in 3.11
67
if sys.version_info < (3, 11):
@@ -128,15 +129,45 @@ def generate_yaml_schema(config: list[OptionGroup]):
128129

129130
return json.dumps(root, indent=2, sort_keys=True)
130131

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
132142
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))
135147

148+
# Generate the schema
149+
mrdocs_config_path = os.path.join(mrdocs_root_dir, 'src', 'lib', 'Lib', 'ConfigOptions.json')
136150
with open(mrdocs_config_path, 'r') as f:
137151
config = json.loads(f.read())
138-
139152
yaml_schema = generate_yaml_schema(config)
140153

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

Comments
 (0)