-
Notifications
You must be signed in to change notification settings - Fork 815
/
Copy pathgenerate-protobuf.sh
executable file
·53 lines (40 loc) · 2.38 KB
/
generate-protobuf.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
#!/bin/bash
set -euo pipefail
# We use the shaded protobuf JAR from the protobuf-shaded module.
# I could not figure out how to use a protoc Maven plugin to use the shaded module, so I ran this command to generate the sources manually.
TARGET_DIR=$1
PROTO_DIR=src/main/protobuf
PROTOBUF_VERSION_STRING=$2
PROTOBUF_VERSION="${PROTOBUF_VERSION_STRING//_/.}"
echo "Generating protobuf sources for version $PROTOBUF_VERSION in $TARGET_DIR"
rm -rf $TARGET_DIR
mkdir -p $TARGET_DIR
rm -rf $PROTO_DIR || true
mkdir -p $PROTO_DIR
OLD_PACKAGE=$(sed -nE 's/import (io.prometheus.metrics.expositionformats.generated.*).Metrics;/\1/p' src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java)
PACKAGE="io.prometheus.metrics.expositionformats.generated.com_google_protobuf_${PROTOBUF_VERSION_STRING}"
if [[ $OLD_PACKAGE != "$PACKAGE" ]]; then
echo "Replacing package $OLD_PACKAGE with $PACKAGE in all java files"
find .. -type f -name "*.java" -exec sed -i "s/$OLD_PACKAGE/$PACKAGE/g" {} +
fi
curl -sL https://raw.githubusercontent.com/prometheus/client_model/master/io/prometheus/client/metrics.proto -o $PROTO_DIR/metrics.proto
sed -i "s/java_package = \"io.prometheus.client\"/java_package = \"$PACKAGE\"/" $PROTO_DIR/metrics.proto
protoc --java_out $TARGET_DIR $PROTO_DIR/metrics.proto
sed -i '1 i\//CHECKSTYLE:OFF: checkstyle' $(find src/main/generated/io -type f)
sed -i -e $'$a\\\n//CHECKSTYLE:ON: checkstyle' $(find src/main/generated/io -type f)
GENERATED_WITH=$(grep -oP '\/\/ Protobuf Java Version: \K.*' "$TARGET_DIR/${PACKAGE//\.//}"/Metrics.java)
if [[ $GENERATED_WITH != "$PROTOBUF_VERSION" ]]; then
echo "Generated protobuf sources version $GENERATED_WITH does not match provided version $PROTOBUF_VERSION"
echo "Please use https://mise.jdx.dev/ - this will use the version specified in mise.toml"
echo "Generated protobuf sources are not up-to-date. Please run 'mise up && mise run test' and commit the changes."
exit 1
fi
git checkout -- ../mise.lock # see https://github.com/jdx/mise/discussions/4782
STATUS=$(git status --porcelain)
if [[ ${REQUIRE_PROTO_UP_TO_DATE:-false} == "true" && -n "$STATUS" ]]; then
echo "Please use https://mise.jdx.dev/ - this will use the version specified in mise.toml"
echo "Generated protobuf sources are not up-to-date. Please run 'mise run test' and commit the changes."
echo "Local changes:"
echo "$STATUS"
exit 1
fi