Skip to content

Commit

Permalink
feat(ts): implement grpc-js client services (#184)
Browse files Browse the repository at this point in the history
Issue: #184
  • Loading branch information
ygrishajev committed Apr 30, 2024
1 parent b7813c9 commit 2a3fc30
Show file tree
Hide file tree
Showing 24 changed files with 4,779 additions and 104 deletions.
41 changes: 25 additions & 16 deletions script/protocgen-legacy.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

source ./script/ts-proto.sh

set -eo pipefail

PATH=$(pwd)/.cache/bin/legacy:$PATH
Expand Down Expand Up @@ -31,15 +33,17 @@ for dir in $proto_dirs; do
--grpc-gateway_out=logtostderr=true:. \
$(find "${dir}" -maxdepth 1 -name '*.proto')

.cache/bin/protoc \
-I "proto/node" \
run_ts_proto \
-I ".cache/include/google/protobuf" \
-I "vendor/github.com/cosmos/cosmos-sdk/proto" \
-I "vendor/github.com/cosmos/cosmos-sdk/third_party/proto" \
--plugin="${AKASH_TS_NODE_BIN}/protoc-gen-ts_proto" \
--ts_proto_out="${AKASH_TS_ROOT}/src/generated" \
--ts_proto_opt=esModuleInterop=true,forceLong=long,outputTypeRegistry=true,useExactTypes=false,outputIndex=true \
$(find "${dir}" -maxdepth 1 -name '*.proto')
--out "${AKASH_TS_ROOT}/src/generated" \
--opt "outputIndex=true" \
--dir "${dir}"

run_ts_proto \
-I ".cache/include/google/protobuf" \
--out "$TMP_GRPC_JS_SERVICES_DIR" \
--opt "outputServices=grpc-js" \
--dir "${dir}"
done

proto_dirs=$(find ./proto/provider -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)
Expand All @@ -64,18 +68,23 @@ for dir in $proto_dirs; do
--grpc-gateway_out=logtostderr=true:. \
$(find "${dir}" -maxdepth 1 -name '*.proto')

.cache/bin/protoc \
run_ts_proto \
-I "proto/provider" \
-I "proto/node" \
-I ".cache/include" \
-I "vendor/github.com/cosmos/cosmos-sdk/proto" \
-I "vendor/github.com/cosmos/cosmos-sdk/third_party/proto" \
--plugin="${AKASH_TS_NODE_BIN}/protoc-gen-ts_proto" \
--ts_proto_out="${AKASH_TS_ROOT}/src/generated" \
--ts_proto_opt=esModuleInterop=true,forceLong=long,outputTypeRegistry=true,useExactTypes=false,outputIndex=true \
$(find "${dir}" -maxdepth 1 -name '*.proto')
--out "${AKASH_TS_ROOT}/src/generated" \
--opt "outputIndex=true" \
--dir "${dir}"

run_ts_proto \
-I "proto/provider" \
-I ".cache/include" \
--out "$TMP_GRPC_JS_SERVICES_DIR" \
--opt "outputServices=grpc-js" \
--dir "${dir}"
done

add_grpc_js_services

# move proto files to the right places
cp -rv github.com/akash-network/akash-api/* ./

Expand Down
87 changes: 87 additions & 0 deletions script/ts-proto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env bash

TMP_GRPC_JS_SERVICES_DIR="${AKASH_DEVCACHE_BASE}/ts/generated-grpc-js"

function run_ts_proto() {
local ts_proto_opt
local inputs=()
local dir
local out

while (( "$#" )); do
case "$1" in
--opt)
ts_proto_opt=$2
shift 2
;;
-I)
inputs+=("$2")
shift 2
;;
--dir)
dir=$2
shift 2
;;
--out)
out=$2
shift 2
;;
--)
shift
break
;;
*)
echo "Error: Invalid argument - $1"
return 1
;;
esac
done

local I_opts=""
for input in "${inputs[@]}"; do
I_opts+="-I ${input} "
done

if [ ! -d "$out" ]; then
mkdir -p "$out"
fi

.cache/bin/protoc \
${I_opts} \
-I "proto/node" \
-I "vendor/github.com/cosmos/cosmos-sdk/proto" \
-I "vendor/github.com/cosmos/cosmos-sdk/third_party/proto" \
--plugin="${AKASH_TS_NODE_BIN}/protoc-gen-ts_proto" \
--ts_proto_out="${out}" \
--ts_proto_opt="esModuleInterop=true,forceLong=long,outputTypeRegistry=true,useExactTypes=false,${ts_proto_opt}" \
$(find "${dir}" -maxdepth 1 -name '*.proto')
}

function add_grpc_js_services() {
local ts_grpc_js_services
local file
local dest_path
local index_file_path
local index_file_name
local index_file_path
local export_statement

ts_grpc_js_services=$(find "$TMP_GRPC_JS_SERVICES_DIR" -name 'service.ts')

for file in $ts_grpc_js_services; do
dest_path=$(dirname "${file/$TMP_GRPC_JS_SERVICES_DIR/$AKASH_TS_ROOT\/src\/generated}")
dest_file="${dest_path}/service.grpc-js.ts"

mv "$file" "$dest_file"

path_from_gen_dir=${dest_file#"${AKASH_TS_ROOT}/src/generated/"}
index_file_name_base=${path_from_gen_dir%/service.grpc-js.ts}
index_file_name="index.${index_file_name_base//\//.}.grpc-js.ts"
index_file_path="${AKASH_TS_ROOT}/src/generated/$index_file_name"
export_statement="export * from \"./${path_from_gen_dir%.ts}\";"

echo "$export_statement" > "$index_file_path"
done

rm -rf "$TMP_GRPC_JS_SERVICES_DIR"
}
Loading

0 comments on commit 2a3fc30

Please sign in to comment.