-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild-schema.sh
More file actions
executable file
·176 lines (153 loc) · 5.49 KB
/
build-schema.sh
File metadata and controls
executable file
·176 lines (153 loc) · 5.49 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
#!/bin/bash
# Build HyperFleet OpenAPI Schema
# Usage: ./build-schema.sh <provider> [--swagger|--openapi2]
# provider: core, gcp, or any provider with aliases-{provider}.tsp file (required, must be first argument)
# --swagger, --openapi2: Also generate OpenAPI 2.0 (Swagger) format
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Parse arguments
GENERATE_SWAGGER=false
# Provider is required and must be the first argument
if [ $# -lt 1 ]; then
echo -e "${RED}Error: Provider argument is required${NC}"
echo "Usage: $0 <provider> [--swagger|--openapi2]"
echo " provider: core, gcp, or any provider with aliases-{provider}.tsp file"
echo " --swagger, --openapi2: Also generate OpenAPI 2.0 (Swagger) format"
exit 1
fi
PROVIDER="$1"
shift
# Check if provider looks like an option (starts with -)
if [[ "$PROVIDER" == -* ]]; then
echo -e "${RED}Error: Provider must be the first argument${NC}"
echo "Usage: $0 <provider> [--swagger|--openapi2]"
exit 1
fi
# Parse remaining options
for arg in "$@"; do
case $arg in
--swagger|--openapi2)
GENERATE_SWAGGER=true
;;
-*)
echo -e "${RED}Error: Unknown option: $arg${NC}"
echo "Usage: $0 <provider> [--swagger|--openapi2]"
exit 1
;;
*)
echo -e "${RED}Error: Unexpected argument: $arg${NC}"
echo "Usage: $0 <provider> [--swagger|--openapi2]"
exit 1
;;
esac
done
# Script directory (where the script is located)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Define the aliases file for the provider
ALIASES_FILE="aliases-${PROVIDER}.tsp"
# Check if the aliases file exists
if [ ! -f "$ALIASES_FILE" ]; then
echo -e "${RED}Error: Provider aliases file not found: ${ALIASES_FILE}${NC}"
echo ""
echo "Available providers:"
for file in aliases-*.tsp; do
if [ -f "$file" ]; then
provider_name=$(echo "$file" | sed 's/aliases-\(.*\)\.tsp/\1/')
echo " - $provider_name"
fi
done
exit 1
fi
# Check if main.tsp exists
if [ ! -f "main.tsp" ]; then
echo -e "${RED}Error: main.tsp not found in current directory${NC}"
exit 1
fi
# Check if tsp command is available
if ! command -v tsp &> /dev/null; then
echo -e "${RED}Error: tsp command not found. Please install TypeSpec compiler.${NC}"
echo "Install with: npm install -g @typespec/compiler"
exit 1
fi
# Check if api-spec-converter is available when swagger output is requested
if [ "$GENERATE_SWAGGER" = true ]; then
if ! npx api-spec-converter --version &> /dev/null; then
echo -e "${RED}Error: api-spec-converter not found. Please install it.${NC}"
echo "Install with: npm install --save-dev api-spec-converter"
exit 1
fi
fi
echo -e "${GREEN}Building HyperFleet API schema for provider: ${PROVIDER}${NC}"
if [ "$GENERATE_SWAGGER" = true ]; then
echo -e "${GREEN}Output formats: OpenAPI 3.0 + OpenAPI 2.0 (Swagger)${NC}"
else
echo -e "${GREEN}Output format: OpenAPI 3.0${NC}"
fi
echo ""
# Step 1: Re-link aliases.tsp to the provider-specific aliases file
echo -e "${YELLOW}Step 1: Linking aliases.tsp to ${ALIASES_FILE}${NC}"
if [ -L "aliases.tsp" ] || [ -f "aliases.tsp" ]; then
rm -f aliases.tsp
fi
ln -sf "$ALIASES_FILE" aliases.tsp
echo -e "${GREEN}✓ Linked aliases.tsp → ${ALIASES_FILE}${NC}"
echo ""
# Step 2: Create output directory for the provider
OUTPUT_DIR="schemas/${PROVIDER}"
echo -e "${YELLOW}Step 2: Preparing output directory...${NC}"
mkdir -p "$OUTPUT_DIR"
echo -e "${GREEN}✓ Created output directory: ${OUTPUT_DIR}${NC}"
echo ""
# Step 3: Compile TypeSpec to generate OpenAPI schema
echo -e "${YELLOW}Step 3: Compiling TypeSpec...${NC}"
TEMP_OUTPUT_DIR="tsp-output-${PROVIDER}"
# Cleanup function to remove temporary directory on exit
cleanup() {
if [ -d "$TEMP_OUTPUT_DIR" ]; then
rm -rf "$TEMP_OUTPUT_DIR"
fi
}
trap cleanup EXIT
if tsp compile main.tsp --output-dir "$TEMP_OUTPUT_DIR"; then
# Move the generated schema to the provider-specific directory
if [ -f "${TEMP_OUTPUT_DIR}/schema/openapi.yaml" ]; then
mv "${TEMP_OUTPUT_DIR}/schema/openapi.yaml" "${OUTPUT_DIR}/openapi.yaml"
echo ""
echo -e "${GREEN}✓ Successfully generated OpenAPI 3.0 schema${NC}"
echo -e "${GREEN}Output: ${OUTPUT_DIR}/openapi.yaml${NC}"
else
echo ""
echo -e "${RED}✗ Generated schema file not found at expected location${NC}"
echo "Expected: ${TEMP_OUTPUT_DIR}/schema/openapi.yaml"
exit 1
fi
else
echo ""
echo -e "${RED}✗ Failed to compile TypeSpec${NC}"
exit 1
fi
# Step 4: Convert to OpenAPI 2.0 (Swagger) if requested
if [ "$GENERATE_SWAGGER" = true ]; then
echo ""
echo -e "${YELLOW}Step 4: Converting to OpenAPI 2.0 (Swagger)...${NC}"
if npx api-spec-converter \
--from=openapi_3 \
--to=swagger_2 \
--syntax=yaml \
"${OUTPUT_DIR}/openapi.yaml" > "${OUTPUT_DIR}/swagger.yaml" 2>/dev/null; then
echo -e "${GREEN}✓ Successfully generated OpenAPI 2.0 (Swagger) schema${NC}"
echo -e "${GREEN}Output: ${OUTPUT_DIR}/swagger.yaml${NC}"
else
echo -e "${RED}✗ Failed to convert to OpenAPI 2.0 (Swagger)${NC}"
echo "The OpenAPI 3.0 schema may contain features not supported in OpenAPI 2.0"
rm -f "${OUTPUT_DIR}/swagger.yaml"
exit 1
fi
fi
echo ""
echo -e "${GREEN}Build complete!${NC}"