forked from googleapis/google-cloud-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateapis.sh
executable file
·461 lines (407 loc) · 14.3 KB
/
generateapis.sh
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/bin/bash
set -e
source toolversions.sh
declare -r CORE_PROTOS_ROOT=$PROTOBUF_TOOLS_ROOT/tools
# This script generates all APIs from the googleapis/googleapis github repository,
# using the code generator from googleapis/gapic-generator-csharp.
# It will fetch both repositories if necessary.
# (For REGAPIC APIs, it fetches googleapis/googleapis-discovery)
OUTDIR=tmp
# If there's a preconfig file, we assume it has everything we need. We'll
# set both GOOGLEAPIs and GOOGLEAPIS_DISCOVERY based on that, using
# values that will clearly indicate the cause of the failure if a "missing"
# repo is tried.
if [[ "$SYNTHTOOL_PRECONFIG_FILE" != "" ]]
then
declare -r GOOGLEAPIS=$(python - <<END
import json
with open('$SYNTHTOOL_PRECONFIG_FILE') as json_file:
data = json.load(json_file)
repos = data['preclonedRepos']
if 'https://github.com/googleapis/googleapis.git' in repos:
print(repos['https://github.com/googleapis/googleapis.git'])
else:
print('attempt_to_use_googleapis_when_not_configured')
END
)
elif [[ "$SYNTHTOOL_GOOGLEAPIS" != "" ]]
then
declare -r GOOGLEAPIS="$SYNTHTOOL_GOOGLEAPIS"
elif [[ "$SYNTHTOOL_CACHE" != "" ]]
then
declare -r GOOGLEAPIS="$SYNTHTOOL_CACHE/googleapis"
else
declare -r GOOGLEAPIS="$PWD/googleapis"
fi
# Handle either a locally-fetched googleapis-discovery, or
# one specified in a preconfig file.
if [[ "$SYNTHTOOL_PRECONFIG_FILE" != "" ]]
then
declare -r GOOGLEAPIS_DISCOVERY=$(python - <<END
import json
with open('$SYNTHTOOL_PRECONFIG_FILE') as json_file:
data = json.load(json_file)
repos = data['preclonedRepos']
if 'https://github.com/googleapis/googleapis-discovery.git' in repos:
print(repos['https://github.com/googleapis/googleapis-discovery.git'])
else:
print('attempt_to_use_googleapis-discovery_when_not_configured')
END
)
else
declare -r GOOGLEAPIS_DISCOVERY="$PWD/googleapis-discovery"
fi
# Allow pre/post-generation scripts to know where to find the repos
export GOOGLEAPIS
export GOOGLEAPIS_DISCOVERY
fetch_github_repos() {
# We assume that if there's a preconfig file, we're running in autosynth
# and don't need to fetch either. (And if we're using the old SYNTHTOOL_GOOGLEAPIS
# environment variable, we're not building discovery.)
if [[ "$SYNTHTOOL_GOOGLEAPIS" != "" || "$SYNTHTOOL_PRECONFIG_FILE" != "" ]]
then
return 0
fi
if [ -d "$GOOGLEAPIS" ]
then
git -C $GOOGLEAPIS pull -q
else
# Auto-detect whether we're cloning the public or private googleapis repo.
git remote -v | grep -q google-cloud-dotnet-private && repo=googleapis-private || repo=googleapis
git clone https://github.com/googleapis/${repo} $GOOGLEAPIS --depth 1
fi
if [ -d "$GOOGLEAPIS_DISCOVERY" ]
then
git -C $GOOGLEAPIS_DISCOVERY pull -q
else
git clone https://github.com/googleapis/googleapis-discovery $GOOGLEAPIS_DISCOVERY --depth 1
fi
}
delete_generated() {
rm -f "$1/*.g.cs"
}
generate_microgenerator() {
PACKAGE_ID=$1
API_TMP_DIR=$OUTDIR/$PACKAGE_ID
PRODUCTION_PACKAGE_DIR=$API_TMP_DIR/$PACKAGE_ID
GRPC_GENERATION_DIR=$API_TMP_DIR/grpc-$PACKAGE_ID
API_OUT_DIR=apis
API_SRC_DIR=$GOOGLEAPIS/$($PYTHON3 tools/getapifield.py apis/apis.json $PACKAGE_ID protoPath)
# Delete previously-generated files
delete_generated apis/$1/$1
delete_generated apis/$1/$1.Tests
delete_generated apis/$1/$1.Snippets
# If there's exactly one service config file, pass it in. Otherwise, omit it.
GRPC_SERVICE_CONFIG=$(echo $API_SRC_DIR/*.json)
SERVICE_CONFIG_OPTION=
if [[ -f "$GRPC_SERVICE_CONFIG" ]]
then
SERVICE_CONFIG_OPTION=--gapic_opt=grpc-service-config=$GRPC_SERVICE_CONFIG
fi
# Default to "all resources are common" but allow a per-API config file too.
COMMON_RESOURCES_CONFIG=common-resources-config=CommonResourcesConfig.json
# Note: defaulting to "x" is pretty horrible, but it's hard to tell the difference
# between empty and unspecified otherwise.
API_COMMON_RESOURCES_CONFIG=$($PYTHON3 tools/getapifield.py apis/apis.json $PACKAGE_ID commonResourcesConfig --default=x)
if [[ $API_COMMON_RESOURCES_CONFIG != "" && $API_COMMON_RESOURCES_CONFIG != "x" ]]
then
COMMON_RESOURCES_CONFIG=$COMMON_RESOURCES_CONFIG,common-resources-config=$API_COMMON_RESOURCES_CONFIG
fi
COMMON_RESOURCES_OPTION=--gapic_opt=$COMMON_RESOURCES_CONFIG
# Only specify common resource protos for GCP APIs.
# Don't include the file for the ResourceManager API, which genuinely defines these resources.
COMMON_RESOURCES_PROTO=
if [[ $PACKAGE_ID == Google.Cloud.* && $PACKAGE_ID != Google.Cloud.ResourceManager.V3 ]]
then
COMMON_RESOURCES_PROTO=$GOOGLEAPIS/google/cloud/common_resources.proto
fi
mkdir -p $PRODUCTION_PACKAGE_DIR
mkdir -p $GRPC_GENERATION_DIR
# Message and service generation. This doesn't need the common resources,
# and we don't want to pass in the common resources proto because we don't
# want to generate it.
$PROTOC \
--csharp_out=$PRODUCTION_PACKAGE_DIR \
--csharp_opt=base_namespace=$1,file_extension=.g.cs \
--grpc_out=$GRPC_GENERATION_DIR \
--plugin=protoc-gen-grpc=$GRPC_PLUGIN \
-I $GOOGLEAPIS \
-I $CORE_PROTOS_ROOT \
$(find $API_SRC_DIR -name '*.proto') \
2>&1 | grep -v "is unused" || true # Ignore import warnings (and grep exit code)
# Change extension of gRPC-generated files
if compgen -G "$GRPC_GENERATION_DIR/*.cs" > /dev/null
then
for grpc_output in $GRPC_GENERATION_DIR/*.cs
do
mv $grpc_output $(echo $grpc_output | sed -E 's/.cs$/.g.cs/g')
done
mv $GRPC_GENERATION_DIR/* $PRODUCTION_PACKAGE_DIR
fi
rm -rf $GRPC_GENERATION_DIR
# Allow protos to be changed after proto/gRPC generation but before the
# GAPIC microgenerator. This is pretty extreme, but is used for service renaming.
if [[ -f $API_OUT_DIR/$1/midmicrogeneration.sh ]]
then
echo "Running mid-micro-generation script for $1"
(cd $API_OUT_DIR/$1; ./midmicrogeneration.sh)
fi
# Add in any common protos that are available. Due to the way autosynth
# runs, this should not fail if it runs against an older version of googleapis.
COMMON_PROTOS=
if [[ -d $GOOGLEAPIS/google/cloud/common ]]
then
COMMON_PROTOS="$COMMON_PROTOS $GOOGLEAPIS/google/cloud/common/*.proto"
fi
# Client generation. This needs the common resources proto as a reference,
# but it won't generate anything for it.
# The Cloud Common protos are likewise included so that operation result/metadata
# types can use the messages in them, but nothing will be generated.
$PROTOC \
--gapic_out=$API_TMP_DIR \
--gapic_opt=metadata \
$SERVICE_CONFIG_OPTION \
$COMMON_RESOURCES_OPTION \
--plugin=protoc-gen-gapic=$GAPIC_PLUGIN \
-I $GOOGLEAPIS \
-I $CORE_PROTOS_ROOT \
$COMMON_PROTOS \
$(find $API_SRC_DIR -name '*.proto') \
$COMMON_RESOURCES_PROTO \
2>&1 | grep -v "is unused" || true # Ignore import warnings (and grep exit code)
# Remove the newly generated standalone snippets until they are ready for surfacing.
rm -rf $API_TMP_DIR/$1.StandaloneSnippets
# We generate our own project files
rm $(find tmp -name '*.csproj')
# Copy the rest into the right place
cp -r $API_TMP_DIR $API_OUT_DIR
}
# TODO: Use a common function for both kinds of generation
generate_microgenerator_regapic() {
PACKAGE_ID=$1
API_TMP_DIR=$OUTDIR/$PACKAGE_ID
PRODUCTION_PACKAGE_DIR=$API_TMP_DIR/$PACKAGE_ID
GRPC_GENERATION_DIR=$API_TMP_DIR/grpc-$PACKAGE_ID
API_OUT_DIR=apis
API_SRC_DIR=$GOOGLEAPIS_DISCOVERY/$($PYTHON3 tools/getapifield.py apis/apis.json $PACKAGE_ID protoPath)
# Delete previously-generated files
delete_generated apis/$1/$1
delete_generated apis/$1/$1.Tests
delete_generated apis/$1/$1.Snippets
mkdir -p $PRODUCTION_PACKAGE_DIR
mkdir -p $GRPC_GENERATION_DIR
# Message and service generation.
$PROTOC \
--csharp_out=$PRODUCTION_PACKAGE_DIR \
--csharp_opt=base_namespace=$1,file_extension=.g.cs \
--grpc_out=$GRPC_GENERATION_DIR \
--plugin=protoc-gen-grpc=$GRPC_PLUGIN \
-I $GOOGLEAPIS_DISCOVERY \
-I $GOOGLEAPIS \
-I $CORE_PROTOS_ROOT \
$(find $API_SRC_DIR -name '*.proto') \
2>&1 | grep -v "is unused" || true # Ignore import warnings (and grep exit code)
# Change extension of gRPC-generated files
if compgen -G "$GRPC_GENERATION_DIR/*.cs" > /dev/null
then
for grpc_output in $GRPC_GENERATION_DIR/*.cs
do
mv $grpc_output $(echo $grpc_output | sed -E 's/.cs$/.g.cs/g')
done
mv $GRPC_GENERATION_DIR/* $PRODUCTION_PACKAGE_DIR
fi
rm -rf $GRPC_GENERATION_DIR
# Client generation.
$PROTOC \
--gapic_out=$API_TMP_DIR \
--gapic_opt=metadata \
$SERVICE_CONFIG_OPTION \
--plugin=protoc-gen-gapic=$GAPIC_PLUGIN \
-I $GOOGLEAPIS_DISCOVERY \
-I $GOOGLEAPIS \
-I $CORE_PROTOS_ROOT \
$(find $API_SRC_DIR -name '*.proto') \
2>&1 | grep -v "is unused" || true # Ignore import warnings (and grep exit code)
# Remove the newly generated standalone snippets until they are ready for surfacing.
rm -rf $API_TMP_DIR/$1.StandaloneSnippets
# We generate our own project files
rm $(find tmp -name '*.csproj')
# Copy the rest into the right place
cp -r $API_TMP_DIR $API_OUT_DIR
}
generate_proto() {
# Delete previously-generated files
delete_generated apis/$1/$1
API_SRC_DIR=$GOOGLEAPIS/$($PYTHON3 tools/getapifield.py apis/apis.json $1 protoPath)
mkdir -p apis/$1/$1
$PROTOC \
--csharp_out=apis/$1/$1 \
--csharp_opt=base_namespace=$1 \
-I $GOOGLEAPIS \
-I $CORE_PROTOS_ROOT \
$(find $API_SRC_DIR -name '*.proto') \
2>&1 | grep -v "is unused" || true # Ignore import warnings (and grep exit code)
}
generate_protogrpc() {
# Delete previously-generated files
delete_generated apis/$1/$1
# Note: not renaming the gRPC-generated files as carefully here. The only API
# that uses this type (right now) is IAM. We'll notice any changes easily.
API_SRC_DIR=$GOOGLEAPIS/$($PYTHON3 tools/getapifield.py apis/apis.json $1 protoPath)
$PROTOC \
--csharp_out=apis/$1/$1 \
--grpc_out=apis/$1/$1 \
--csharp_opt=file_extension=.g.cs \
-I $GOOGLEAPIS \
-I $CORE_PROTOS_ROOT \
--plugin=protoc-gen-grpc=$GRPC_PLUGIN \
$API_SRC_DIR/*.proto \
2>&1 | grep -v "is unused" || true # Ignore import warnings (and grep exit code)
if compgen -G "apis/$1/$1/*Grpc.cs" > /dev/null
then
for grpc_output in apis/$1/$1/*Grpc.cs
do
mv $grpc_output $(echo $grpc_output | sed -E 's/.cs$/.g.cs/g')
done
fi
}
generate_api() {
PACKAGE=$1
PACKAGE_DIR=apis/$1
if [[ $CHECK_COMPATIBILITY == "true" && -d $PACKAGE_DIR ]]
then
echo "Building existing version of $PACKAGE for compatibility checking"
dotnet build -c Release -f netstandard2.0 -v quiet -nologo -clp:NoSummary -p:SourceLinkCreate=false $PACKAGE_DIR/$PACKAGE
cp $PACKAGE_DIR/$PACKAGE/bin/Release/netstandard2.0/$PACKAGE.dll $OUTDIR
fi
echo "Generating $PACKAGE"
GENERATOR=$($PYTHON3 tools/getapifield.py apis/apis.json $PACKAGE generator)
if [[ -f $PACKAGE_DIR/pregeneration.sh ]]
then
echo "Running pre-generation script for $PACKAGE"
(cd $PACKAGE_DIR; ./pregeneration.sh)
fi
case "$GENERATOR" in
micro)
generate_microgenerator $1
;;
regapic)
generate_microgenerator_regapic $1
;;
proto)
generate_proto $1
;;
protogrpc)
generate_protogrpc $1
;;
*)
echo "Unknown generator: $GENERATOR"
exit 1
esac
if [[ -f $PACKAGE_DIR/postgeneration.patch ]]
then
echo "Applying post-generation patch for $PACKAGE"
(cd $PACKAGE_DIR; git apply postgeneration.patch)
fi
if [[ -f $PACKAGE_DIR/postgeneration.sh ]]
then
echo "Running post-generation script for $PACKAGE"
(cd $PACKAGE_DIR; ./postgeneration.sh)
fi
if [[ $(grep -E "^namespace" apis/$1/$1/*.cs | grep -Ev "namespace ${1}[[:space:]{]*\$") ]]
then
echo "API $1 has broken namespace declarations"
exit 1
fi
if [[ $CHECK_COMPATIBILITY == "true" ]]
then
if [[ -f $OUTDIR/$PACKAGE.dll ]]
then
# In order to skip expensive "build and check compatibility" step,
# first see whether git thinks anything has changed.
# Command line arguments:
# -c core.safecrlf=false: don't warn on line ending changes
# diff: do a diff :)
# -s: suppress output as we only care about the exit code
# -b: ignore whitespace
# --exit-code: set the exit code to 0 for no diff, 1 when a diff is detected
if git -c core.safecrlf=false diff -s -b --exit-code -- $PACKAGE_DIR
then
echo "git detects no change in $PACKAGE; skipping compatibility checking"
else
echo "Building new version of $PACKAGE for compatibility checking"
dotnet build -c Release -f netstandard2.0 -v quiet -nologo -clp:NoSummary -p:SourceLinkCreate=false $PACKAGE_DIR/$PACKAGE
echo ""
echo "Changes in $PACKAGE:"
dotnet run -p tools/Google.Cloud.Tools.CompareVersions -- \
--file1=$OUTDIR/$PACKAGE.dll \
--file2=$PACKAGE_DIR/$PACKAGE/bin/Release/netstandard2.0/$PACKAGE.dll
fi
else
echo ""
echo "$PACKAGE is a new API."
fi
fi
# Record the commit in synth.metadata, using either googleapis or googleapis-discovery
# depending on the generator.
if [[ "$GENERATOR" == "regapic" ]]
then
cat > $PACKAGE_DIR/synth.metadata <<END
{
"sources": [
{
"git": {
"name": "googleapis-discovery",
"remote": "https://github.com/googleapis/googleapis-discovery.git",
"sha": "$(git -C $GOOGLEAPIS_DISCOVERY rev-parse HEAD)"
}
},
{
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "$(git -C $GOOGLEAPIS rev-parse HEAD)"
}
}
]
}
END
else
cat > $PACKAGE_DIR/synth.metadata <<END
{
"sources": [
{
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "$(git -C $GOOGLEAPIS rev-parse HEAD)"
}
}
]
}
END
fi
}
# Entry point
install_protoc
install_microgenerator
install_grpc
fetch_github_repos
OUTDIR=tmp
rm -rf $OUTDIR
mkdir $OUTDIR
CHECK_COMPATIBILITY=false
if [[ $1 == "--check_compatibility" ]]
then
CHECK_COMPATIBILITY=true
shift
fi
packages=$@
if [[ -z "$packages" ]]
then
packages=$($PYTHON3 tools/listapis.py apis/apis.json --test generator)
fi
for package in $packages
do
generate_api $package
done