-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.sh
More file actions
executable file
Β·226 lines (185 loc) Β· 7.29 KB
/
generate.sh
File metadata and controls
executable file
Β·226 lines (185 loc) Β· 7.29 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
# Unified code generation script for Computor
# Generates TypeScript types, clients, validators, schemas, and error codes
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck disable=SC1090
source "${ROOT_DIR}/scripts/utilities/ensure_venv.sh"
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Print usage
usage() {
cat << EOF
Usage: bash generate.sh [OPTIONS] [TARGET]
Generate code artifacts for the Computor platform.
TARGETS:
types Generate TypeScript interfaces from Pydantic models
clients Generate TypeScript API client classes
python-client Generate Python HTTP client library
schemas Generate JSON schemas for meta.yaml and test.yaml
constants Generate TypeScript constants from Python constants
error-codes Generate error code definitions (TypeScript, JSON, Markdown)
all Generate all artifacts (default)
OPTIONS:
-h, --help Show this help message
-w, --watch Watch mode for types generation
--no-error-codes Skip error codes generation when using 'types' or 'all'
EXAMPLES:
bash generate.sh # Generate all artifacts
bash generate.sh types # Generate only TypeScript types
bash generate.sh types --watch # Generate types in watch mode
bash generate.sh clients # Generate only clients
bash generate.sh all --no-error-codes # Generate all except error codes
EOF
exit 0
}
# Parse arguments
TARGETS=()
WATCH_MODE=false
SKIP_ERROR_CODES=false
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
;;
-w|--watch)
WATCH_MODE=true
shift
;;
--no-error-codes)
SKIP_ERROR_CODES=true
shift
;;
*)
TARGETS+=("$1")
shift
;;
esac
done
# Default to 'all' if no targets specified
if [ ${#TARGETS[@]} -eq 0 ]; then
TARGETS=("all")
fi
# Ensure virtual environment
ensure_venv
# Determine Python binary
PYTHON_BIN="${PYTHON_BIN:-python}"
if ! command -v "${PYTHON_BIN}" >/dev/null 2>&1; then
PYTHON_BIN="python3"
fi
# Set PYTHONPATH
export PYTHONPATH="${ROOT_DIR}/computor-backend/src:${ROOT_DIR}/computor-types/src:${ROOT_DIR}/computor-cli/src${PYTHONPATH:+:${PYTHONPATH}}"
# Function to generate TypeScript types
generate_types() {
echo -e "${BLUE}π Generating TypeScript interfaces from Pydantic models...${NC}"
local args=()
if [ "$WATCH_MODE" = true ]; then
args+=("--watch")
fi
cd "${ROOT_DIR}" && \
"${PYTHON_BIN}" "${ROOT_DIR}/computor-backend/src/computor_backend/scripts/generate_typescript_interfaces.py" ${args[@]+"${args[@]}"}
echo -e "${GREEN}β
TypeScript interfaces generated successfully!${NC}"
echo -e "π Check computor-web/src/generated/types/ for the generated files\n"
}
# Function to generate TypeScript clients
generate_clients() {
echo -e "${BLUE}π οΈ Generating TypeScript API clients...${NC}"
cd "${ROOT_DIR}" && \
"${PYTHON_BIN}" "${ROOT_DIR}/computor-backend/src/computor_backend/scripts/generate_typescript_clients.py" "$@"
echo -e "${GREEN}β
TypeScript API clients generated successfully!${NC}"
echo -e "π Check computor-web/src/generated/clients/ for the generated files\n"
}
# Function to generate Python client
generate_python_client() {
echo -e "${BLUE}π Generating Python HTTP client library...${NC}"
cd "${ROOT_DIR}" && \
"${PYTHON_BIN}" "${ROOT_DIR}/computor-backend/src/computor_backend/scripts/generate_python_clients.py" "$@"
echo -e "${GREEN}β
Python client library generated successfully!${NC}"
echo -e "π Check computor-client/src/computor_client/endpoints/ for the generated files\n"
}
# Function to generate JSON schemas (meta.yaml, test.yaml)
generate_schemas() {
echo -e "${BLUE}π Generating JSON schemas for YAML validation...${NC}"
cd "${ROOT_DIR}" && \
"${PYTHON_BIN}" "${ROOT_DIR}/computor-backend/src/computor_backend/scripts/generate_json_schema.py"
echo -e "${GREEN}β
JSON schemas generated successfully!${NC}"
echo -e "π Check computor-web/src/generated/schemas/ for the generated files\n"
}
# Function to generate TypeScript constants
generate_constants() {
echo -e "${BLUE}π¦ Generating TypeScript constants from Python...${NC}"
cd "${ROOT_DIR}" && \
"${PYTHON_BIN}" "${ROOT_DIR}/computor-backend/src/computor_backend/scripts/generate_typescript_constants.py"
echo -e "${GREEN}β
TypeScript constants generated successfully!${NC}\n"
}
# Function to generate error codes
generate_error_codes() {
if [ "$SKIP_ERROR_CODES" = true ]; then
echo -e "${YELLOW}βοΈ Skipping error code generation (--no-error-codes flag)${NC}\n"
return
fi
echo -e "${BLUE}π¨ Generating error code definitions...${NC}"
# Check if error_registry.yaml exists
if [ ! -f "${ROOT_DIR}/computor-backend/error_registry.yaml" ]; then
echo -e "${YELLOW}β οΈ Warning: error_registry.yaml not found in computor-backend/. Skipping error codes.${NC}\n"
return
fi
# Check if PyYAML is installed
if ! "${PYTHON_BIN}" -c "import yaml" 2>/dev/null; then
echo "Installing PyYAML..."
pip install pyyaml
fi
cd "${ROOT_DIR}" && \
"${PYTHON_BIN}" "${ROOT_DIR}/computor-backend/src/computor_backend/scripts/generate_error_codes.py"
echo -e "${GREEN}β
Error code definitions generated successfully!${NC}\n"
}
# Main generation logic
echo -e "${GREEN}ββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "${GREEN}β Computor Code Generation Tool β${NC}"
echo -e "${GREEN}ββββββββββββββββββββββββββββββββββββββββββ${NC}\n"
for target in "${TARGETS[@]}"; do
case $target in
types)
generate_types
if [ "$SKIP_ERROR_CODES" = false ]; then
generate_error_codes
fi
;;
clients)
generate_clients
;;
python-client)
generate_python_client
;;
schemas)
generate_schemas
;;
constants)
generate_constants
;;
error-codes)
generate_error_codes
;;
all)
generate_types
generate_schemas
generate_constants
generate_clients
generate_python_client
if [ "$SKIP_ERROR_CODES" = false ]; then
generate_error_codes
fi
;;
*)
echo -e "${YELLOW}β οΈ Unknown target: $target${NC}"
echo "Run 'bash generate.sh --help' for usage information"
exit 1
;;
esac
done
echo -e "${GREEN}ββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "${GREEN}β Generation Complete! β¨ β${NC}"
echo -e "${GREEN}ββββββββββββββββββββββββββββββββββββββββββ${NC}"