-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: proto-build better modularity (#36)
- Loading branch information
Showing
27 changed files
with
906 additions
and
949 deletions.
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
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
#[cfg(feature = "abstract-any")] | ||
pub mod any; | ||
|
||
#[allow(clippy::all)] | ||
mod gen; | ||
|
||
pub use gen::*; | ||
|
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,49 @@ | ||
use crate::utils::patch_file::patch_file; | ||
use glob::glob; | ||
use std::path::{Path, PathBuf}; | ||
|
||
/// Fix clashing type names in prost-generated code. | ||
fn apply_cosmos_staking_patches(out_dir: &Path) -> crate::Result<()> { | ||
const REPLACEMENTS: &[(&str, &str)] = &[ | ||
("enum Validators", "enum Policy"), | ||
( | ||
"stake_authorization::Validators", | ||
"stake_authorization::Policy", | ||
), | ||
]; | ||
|
||
patch_file(&out_dir.join("cosmos.staking.v1beta1.rs"), REPLACEMENTS)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn apply_patches(out_dir: &Path) -> crate::Result<()> { | ||
println!("Applying patches..."); | ||
/// Regex substitutions to apply to the prost-generated output | ||
const REPLACEMENTS: &[(&str, &str)] = &[ | ||
// Feature-gate gRPC impls which use `tonic::transport` | ||
( | ||
"impl(.+)tonic::transport(.+)", | ||
"#[cfg(feature = \"grpc-transport\")]\n \ | ||
#[cfg_attr(docsrs, doc(cfg(feature = \"grpc-transport\")))]\n \ | ||
impl${1}tonic::transport${2}", | ||
), | ||
// Feature-gate the ProtoBuf descriptors | ||
( | ||
"pub const FILE_DESCRIPTOR_SET", | ||
"#[cfg(feature = \"proto-descriptor\")]\n \ | ||
#[cfg_attr(docsrs, doc(cfg(feature = \"proto-descriptor\")))]\n \ | ||
pub const FILE_DESCRIPTOR_SET", | ||
), | ||
]; | ||
|
||
let src_files_glob = out_dir.join("*.rs"); | ||
let src_files: Vec<PathBuf> = glob(src_files_glob.to_str().unwrap())?.flatten().collect(); | ||
for src in src_files { | ||
patch_file(&src, REPLACEMENTS)?; | ||
} | ||
|
||
apply_cosmos_staking_patches(out_dir)?; | ||
|
||
Ok(()) | ||
} |
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,17 @@ | ||
use glob::glob; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
const EXCLUDED_PROTO_PACKAGES: &[&str] = &["amino", "gogoproto", "google", "tendermint"]; | ||
|
||
pub fn cleanup(out_dir: &Path) { | ||
println!("Cleaning up..."); | ||
for &pkg in EXCLUDED_PROTO_PACKAGES { | ||
let excluded_files_glob = format!("{}/{pkg}*.rs", out_dir.display()); | ||
glob(excluded_files_glob.as_str()) | ||
.unwrap() | ||
.flatten() | ||
.try_for_each(fs::remove_file) | ||
.unwrap(); | ||
} | ||
} |
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,15 @@ | ||
use crate::consts::{ARCHWAY_DIR, COSMOS_SDK_DIR, IBC_DIR, WASMD_DIR}; | ||
use crate::utils::run::run_buf_export; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
pub fn export(submodules_dir: &Path, proto_dir: &Path) { | ||
if proto_dir.exists() { | ||
fs::remove_dir_all(proto_dir).unwrap(); | ||
} | ||
|
||
run_buf_export(submodules_dir, ARCHWAY_DIR, proto_dir).unwrap(); | ||
run_buf_export(submodules_dir, COSMOS_SDK_DIR, proto_dir).unwrap(); | ||
run_buf_export(submodules_dir, IBC_DIR, proto_dir).unwrap(); | ||
run_buf_export(submodules_dir, WASMD_DIR, proto_dir).unwrap(); | ||
} |
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 @@ | ||
use crate::utils::run::run_cmd; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
pub fn generate(buf_gen_path: &Path, proto_path: &Path, out_dir: &Path) -> crate::Result<String> { | ||
println!("Generating proto..."); | ||
|
||
if out_dir.exists() { | ||
fs::remove_dir_all(out_dir).unwrap(); | ||
} | ||
|
||
run_cmd( | ||
"buf", | ||
[ | ||
"generate", | ||
"--template", | ||
&buf_gen_path.display().to_string(), | ||
"--include-imports", | ||
"-o", | ||
&out_dir.display().to_string(), | ||
&proto_path.display().to_string(), | ||
], | ||
) | ||
} |
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,7 @@ | ||
pub mod apply_patches; | ||
pub mod cleanup; | ||
pub mod export; | ||
pub mod generate; | ||
pub mod output_version; | ||
pub mod rustfmt; | ||
pub mod update_submodules; |
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,12 @@ | ||
use crate::consts::{ARCHWAY_REV, COSMOS_SDK_REV, IBC_REV, WASMD_REV}; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
pub fn output_versions(out_dir: &Path) { | ||
println!("Writing versions..."); | ||
let out_dir = Path::new(out_dir); | ||
fs::write(out_dir.join("ARCHWAY_COMMIT"), ARCHWAY_REV).unwrap(); | ||
fs::write(out_dir.join("COSMOS_SDK_COMMIT"), COSMOS_SDK_REV).unwrap(); | ||
fs::write(out_dir.join("IBC_COMMIT"), IBC_REV).unwrap(); | ||
fs::write(out_dir.join("WASMD_COMMIT"), WASMD_REV).unwrap(); | ||
} |
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,22 @@ | ||
use crate::utils::run::run_cmd; | ||
use glob::glob; | ||
use std::path::{Path, PathBuf}; | ||
|
||
fn collect_files(dir: &Path, pattern: &str) -> crate::Result<Vec<PathBuf>> { | ||
// dir.join("**").join(pattern); | ||
let file_glob = format!("{}/**/{pattern}", dir.display()); | ||
let paths: Vec<PathBuf> = glob(file_glob.as_str())?.flatten().collect(); | ||
Ok(paths) | ||
} | ||
|
||
pub fn rustfmt(out_dir: &Path) -> crate::Result<String> { | ||
println!("Running rustfmt..."); | ||
let files = collect_files(out_dir, "*.rs")?.into_iter().map(Into::into); | ||
let args: Vec<std::ffi::OsString> = ["--edition", "2021"] | ||
.iter() | ||
.map(Into::into) | ||
.chain(files) | ||
.collect(); | ||
|
||
run_cmd("rustfmt", args) | ||
} |
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,48 @@ | ||
use crate::consts::{ | ||
ARCHWAY_DIR, ARCHWAY_REV, COSMOS_SDK_DIR, COSMOS_SDK_REV, IBC_DIR, IBC_REV, WASMD_DIR, | ||
WASMD_REV, | ||
}; | ||
use crate::utils::run::run_git; | ||
use std::path::Path; | ||
|
||
pub fn update_submodules(submodules_dir: &Path) { | ||
run_git(["submodule", "update", "--init"]).unwrap(); | ||
run_git(["submodule", "foreach", "git", "fetch"]).unwrap(); | ||
|
||
println!("Updating archway-network/archway submodule..."); | ||
let archway_dir = submodules_dir.join(ARCHWAY_DIR); | ||
run_git([ | ||
"-C", | ||
archway_dir.to_str().unwrap(), | ||
"reset", | ||
"--hard", | ||
ARCHWAY_REV, | ||
]) | ||
.unwrap(); | ||
|
||
println!("Updating cosmos/cosmos-sdk submodule..."); | ||
let sdk_dir = submodules_dir.join(COSMOS_SDK_DIR); | ||
run_git([ | ||
"-C", | ||
sdk_dir.to_str().unwrap(), | ||
"reset", | ||
"--hard", | ||
COSMOS_SDK_REV, | ||
]) | ||
.unwrap(); | ||
|
||
println!("Updating cosmos/ibc-go submodule..."); | ||
let ibc_dir = submodules_dir.join(IBC_DIR); | ||
run_git(["-C", ibc_dir.to_str().unwrap(), "reset", "--hard", IBC_REV]).unwrap(); | ||
|
||
println!("Updating wasmd submodule..."); | ||
let wasmd_dir = submodules_dir.join(WASMD_DIR); | ||
run_git([ | ||
"-C", | ||
wasmd_dir.to_str().unwrap(), | ||
"reset", | ||
"--hard", | ||
WASMD_REV, | ||
]) | ||
.unwrap(); | ||
} |
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,18 @@ | ||
/// The Archway commit or tag to be cloned and used to build the proto files | ||
pub const ARCHWAY_REV: &str = "v7.0.1"; | ||
pub const ARCHWAY_DIR: &str = "archway"; | ||
|
||
/// The Cosmos SDK commit or tag to be cloned and used to build the proto files | ||
pub const COSMOS_SDK_REV: &str = "v0.47.11"; | ||
pub const COSMOS_SDK_DIR: &str = "cosmos-sdk"; | ||
|
||
/// The Cosmos ibc-go commit or tag to be cloned and used to build the proto files | ||
pub const IBC_REV: &str = "v7.4.0"; | ||
pub const IBC_DIR: &str = "ibc-go"; | ||
|
||
/// The wasmd commit or tag to be cloned and used to build the proto files | ||
pub const WASMD_REV: &str = "v0.45.0"; | ||
pub const WASMD_DIR: &str = "wasmd"; | ||
|
||
pub const PROTO_DIR: &str = "proto"; | ||
pub const OUT_DIR: &str = "packages/proto/src/gen"; |
Oops, something went wrong.