Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize protoc-swagger-gen.sh #747

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 51 additions & 55 deletions scripts/protoc-swagger-gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,78 @@ set -eo pipefail

go mod tidy

mkdir -p tmp_deps

#copy some deps to use their proto files to generate swagger
declare -a deps=("github.com/cosmos/cosmos-sdk"
"github.com/CosmWasm/wasmd"
"github.com/cosmos/admin-module/v2"
"github.com/cosmos/interchain-security/v5"
"github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8"
"github.com/skip-mev/feemarket"
"github.com/skip-mev/slinky"
"github.com/skip-mev/block-sdk/v2")

for dep in "${deps[@]}"
do
path=$(go list -f '{{ .Dir }}' -m $dep); \
cp -r $path tmp_deps; \
# Create temporary directories for dependencies and swagger files
mkdir -p tmp_deps tmp-swagger-gen/_all

# Copy some dependencies to use their proto files to generate swagger
declare -a deps=(
"github.com/cosmos/cosmos-sdk"
"github.com/CosmWasm/wasmd"
"github.com/cosmos/admin-module/v2"
"github.com/cosmos/interchain-security/v5"
"github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8"
"github.com/skip-mev/feemarket"
"github.com/skip-mev/slinky"
"github.com/skip-mev/block-sdk/v2"
)

for dep in "${deps[@]}"; do
path=$(go list -f '{{ .Dir }}' -m "$dep")
cp -r "$path" tmp_deps
done

proto_dirs=$(find ./proto ./tmp_deps -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)
for dir in $proto_dirs; do
# Find proto directories and generate swagger files
proto_dirs=$(find ./proto ./tmp_deps -name '*.proto' -print0 | xargs -0 -n1 dirname | sort -u)

# generate swagger files (filter query files)
for dir in $proto_dirs; do
query_file=$(find "${dir}" -maxdepth 1 \( -name 'query.proto' -o -name 'service.proto' \))
if [[ ! -z "$query_file" ]]; then
buf generate --template proto/buf.gen.swagger.yaml $query_file
if [[ -n "$query_file" ]]; then
buf generate --template proto/buf.gen.swagger.yaml "$query_file"
fi
done

# Fix circular definition in cosmos b just removing them
# Fix circular definitions in swagger files
jq 'del(.definitions["cosmos.tx.v1beta1.ModeInfo.Multi"].properties.mode_infos.items["$ref"])' ./tmp-swagger-gen/cosmos/tx/v1beta1/service.swagger.json > ./tmp-swagger-gen/cosmos/tx/v1beta1/fixed_service.swagger.json
jq 'del(.definitions["cosmos.autocli.v1.ServiceCommandDescriptor"].properties.sub_commands)' ./tmp-swagger-gen/cosmos/autocli/v1/query.swagger.json > ./tmp-swagger-gen/cosmos/autocli/v1/fixed_query.swagger.json

rm -rf ./tmp-swagger-gen/cosmos/tx/v1beta1/service.swagger.json
rm -rf ./tmp-swagger-gen/cosmos/autocli/v1/query.swagger.json

# remove unnecessary modules and their proto files
rm -rf tmp-swagger-gen/cosmos/staking
rm -rf tmp-swagger-gen/cosmos/distribution
rm -rf tmp-swagger-gen/cosmos/gov
rm -rf tmp-swagger-gen/cosmos/mint
rm -rf tmp-swagger-gen/cosmos/group
rm -rf tmp-swagger-gen/interchain_security/ccv/provider
# Remove unnecessary modules and their proto files
declare -a unnecessary_modules=(
"cosmos/staking"
"cosmos/distribution"
"cosmos/gov"
"cosmos/mint"
"cosmos/group"
"interchain_security/ccv/provider"
)

for module in "${unnecessary_modules[@]}"; do
rm -rf "tmp-swagger-gen/$module"
done

# Convert all *.swagger.json files into a single folder _all
files=$(find ./tmp-swagger-gen -name '*.swagger.json' -print0 | xargs -0)
mkdir -p ./tmp-swagger-gen/_all
counter=0
for f in $files; do
find ./tmp-swagger-gen -name '*.swagger.json' -print0 | while IFS= read -r -d '' f; do
echo "[+] $f"

# check gaia first before cosmos
if [[ "$f" =~ "router" ]]; then
cp $f ./tmp-swagger-gen/_all/pfm-$counter.json
elif [[ "$f" =~ "cosmwasm" ]]; then
cp $f ./tmp-swagger-gen/_all/cosmwasm-$counter.json
elif [[ "$f" =~ "osmosis" ]]; then
cp $f ./tmp-swagger-gen/_all/osmosis-$counter.json
elif [[ "$f" =~ "juno" ]]; then
cp $f ./tmp-swagger-gen/_all/juno-$counter.json
elif [[ "$f" =~ "cosmos" ]]; then
cp $f ./tmp-swagger-gen/_all/cosmos-$counter.json
else
cp $f ./tmp-swagger-gen/_all/other-$counter.json
fi

counter=$(expr $counter + 1)
case "$f" in
*router*) cp "$f" "./tmp-swagger-gen/_all/pfm-$counter.json" ;;
*cosmwasm*) cp "$f" "./tmp-swagger-gen/_all/cosmwasm-$counter.json" ;;
*osmosis*) cp "$f" "./tmp-swagger-gen/_all/osmosis-$counter.json" ;;
*juno*) cp "$f" "./tmp-swagger-gen/_all/juno-$counter.json" ;;
*cosmos*) cp "$f" "./tmp-swagger-gen/_all/cosmos-$counter.json" ;;
*) cp "$f" "./tmp-swagger-gen/_all/other-$counter.json" ;;
esac
((counter++))
done

# merges all the above into FINAL.json
# Merge all the above into FINAL.json
python3 ./scripts/swagger_merger.py

# Makes a swagger temp file with reference pointers
# Combine and extend swagger references
swagger-combine ./tmp-swagger-gen/FINAL.json -o ./tmp-swagger-gen/tmp_swagger.yaml -f yaml --continueOnConflictingPaths true --includeDefinitions true

# extends out the *ref instances to their full value
swagger-merger --input ./tmp-swagger-gen/tmp_swagger.yaml -o ./docs/static/swagger.yaml

rm -rf tmp-swagger-gen
rm -rf tmp_deps
# Clean up temporary files
rm -rf tmp-swagger-gen tmp_deps