-
Notifications
You must be signed in to change notification settings - Fork 828
Expand file tree
/
Copy pathgenerate-protobuf.sh
More file actions
executable file
·64 lines (50 loc) · 2.68 KB
/
generate-protobuf.sh
File metadata and controls
executable file
·64 lines (50 loc) · 2.68 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
#!/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/.*extends (io\.prometheus\.metrics\.expositionformats\.generated\.[^ ]*?)\.Metrics.*/\1/p' src/main/java/io/prometheus/metrics/expositionformats/generated/Metrics.java)
PACKAGE="io.prometheus.metrics.expositionformats.generated.com_google_protobuf_${PROTOBUF_VERSION_STRING}"
SUPERCLASS_FILE="src/main/java/io/prometheus/metrics/expositionformats/generated/Metrics.java"
if [[ $OLD_PACKAGE != "$PACKAGE" ]]; then
echo "Replacing package $OLD_PACKAGE with $PACKAGE in $SUPERCLASS_FILE"
sed -i "s/$OLD_PACKAGE/$PACKAGE/g" "$SUPERCLASS_FILE"
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_FILE="$TARGET_DIR/${PACKAGE//\.//}/Metrics.java"
sed -i 's/public final class Metrics/public class Metrics/' "$GENERATED_FILE"
sed -i 's/private Metrics() {}/protected Metrics() {}/' "$GENERATED_FILE"
GENERATED_WITH=$(grep -oP '\/\/ Protobuf Java Version: \K.*' "$TARGET_DIR/${PACKAGE//\.//}"/Metrics.java)
function help() {
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 generate' and commit the changes."
echo "NOTE:"
echo "1. You should only run 'mise run generate' in a PR from renovate"
echo "2. The PR should update both '<protobuf-java.version>' in pom.xml and protoc in mise.toml"
echo " - but at least <protobuf-java.version>. If not, wait until renovate updates the PR."
}
if [[ $GENERATED_WITH != "$PROTOBUF_VERSION" ]]; then
echo "Generated protobuf sources version $GENERATED_WITH does not match provided version $PROTOBUF_VERSION"
help
exit 1
fi
STATUS=$(git status --porcelain)
if [[ ${REQUIRE_PROTO_UP_TO_DATE:-false} == "true" && -n "$STATUS" ]]; then
help
echo "Local changes:"
echo "$STATUS"
exit 1
fi