-
Notifications
You must be signed in to change notification settings - Fork 202
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
chore(proto): Add a buf.gen.rs.yaml and corresponding script to create Rust types for Wasm Stargate messages #1579
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fca9397
chore(proto): Add a buf.gen.rs.yaml and corresponding script to creat…
Unique-Divine 7ec82d8
changelog
Unique-Divine fdde88f
uncomment degit so that it pulls cosmos-sdk
Unique-Divine 9758e50
rm zombie comments
Unique-Divine 959d8d2
ci: implement skip-* pattern for coverage workflow liek unit and inte…
Unique-Divine a05c818
ci: fix typo
Unique-Divine b2f72c6
Merge branch 'master' into realu/bash-for-rs
Unique-Divine 2bc4381
Merge branch 'master' into realu/bash-for-rs
Unique-Divine a916270
Merge branch 'master' into realu/bash-for-rs
Unique-Divine f0f5199
ci: remove merge conflict code
Unique-Divine 1b6db9a
Merge branch 'master' into realu/bash-for-rs
Unique-Divine 74bd976
Merge branch 'master' into realu/bash-for-rs
Unique-Divine e384910
Merge branch 'master' into realu/bash-for-rs
Unique-Divine e906ca7
Merge branch 'master' into realu/bash-for-rs
Unique-Divine d8acb4b
Merge branch 'master' into realu/bash-for-rs
k-yang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,4 @@ mockdata/ | |
docs | ||
dist | ||
coverage.txt | ||
temp | ||
temp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
#!/usr/bin/env bash | ||
|
||
# buf.gen.rs.sh: Generates Rust protobuf types based on NibiruChain/nibiru/proto. | ||
|
||
set -eo pipefail | ||
|
||
move_to_dir_with_protos() { | ||
# Get the name of the current directory | ||
local start_path=$(pwd) | ||
local start_dir_name=$(basename $start_path) | ||
|
||
# Check if 'start_dir_name' is "nibiru" (the repo). | ||
# Or if the immediate parent is "nibiru", move to it. | ||
if [ "$start_dir_name" != "nibiru" ]; then | ||
if [ -d "../nibiru" ]; then | ||
cd ../nibiru | ||
else | ||
echo "Not in 'nibiru' directory or its immediate child. Exiting." | ||
return 1 | ||
fi | ||
fi | ||
|
||
# Check if nibiru/go.mod exists | ||
if [ ! -f "go.mod" ]; then | ||
echo "start_path: $start_path" | ||
echo "go.mod not found in 'nibiru' directory. Exiting." | ||
return 1 | ||
fi | ||
|
||
go mod tidy | ||
|
||
# Check if the nibiru/proto directory exists | ||
if [ ! -d "proto" ]; then | ||
echo "start_path: $start_path" | ||
echo "'proto' directory not found in 'nibiru'. Exiting." | ||
return 1 | ||
fi | ||
|
||
NIBIRU_REPO_PATH=$(pwd) | ||
echo "NIBIRU_REPO_PATH: $NIBIRU_REPO_PATH" | ||
} | ||
|
||
init_globals() { | ||
proto_dir="proto" # nibiru/proto | ||
out_dir="dist" # nibiru/dist | ||
|
||
cosmos_sdk_gh_path=$(go list -f '{{ .Dir }}' -m github.com/cosmos/cosmos-sdk) | ||
# cosmos_sdk_gh_path is parsed from the go.mod and will be a string like: | ||
# $HOME/go/pkg/mod/github.com/cosmos/[email protected] | ||
|
||
nibiru_cosmos_sdk_version=$(echo $cosmos_sdk_gh_path | awk -F'@' '{print $2}') | ||
# Example: v0.47.4 | ||
|
||
# OUT_PATH: Path to the generated protobuf types. | ||
OUT_PATH="${NIBIRU_REPO_PATH:-.}/$out_dir" | ||
echo "OUT_PATH: $OUT_PATH" | ||
} | ||
|
||
|
||
ensure_nibiru_cosmos_sdk_version() { | ||
# Ensure nibiru_cosmos_sdk_version has a value before proceeding | ||
if [ -z "$nibiru_cosmos_sdk_version" ]; then | ||
echo "nibiru_cosmos_sdk_version is empty. Exiting." | ||
return 1 | ||
fi | ||
} | ||
|
||
go_get_cosmos_protos() { | ||
echo "Grabbing cosmos-sdk proto file locations from disk" | ||
if ! grep "github.com/gogo/protobuf => github.com/regen-network/protobuf" go.mod &>/dev/null; then | ||
echo -e "\tPlease run this command from somewhere inside the cosmos-sdk folder." | ||
return 1 | ||
fi | ||
|
||
ensure_nibiru_cosmos_sdk_version | ||
|
||
# get protos for: cosmos-sdk, cosmos-proto | ||
go get github.com/cosmos/cosmos-sdk@$nibiru_cosmos_sdk_version | ||
go get github.com/cosmos/cosmos-proto | ||
} | ||
|
||
buf_gen() { | ||
# Called by proto_gen | ||
local proto_dir="$1" | ||
|
||
if [ ! buf ]; then | ||
echo "Please install buf to generate protos." | ||
exit 1 | ||
fi | ||
|
||
local buf_dir="$NIBIRU_REPO_PATH/proto" | ||
|
||
buf generate $proto_dir \ | ||
--template $buf_dir/buf.gen.rs.yaml \ | ||
-o $OUT_PATH \ | ||
--config $proto_dir/buf.yaml | ||
} | ||
|
||
proto_clone_sdk() { | ||
move_to_dir_with_protos | ||
local start_dir=$(pwd) # nibiru | ||
cd .. | ||
|
||
# degit copies a GitHub repo without downloading the entire git history, | ||
# which is much faster than a full git clone. | ||
npx degit "cosmos/cosmos-sdk#$nibiru_cosmos_sdk_version" cosmos-sdk --force | ||
DIR_COSMOS_SDK="$(pwd)/cosmos-sdk" | ||
cd $start_dir | ||
} | ||
|
||
proto_gen() { | ||
|
||
go_get_cosmos_protos | ||
proto_clone_sdk | ||
|
||
printf "\nProto Directories: \n" | ||
cd $NIBIRU_REPO_PATH/.. | ||
proto_dirs=() | ||
proto_dirs+=("$DIR_COSMOS_SDK/proto") | ||
proto_dirs+=("nibiru/proto") | ||
echo "$proto_dirs" | ||
|
||
echo "Generating protobuf types for each proto_dir" | ||
for proto_dir in "${proto_dirs[@]}"; do | ||
string=$proto_dir | ||
# Remove prefix | ||
prefix=$HOME/go/pkg/mod/github.com/ | ||
prefix_removed_string=${string/#$prefix/} | ||
# Remove prefix | ||
# prefix=$(dirname $(pwd)) | ||
prefix=$(pwd)/ | ||
prefix_removed_string=${prefix_removed_string/#$prefix/} | ||
echo "------------ generating $prefix_removed_string ------------" | ||
mkdir -p $OUT_PATH/${proto_dir} | ||
|
||
buf_gen $proto_dir | ||
done | ||
|
||
echo "Completed - proto_gen() to path: $OUT_PATH" | ||
} | ||
|
||
clean() { | ||
# clean: the Rust buf generator we're using leaves some garbage. | ||
# This function clears that way after a successful build. | ||
|
||
# Guarantee that OUT_PATH is defined | ||
if [ -z "$OUT_PATH" ]; then | ||
echo "skipped clean() since NIBIRU_REPO_PATH is not defined." | ||
return 0 | ||
fi | ||
|
||
# Guarantee that OUT_PATH is defined again just to be extra safe. | ||
OUT_PATH="${OUT_PATH:-.}" | ||
rm -rf $OUT_PATH/home | ||
rm -rf $OUT_PATH/nibiru | ||
} | ||
|
||
# ------------------------------------------------ | ||
# main : Start of script execution | ||
# ------------------------------------------------ | ||
|
||
main () { | ||
move_to_dir_with_protos | ||
init_globals | ||
proto_gen | ||
clean | ||
} | ||
|
||
if main; then | ||
echo "🔥 Generated Rust proto types successfully. " | ||
else | ||
echo "❌ Generation failed." | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# buf.gen.rs.yaml | ||
# | ||
# Run with: | ||
# ```bash | ||
# bash proto/buf.gen.rs.sh | ||
# ``` | ||
# | ||
version: v1 | ||
managed: | ||
enabled: true | ||
plugins: | ||
# protoc-gen-prost is the main code generation plugin | ||
# https://github.com/neoeinstein/protoc-gen-prost/blob/main/protoc-gen-prost/README.md | ||
- plugin: buf.build/community/neoeinstein-prost:v0.2.3 | ||
out: proto-rs | ||
opt: | ||
- bytes=. | ||
|
||
# TODO(Unique-Divine): investigate whether JSON serialization trait | ||
# implementations are needed for Rust protobufs used on Wasm::StargateMsg. | ||
# protoc-gen-prost-serde: Canonical JSON serialization of protobuf types | ||
# https://github.com/neoeinstein/protoc-gen-prost/blob/main/protoc-gen-prost-serde/README.md | ||
# - plugin: buf.build/community/neoeinstein-prost-serde:v0.2.3 | ||
# out: proto-rs |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend reading
main()
and working backwards since bash is a bit hard to follow