Skip to content

Commit

Permalink
Update diagram script and workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
havan committed Dec 7, 2023
1 parent b5c9175 commit 03dcb12
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 42 deletions.
36 changes: 0 additions & 36 deletions .github/workflows/buf.yaml

This file was deleted.

70 changes: 70 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: CI

on:
push:
# Only run when there are changes under proto dir
paths:
- "proto/**"
- ".github/workflows/**"
pull_request:
# Only run when there are changes under proto dir
paths:
- "proto/**"
- ".github/workflows/**"

jobs:
# Run buf's lint to check for errros
buf-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: bufbuild/buf-setup-action@v1
- uses: bufbuild/buf-lint-action@v1
with:
input: "proto"
# Push the draft branch to buf.build
bsr-push-draft:
runs-on: ubuntu-latest
needs: buf-lint
if: github.ref == 'refs/heads/draft'
environment: draft
steps:
- uses: actions/checkout@v4
- uses: bufbuild/buf-setup-action@v1
- uses: bufbuild/buf-push-action@v1
with:
input: "proto"
draft: true
buf_token: ${{ secrets.BUF_BSR_TOKEN }}

# Generate and upload protodot diagrams
diagrams:
runs-on: ubuntu-latest
needs: buf-lint
if: github.ref == 'refs/heads/draft'
steps:
# Generate diagrams
- name: Generate Diagrams
run: |
wget https://github.com/seamia/protodot/raw/master/binaries/protodot-linux-amd64
chmod +x protodot-linux-amd64
mkdir -v -p gen/bin
mv protodot-linux-amd64 gen/bin/protodot
export PATH=${PWD}/gen/bin:${PATH}
bash scripts/generate_protodot.sh
find gen/diagrams -type f -name "*.dot" -exec rm -f {} +
tree gen/diagrams
# Deploy diagrams
- name: Cloud Authentication
id: auth
uses: "google-github-actions/auth@v1"
with:
credentials_json: "${{ secrets.GOOGLE_CREDENTIALS }}"

- name: Setup Cloud SDK
uses: google-github-actions/setup-gcloud@v1

- name: Upload Diagrams
run: |
gsutil -m rsync -R -d gen/diagrams gs://docs-cmp-files/diagrams
81 changes: 75 additions & 6 deletions scripts/generate_protodot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,82 @@

PROTODOT="protodot"
GENERATED_DIR="${1:-gen}"
TCM_DIR="${2:-proto/cmp}"
PROTO_DIR="${2:-proto/cmp}"
PROTODOT_DIR="${3:-diagrams}"
VERSION="v1alpha1"

for pkg in `ls -1 ${TCM_DIR}`; do
for protofile in `ls -1 ${TCM_DIR}/${pkg}/${VERSION}`; do
mkdir -p ${GENERATED_DIR}/${PROTODOT_DIR}/${pkg}/${VERSION}
${PROTODOT} -generated ${GENERATED_DIR}/${PROTODOT_DIR}/${pkg}/${VERSION} -src ${TCM_DIR}/${pkg}/${VERSION}/${protofile} -output ${protofile}
done
declare -a BIG_ENUMS=(
"country.proto"
"currency.proto"
"language.proto"
)

declare -a TRUNCATE=()

# Usage: shorten_protobuf "path/to/country.proto"
shorten_protobuf() {
local filename="$1"

# Check if the file exists
if [[ ! -f "$filename" ]]; then
echo "File not found: $filename"
return 1
fi

# Create a backup of the original file
cp -v "$filename" "${filename}.bak"

# Process the file
awk '
BEGIN { print_flag=1; enum_count=0; }
/enum [^ ]+ {/ { print_flag=0; print; next; }
/}/ { if (print_flag == 0) { print " X_TRUNCATED_X = 999;"; print; print_flag=1; } else { print; } next; }
{ if (print_flag) { print; } else { if (enum_count < 7) { print; enum_count++; } } }
' "$filename" > "${filename}.tmp"

# Replace original file with modified file
mv -v "${filename}.tmp" "$filename"
}

# Usage: revert_protobuf "path/to/country.proto"
revert_protobuf() {
local filename="$1"

# Check if the backup file exists
local backup_filename="${filename}.bak"
if [[ ! -f "$backup_filename" ]]; then
echo "Backup file not found: $backup_filename"
return 1
fi

# Replace the modified file with the backup
mv -v "$backup_filename" "$filename"
}

# Collect big enum files
for protofile in `find ${PROTO_DIR} -type f -name '*.proto'`; do
if [[ ${BIG_ENUMS[@]} =~ $(basename ${protofile}) ]]; then
# Found a big enum, add to TRUNCATE
TRUNCATE+=(${protofile})
# Then truncate the file
shorten_protobuf ${protofile}
fi
done

# Generate diagrams
for protofile in `find ${PROTO_DIR} -type f -name '*.proto'`; do
# Set proto file dir
protofile_dir=${GENERATED_DIR}/${PROTODOT_DIR}/$(dirname ${protofile})

# Create proto file dir if it doesn't exist
mkdir -p ${protofile_dir}

# Create the protodot diagram
${PROTODOT} -generated ${protofile_dir} -src ${protofile} -output $(basename ${protofile})
done

# When generating finished, revert the truncated big enums
for truncated_file in "${TRUNCATE[@]}"; do
# Revert the truncated files back
revert_protobuf ${truncated_file}
done

0 comments on commit 03dcb12

Please sign in to comment.