Skip to content

Commit

Permalink
feat: proto-build better modularity (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
aelesbao authored Oct 30, 2024
2 parents 5238713 + d54470f commit f06f1bc
Show file tree
Hide file tree
Showing 27 changed files with 906 additions and 949 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# will have compiled files and executables
debug/
target/
artifacts/

# These are backup files generated by rustfmt
**/*.rs.bk
Expand All @@ -26,3 +25,7 @@ artifacts/

# Exported by proto-build
/proto

# Local settings
artifacts/
.envrc
1 change: 1 addition & 0 deletions packages/proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[cfg(feature = "abstract-any")]
pub mod any;

#[allow(clippy::all)]
mod gen;

pub use gen::*;
Expand Down
49 changes: 49 additions & 0 deletions proto-build/src/commands/apply_patches.rs
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(())
}
17 changes: 17 additions & 0 deletions proto-build/src/commands/cleanup.rs
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();
}
}
15 changes: 15 additions & 0 deletions proto-build/src/commands/export.rs
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();
}
24 changes: 24 additions & 0 deletions proto-build/src/commands/generate.rs
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(),
],
)
}
7 changes: 7 additions & 0 deletions proto-build/src/commands/mod.rs
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;
12 changes: 12 additions & 0 deletions proto-build/src/commands/output_version.rs
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();
}
22 changes: 22 additions & 0 deletions proto-build/src/commands/rustfmt.rs
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)
}
48 changes: 48 additions & 0 deletions proto-build/src/commands/update_submodules.rs
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();
}
18 changes: 18 additions & 0 deletions proto-build/src/consts.rs
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";
Loading

0 comments on commit f06f1bc

Please sign in to comment.