-
Notifications
You must be signed in to change notification settings - Fork 323
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
Consolidate proto-compiler logic into main.rs #317
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a2c9b70
Consolidate proto-compiler logic into main.rs
romac 22d2e4b
Remove dependency on tempdir
romac 0e08462
Add README
romac eeac77a
Use path API instead of string formatting to compose paths
romac 2709789
Revert "Remove dependency on tempdir"
romac 6026582
Update READMEc
romac f9059c1
Update proto-compiler/README.md
romac 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 |
---|---|---|
@@ -1,18 +1,12 @@ | ||
[package] | ||
name = "ibc-proto-compiler" | ||
name = "ibc-proto-compiler" | ||
version = "0.1.0" | ||
authors = ["Greg Szabo <greg@philosobear.com>"] | ||
edition = "2018" | ||
publish = false | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
walkdir = { version = "2.3" } | ||
|
||
[build-dependencies] | ||
prost-build = { version = "0.6" } | ||
walkdir = { version = "2.3" } | ||
git2 = { version = "0.13" } | ||
|
||
[workspace] | ||
git2 = "0.13" | ||
prost-build = "0.6" | ||
tempdir = "0.3.7" | ||
walkdir = "2.3" |
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,23 @@ | ||
# ibc-proto-compiler | ||
|
||
The `ibc-proto-compiler` is a simple command-line tool to automate the compilation of [Protocol Buffers](https://developers.google.com/protocol-buffers) message definitions from the [Cosmos SDK](https://github.com/cosmos/cosmos-sdk) to Rust source code with [Prost](https://lib.rs/crates/prost), for use in the [`ibc-proto` crate](https://lib.rs/crates/ibc-proto) in the [`ibc-rs` project](https://github.com/informalsystems/ibc-rs/). | ||
|
||
## Usage | ||
|
||
From within the `proto-compiler` directory, run the following command to clone the Cosmos SDK repository, generate the Rust sources from the Protobuf definitions, and copy them to the `ibc-proto` crate within the `ibc-rs` project: | ||
|
||
```bash | ||
$ cargo run | ||
``` | ||
|
||
The path to the Cosmos SDK checkout can be specified with the `SDK_DIR` environment variable: (default: `target/cosmos-sdk/`) | ||
|
||
```bash | ||
$ SDK_DIR=$HOME/Code/cosmos-sdk cargo run | ||
``` | ||
|
||
The directory to which the Rust sources should be generated in before being copied to the appropriate location can be specified with the `OUT_DIR` environment variable: (default: temporary directory via the `tempdir` crate) | ||
|
||
```bash | ||
$ OUT_DIR=/tmp/rust cargo run | ||
``` |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,36 +1,129 @@ | ||
use std::env::var; | ||
use std::fs::remove_dir_all; | ||
use std::fs::{copy, create_dir_all}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use git2::Repository; | ||
use tempdir::TempDir; | ||
use walkdir::WalkDir; | ||
|
||
pub(crate) fn main() { | ||
let ibc_proto_path = "../proto/src/prost"; | ||
fn main() { | ||
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
let target_dir = root.join("../proto/src/prost"); | ||
let out_dir = var("OUT_DIR") | ||
.map(PathBuf::from) | ||
.or_else(|_| TempDir::new("ibc_proto_out").map(|d| d.into_path())) | ||
.unwrap(); | ||
|
||
let sdk_dir = clone_cosmos_sdk(); | ||
compile_protos(&sdk_dir, &out_dir); | ||
copy_generated_files(&out_dir, &target_dir); | ||
} | ||
|
||
fn clone_cosmos_sdk() -> PathBuf { | ||
let sdk_dir = var("SDK_DIR").unwrap_or_else(|_| "target/cosmos-sdk".to_string()); | ||
|
||
if Path::new(&sdk_dir).exists() { | ||
println!("[info ] Found Cosmos SDK source at '{}'", sdk_dir); | ||
} else { | ||
println!("[info ] Cloning cosmos/cosmos-sdk repository..."); | ||
|
||
let url = "https://github.com/cosmos/cosmos-sdk"; | ||
Repository::clone(url, &sdk_dir).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll have a more robust solution to this in the tendermint-proto crate. Later, for a next update. |
||
|
||
println!("[info ] => Cloned at '{}'", sdk_dir); | ||
} | ||
|
||
PathBuf::from(sdk_dir) | ||
} | ||
|
||
fn compile_protos(sdk_dir: impl AsRef<Path>, out_dir: impl AsRef<Path>) { | ||
println!( | ||
"[info ] Compiling .proto files to Rust into '{}'...", | ||
out_dir.as_ref().display() | ||
); | ||
|
||
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
let sdk_dir = sdk_dir.as_ref().to_owned(); | ||
|
||
// Paths | ||
let proto_paths = [ | ||
root.join("../proto/definitions/mock"), | ||
sdk_dir.join("proto/ibc"), | ||
sdk_dir.join("proto/cosmos/tx"), | ||
sdk_dir.join("proto/cosmos/base"), | ||
]; | ||
|
||
let proto_includes_paths = [ | ||
root.join("../proto"), | ||
sdk_dir.join("proto"), | ||
sdk_dir.join("third_party/proto"), | ||
]; | ||
|
||
// List available proto files | ||
let mut protos: Vec<PathBuf> = vec![]; | ||
for proto_path in &proto_paths { | ||
protos.append( | ||
&mut WalkDir::new(proto_path) | ||
.into_iter() | ||
.filter_map(|e| e.ok()) | ||
.filter(|e| { | ||
e.file_type().is_file() | ||
&& e.path().extension().is_some() | ||
&& e.path().extension().unwrap() == "proto" | ||
}) | ||
.map(|e| e.into_path()) | ||
.collect(), | ||
); | ||
} | ||
|
||
// List available paths for dependencies | ||
let includes: Vec<PathBuf> = proto_includes_paths.iter().map(PathBuf::from).collect(); | ||
|
||
// Compile all proto files | ||
let mut config = prost_build::Config::default(); | ||
config.out_dir(out_dir.as_ref()); | ||
config.extern_path(".tendermint", "::tendermint_proto"); | ||
config.compile_protos(&protos, &includes).unwrap(); | ||
|
||
println!("[info ] => Done!"); | ||
} | ||
|
||
fn copy_generated_files(from_dir: impl AsRef<Path>, to_dir: impl AsRef<Path>) { | ||
println!( | ||
"[info ] Copying generated Rust sources into '{}'...", | ||
to_dir.as_ref().display() | ||
); | ||
|
||
// Remove old compiled files | ||
remove_dir_all(ibc_proto_path).unwrap_or_default(); | ||
create_dir_all(ibc_proto_path).unwrap(); | ||
remove_dir_all(&to_dir).unwrap_or_default(); | ||
create_dir_all(&to_dir).unwrap(); | ||
|
||
// Copy new compiled files (prost does not use folder structures) | ||
let err: Vec<std::io::Error> = WalkDir::new(env!("OUT_DIR")) | ||
let errors = WalkDir::new(from_dir) | ||
.into_iter() | ||
.filter_map(|e| e.ok()) | ||
.filter(|e| e.file_type().is_file()) | ||
.map(|e| { | ||
copy( | ||
e.path(), | ||
std::path::Path::new(&format!( | ||
format!( | ||
"{}/{}", | ||
ibc_proto_path, | ||
to_dir.as_ref().display(), | ||
&e.file_name().to_os_string().to_str().unwrap() | ||
)), | ||
), | ||
) | ||
}) | ||
.filter_map(|e| e.err()) | ||
.collect(); | ||
.collect::<Vec<_>>(); | ||
|
||
if !err.is_empty() { | ||
for e in err { | ||
dbg!(e); | ||
if !errors.is_empty() { | ||
for e in errors { | ||
println!("[error] Error while copying compiled file: {}", e); | ||
} | ||
panic!("error while copying compiled files") | ||
|
||
panic!("[error] Aborted."); | ||
} | ||
|
||
println!("[info ] => Done!"); | ||
} |
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.
nice!