-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathgenerate_models.sh
More file actions
executable file
·81 lines (64 loc) · 2.2 KB
/
generate_models.sh
File metadata and controls
executable file
·81 lines (64 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
# Generate Pydantic models from UCP JSON Schemas
# Ensure we are in the script's directory
cd "$(dirname "$0")" || exit
# Add ~/.local/bin to PATH for uv
export PATH="$HOME/.local/bin:$PATH"
# Check if git is installed
if ! command -v git &> /dev/null; then
echo "Error: git not found. Please install git."
exit 1
fi
# UCP Version to use (if provided, use release/$1 branch; otherwise, use main)
if [ -z "$1" ]; then
BRANCH="main"
echo "No version specified, cloning main branch..."
else
BRANCH="release/$1"
echo "Cloning version $1 (branch: $BRANCH)..."
fi
# Ensure ucp directory is clean before cloning
rm -rf ucp
git clone -b "$BRANCH" --depth 1 https://github.com/Universal-Commerce-Protocol/ucp ucp
# Output directory
OUTPUT_DIR="src/ucp_sdk/models/schemas"
# Schema directory (relative to this script)
SCHEMA_DIR="ucp/source/schemas"
echo "Preprocessing schemas..."
uv run python preprocess_schemas.py
echo "Generating Pydantic models from preprocessed schemas..."
# Check if uv is installed
if ! command -v uv &> /dev/null; then
echo "Error: uv not found."
echo "Please install uv: curl -LsSf https://astral.sh/uv/install.sh | sh"
exit 1
fi
# Ensure output directory is clean
rm -r -f "$OUTPUT_DIR"
mkdir -p "$OUTPUT_DIR"
# Run generation using uv
# We use --use-schema-description to use descriptions from JSON schema as docstrings
# We use --field-constraints to include validation constraints (regex, min/max, etc.)
# Note: Formatting is done as a post-processing step.
uv run \
--link-mode=copy \
--extra-index-url https://pypi.org/simple python \
-m datamodel_code_generator \
--input "$SCHEMA_DIR" \
--input-file-type jsonschema \
--output "$OUTPUT_DIR" \
--output-model-type pydantic_v2.BaseModel \
--use-schema-description \
--field-constraints \
--use-field-description \
--enum-field-as-literal all \
--disable-timestamp \
--use-double-quotes \
--no-use-annotated \
--allow-extra-fields \
--custom-template-dir templates \
--additional-imports pydantic.ConfigDict
echo "Formatting generated models..."
uv run ruff format
uv run ruff check --fix "$OUTPUT_DIR"
echo "Done. Models generated in $OUTPUT_DIR"