-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate-code.sh
executable file
·149 lines (127 loc) · 4.89 KB
/
generate-code.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
#!/bin/bash
# Script to generate code from https://github.com/googleapis/google-cloudevents:
# - Classes from Protobuf messages (as per the regular C# generator)
# - Attributes to specify CloudEvent converters
# - CloudEvent type constants
#
# Note that the attributes and CloudEvent type constants are generated for
# all supported serialization formats.
set -e
echo "~ START"
PROTOBUF_VERSION=25.2
# protoc is a native application, so we need to download different zip files
# and use different binaries depending on the OS.
echo "- Determining OS type"
case "$OSTYPE" in
linux*)
PROTOBUF_PLATFORM=linux-x86_64
PROTOC=tmp/protobuf/bin/protoc
;;
win* | msys* | cygwin*)
PROTOBUF_PLATFORM=win64
PROTOC=tmp/protobuf/bin/protoc.exe
;;
darwin*)
PROTOBUF_PLATFORM=osx-x86_64
PROTOC=tmp/protobuf/bin/protoc
;;
*)
echo "Unknown OSTYPE: $OSTYPE"
exit 1
esac
rm -rf tmp
mkdir tmp
if [[ -z $GOOGLE_CLOUDEVENTS ]]
then
echo "- Cloning github.com/googleapis/google-cloudevents into tmp"
# For the moment, just clone google-cloudevents. Later we might make
# it a submodule. We clone quietly, and only with a depth of 1
# as we don't need history.
git clone https://github.com/googleapis/google-cloudevents tmp/google-cloudevents -q --depth 1
GOOGLE_CLOUDEVENTS=tmp/google-cloudevents
else
echo "Using local copy of google-cloudevents: $GOOGLE_CLOUDEVENTS"
fi
# We download a specific version rather than using package managers
# for portability and being able to rely on the version being available
# as soon as it's released on GitHub.
echo "- Downloading protobuf tools"
cd tmp
curl -sSL \
https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOBUF_VERSION/protoc-$PROTOBUF_VERSION-$PROTOBUF_PLATFORM.zip \
--output protobuf.zip
(mkdir protobuf && cd protobuf && unzip -q ../protobuf.zip)
cd ..
chmod +x $PROTOC
echo "- Generating src/Google.Events.Protobuf"
# First delete any previously-generated files
rm -f $(find src/Google.Events.Protobuf -name '*.g.cs')
# Generate the C# messages, just for data protos
# Explanation of flags:
# --csharp_out=...
# We're generating C#
# --csharp_opt=base_namespace=Google.Events.Protobuf
# Controls how directories are generated (C#-specific)
# --csharp_opt=file_extension=.g.cs
# Controls the extension of generated files (C# specific)
# -I tmp/protobuf/include
# Make the google.protobuf protos available (e.g. google.protobuf.Timestamp)
# -I $GOOGLE_CLOUDEVENTS/third_party/googleapis
# Make the common API-related protos available (e.g. google.type.Date)
# -I $GOOGLE_CLOUDEVENTS/proto
# Make the CloudEvent protos themselves available (these are the ones we generate)
# $(find $GOOGLE_CLOUDEVENTS/proto -name data.proto)
# Specifies the protos we want to generate - just the event data messages
echo "- Generating C# from protobuf messages"
$PROTOC \
--csharp_out=src/Google.Events.Protobuf \
--csharp_opt=base_namespace=Google.Events.Protobuf \
--csharp_opt=file_extension=.g.cs \
-I tmp/protobuf/include \
-I $GOOGLE_CLOUDEVENTS/third_party/googleapis \
-I $GOOGLE_CLOUDEVENTS/proto \
$(find $GOOGLE_CLOUDEVENTS/proto -name data.proto)
# protoc doesn't include a copyright message. Add it here.
echo "- Adding copyright to generated code"
cat > tmp/copyright.txt <<END_OF_COPYRIGHT
// Copyright $(date +%Y), Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
END_OF_COPYRIGHT
for generated in $(find src/Google.Events.Protobuf -name '*.g.cs')
do
cat tmp/copyright.txt $generated > tmp/with-copyright
mv tmp/with-copyright $generated
done
# Create a protobuf descriptor set, which we can use to extract the CloudEvent
# types from annotations.
echo "- Generating protobuf extensions for tooling"
$PROTOC \
-I tmp/protobuf/include \
-I $GOOGLE_CLOUDEVENTS/proto \
--csharp_out=tools/Google.Events.Tools.CodeGenerator \
--csharp_opt=file_extension=.g.cs \
$GOOGLE_CLOUDEVENTS/proto/google/events/cloudevent.proto
echo "- Compiling protobuf descriptor set"
$PROTOC \
--descriptor_set_out=tmp/protos.pb \
-I tmp/protobuf/include \
-I $GOOGLE_CLOUDEVENTS/third_party/googleapis \
-I $GOOGLE_CLOUDEVENTS/proto \
$(find $GOOGLE_CLOUDEVENTS/proto -name '*.proto')
# Run code generation from the descriptor set
echo "- Generating additional code from descriptor set"
dotnet run --project tools/Google.Events.Tools.CodeGenerator -- tmp/protos.pb src
echo "- Removing tmp/"
rm -rf tmp
echo "~ DONE ✓"