Skip to content

Commit

Permalink
Modernize Regenerate Script
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen committed Nov 21, 2022
1 parent eb8ffda commit b5a617f
Showing 1 changed file with 86 additions and 24 deletions.
110 changes: 86 additions & 24 deletions bin/regenerate.sh
Expand Up @@ -5,11 +5,75 @@ BIN_DIR="$( dirname "$SCRIPT" )"
ROOT_DIR="$( dirname "$BIN_DIR" )"
LIB_DIR="$ROOT_DIR/lib"
PROTO_ROOT="$ROOT_DIR/priv/proto"
API_VERSION_FILE="$ROOT_DIR/API_VERSION"

set -eu

store_lib () {
URL="$1"
FILE="$2"
ROOT_DIR="$( dirname "$FILE" )"

gh_curl "$URL" \
--output "$FILE" \
--fail \
--silent
}

list_files () {
REPO="$1"
REPO_PATH="$2"

gh_curl "https://api.github.com/repos/$REPO/contents/$REPO_PATH" \
--fail \
--silent \
| jq \
'.[] | select(.name | endswith(".proto")) .name' \
--raw-output
}

download_files () {
REPO="$1"
REPO_PATH="$2"
LOCAL_PATH="$3"
VERSION="$4"
TARGET_DIR="$PROTO_ROOT/$LOCAL_PATH"

echo >&2 "Downloading $REPO#$VERSION/$REPO_PATH to $TARGET_DIR"

mkdir -p "$TARGET_DIR"

list_files "$REPO" "$REPO_PATH" | while read FILE; do
store_lib "https://raw.githubusercontent.com/$REPO/$VERSION/$REPO_PATH/$FILE" "$TARGET_DIR/$FILE"
done
}

gh_curl () {
if [ -z "$GITHUB_TOKEN" ]; then
curl \
--fail \
--silent \
"$@"
else
curl \
--fail \
--silent \
--header "Authorization: Bearer $GITHUB_TOKEN" \
"$@"
fi
}

update_api_version () {
gh_curl https://api.github.com/repos/zitadel/zitadel/releases/latest \
| jq \
'.tag_name' \
--raw-output \
> "$API_VERSION_FILE"
}

echo >&2 "Zitadel API Version"
API_VERSION="$( sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' "$ROOT_DIR/API_VERSION" )"
update_api_version
API_VERSION="$( sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' "$API_VERSION_FILE" )"
echo >&2 "$API_VERSION"

echo >&2 "Remove Old Source Code"
Expand All @@ -22,30 +86,28 @@ echo >&2 "Ensure Target Directories exist"
mkdir -p "$LIB_DIR"
mkdir -p "$PROTO_ROOT"

store_lib () {
DIR="$PROTO_ROOT/$1"
mkdir -p "$DIR"

(
cd "$DIR"
curl -O --fail --silent "$2"
)
}

echo >&2 "Download protofiles"
ZITADEL_FILES="action.proto admin.proto app.proto auth.proto auth_n_key.proto change.proto idp.proto instance.proto management.proto member.proto message.proto metadata.proto object.proto options.proto org.proto policy.proto project.proto settings.proto system.proto text.proto user.proto v1.proto"
for ZITADEL_FILE in $ZITADEL_FILES; do
store_lib zitadel "https://raw.githubusercontent.com/zitadel/zitadel/$API_VERSION/proto/zitadel/$ZITADEL_FILE"
done
OPENAPI_V2_FILES="annotations.proto openapiv2.proto"
for OPENAPI_V2_FILE in $OPENAPI_V2_FILES; do
store_lib "protoc-gen-openapiv2/options" "https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-openapiv2/options/$OPENAPI_V2_FILE"
done
GOOGLE_API_FILES="annotations.proto http.proto"
for GOOGLE_API_FILE in $GOOGLE_API_FILES; do
store_lib "google/api" "https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/$GOOGLE_API_FILE"
done
store_lib "validate" "https://raw.githubusercontent.com/envoyproxy/protoc-gen-validate/v0.6.2/validate/validate.proto"

download_files \
"zitadel/zitadel" \
"proto/zitadel" \
"zitadel" \
"$API_VERSION"
download_files \
"grpc-ecosystem/grpc-gateway" \
"protoc-gen-openapiv2/options" \
"protoc-gen-openapiv2/options" \
"master"
download_files \
"googleapis/googleapis" \
"google/api" \
"google/api" \
"master"
download_files \
"bufbuild/protoc-gen-validate" \
"validate" \
"validate" \
"v0.6.7"

if command -v protoc-gen-elixir > /dev/null; then
echo >&2 "Elixir Protoc Plugin already installed"
Expand Down

0 comments on commit b5a617f

Please sign in to comment.