Skip to content

Commit 9ad7a1a

Browse files
make build for yaml schemas.
1 parent e7e8af3 commit 9ad7a1a

14 files changed

+258
-139
lines changed

docs/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
BASE_URL ?= "/"
22
VERSION ?= ""
33
SCHEMA_PATTERN = *.schema.json
4-
SCHEMA_INPUT_DIR = ../schemas
4+
SCHEMA_BASE_INPUT_DIR = ../schemas
5+
SCHEMA_INPUT_DIR = $(SCHEMA_BASE_INPUT_DIR)/output
56
BASE_OUTPUT_DIR = _site
67
SCHEMA_OUTPUT_DIR = $(BASE_OUTPUT_DIR)/schemas
78
SHELL = /bin/bash
@@ -29,6 +30,7 @@ build-docs: version
2930
npm run build $(baseurlparam)
3031

3132
build-schemas: clean-schemas
33+
(cd $(SCHEMA_BASE_INPUT_DIR) && make config && make build)
3234
mkdir -p $(SCHEMA_OUTPUT_DIR)
3335
@echo "Finding schema files..."
3436
@find $(SCHEMA_INPUT_DIR) -name "$(SCHEMA_PATTERN)" -print

schemas/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
output

schemas/2025-10/digital-letters-data.schema.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

schemas/2025-10/digital-letters.schema.json

Lines changed: 0 additions & 55 deletions
This file was deleted.

schemas/2025-10/new-letter-from-customer-received-data.schema.json

Lines changed: 0 additions & 30 deletions
This file was deleted.

schemas/2025-10/new-letter-from-customer-received.schema.json

Lines changed: 0 additions & 36 deletions
This file was deleted.

schemas/Makefile

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Variables
2+
SCHEMA_SRC_DIR := src
3+
OUTPUT_DIR := output
4+
SCRIPTS_DIR := scripts
5+
YAML_TO_JSON_SCRIPT := $(SCRIPTS_DIR)/yaml_to_json.py
6+
YAML_SCHEMAS := $(shell find $(SCHEMA_SRC_DIR) -name "*.schema.yaml" -type f)
7+
JSON_OUTPUTS := $(patsubst $(SCHEMA_SRC_DIR)/%.schema.yaml,$(OUTPUT_DIR)/%.schema.json,$(YAML_SCHEMAS))
8+
9+
# Default target
10+
.PHONY: all clean build convert-schemas config check-deps
11+
12+
all: build
13+
14+
# Create output directory
15+
$(OUTPUT_DIR):
16+
@mkdir -p $(OUTPUT_DIR)
17+
18+
# Setup dependencies
19+
config:
20+
@echo "Setting up dependencies for YAML to JSON conversion..."
21+
@echo "Checking Python installation..."
22+
@python3 --version || (echo "Error: Python 3 is required" && exit 1)
23+
@echo "Checking PyYAML installation..."
24+
@python3 -c "import yaml; print('PyYAML is already installed')" 2>/dev/null || \
25+
(echo "Installing PyYAML..." && pip install PyYAML)
26+
@echo "Dependencies setup complete!"
27+
28+
# Check dependencies before building
29+
check-deps:
30+
@python3 -c "import yaml" 2>/dev/null || \
31+
(echo "PyYAML not found. Run 'make config' to install dependencies." && exit 1)
32+
33+
# Convert all YAML schemas to JSON
34+
convert-schemas: check-deps $(OUTPUT_DIR)
35+
@echo "Converting YAML schemas to JSON..."
36+
@for yaml_file in $(YAML_SCHEMAS); do \
37+
relative_path=$${yaml_file#$(SCHEMA_SRC_DIR)/}; \
38+
output_file="$(OUTPUT_DIR)/$${relative_path%.schema.yaml}.schema.json"; \
39+
output_dir=$$(dirname "$$output_file"); \
40+
mkdir -p "$$output_dir"; \
41+
echo "Converting $$yaml_file to $$output_file"; \
42+
python3 $(YAML_TO_JSON_SCRIPT) "$$yaml_file" "$$output_file"; \
43+
done
44+
45+
# Build all schemas
46+
build: convert-schemas
47+
@echo "Building schemas complete!"
48+
@echo "Found $(words $(YAML_SCHEMAS)) YAML schema(s):"
49+
@for schema in $(YAML_SCHEMAS); do echo " - $$schema"; done
50+
@echo "Generated JSON schemas in $(OUTPUT_DIR)/"
51+
52+
# Clean output directory
53+
clean:
54+
@echo "Cleaning output directory..."
55+
@rm -rf $(OUTPUT_DIR)
56+
@echo "Clean complete!"
57+
58+
# Show help
59+
help:
60+
@echo "Available targets:"
61+
@echo " all - Build all schemas (default)"
62+
@echo " config - Setup dependencies (Python, PyYAML)"
63+
@echo " build - Convert YAML schemas from src/ to JSON in output/ (preserving folder structure)"
64+
@echo " list - List all YAML schema files that would be processed"
65+
@echo " clean - Remove output directory"
66+
@echo " help - Show this help message"
67+
@echo ""
68+
@echo "Source schemas are looked for in: $(SCHEMA_SRC_DIR)/"
69+
@echo "Output JSON files are generated in: $(OUTPUT_DIR)/ (with preserved folder structure)"
70+
71+
# List all YAML schema files
72+
list:
73+
@echo "YAML schema files found:"
74+
@for schema in $(YAML_SCHEMAS); do echo " - $$schema"; done
75+
@echo "Total: $(words $(YAML_SCHEMAS)) file(s)"

schemas/scripts/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Schema Scripts
2+
3+
This directory contains utility scripts for schema processing.
4+
5+
## Files
6+
7+
- `yaml_to_json.py` - Converts YAML schema files to JSON format using PyYAML
8+
9+
## Usage
10+
11+
The scripts are automatically used by the Makefile targets. You can also run them directly:
12+
13+
```bash
14+
# Convert a single YAML file to JSON
15+
python3 scripts/yaml_to_json.py input.schema.yaml output.schema.json
16+
```
17+
18+
## Dependencies
19+
20+
- Python 3.x
21+
- PyYAML (automatically installed via `make config`)

schemas/scripts/yaml_to_json.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
"""
3+
YAML to JSON converter for schema files using PyYAML.
4+
"""
5+
import yaml
6+
import json
7+
import sys
8+
9+
def yaml_to_json(yaml_file, json_file):
10+
"""Convert YAML file to JSON file."""
11+
try:
12+
with open(yaml_file, 'r') as f:
13+
data = yaml.safe_load(f)
14+
15+
with open(json_file, 'w') as f:
16+
json.dump(data, f, indent=2)
17+
18+
return True
19+
except Exception as e:
20+
print(f"Error converting {yaml_file}: {e}")
21+
return False
22+
23+
if __name__ == "__main__":
24+
if len(sys.argv) != 3:
25+
print("Usage: python yaml_to_json.py <input.yaml> <output.json>")
26+
sys.exit(1)
27+
28+
yaml_file = sys.argv[1]
29+
json_file = sys.argv[2]
30+
31+
if yaml_to_json(yaml_file, json_file):
32+
print(f"Converted {yaml_file} to {json_file}")
33+
else:
34+
print(f"Failed to convert {yaml_file}")
35+
sys.exit(1)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
$id: https://notify.nhs.uk/schemas/events/digital-letters/2025-10/digital-letters-data.schema.json
2+
$schema: http://json-schema.org/draft-07/schema#
3+
4+
additionalProperties: true
5+
6+
properties:
7+
digital-letter-id:
8+
$comment: Corresponds to ....
9+
description: The unique identifier for the digital letter.
10+
format: uuid
11+
type: string
12+
13+
required:
14+
- digital-letter-id
15+
16+
type: object

0 commit comments

Comments
 (0)