Skip to content

Commit

Permalink
Merge pull request #232 from jokemanfire/dev
Browse files Browse the repository at this point in the history
Add gen mod for more convenient to use
  • Loading branch information
lifupan authored Oct 10, 2024
2 parents db8a8e9 + 3243468 commit 0210c7a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ Cargo.lock
.idea
*.o
example/protocols/**/*.rs
!example/protocols/**/mod.rs
src/ttrpc.rs
31 changes: 30 additions & 1 deletion compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@

#![allow(dead_code)]

use std::collections::HashMap;
use std::{
collections::{HashMap, HashSet},
fs,
io::BufRead,
};

use crate::Customize;
use protobuf::{
Expand Down Expand Up @@ -723,6 +727,31 @@ pub fn gen_and_write(
) -> io::Result<()> {
let results = gen(file_descriptors, files_to_generate, customize);

if customize.gen_mod {
let file_path = out_dir.join("mod.rs");
let mut set = HashSet::new();
//if mod file exists
if let Ok(file) = File::open(&file_path) {
let reader = io::BufReader::new(file);
reader.lines().for_each(|line| {
let _ = line.map(|r| set.insert(r));
});
}
let mut file_write = fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&file_path)?;
for r in &results {
let prefix_name: Vec<&str> = r.name.split('.').collect();
set.insert(format!("pub mod {};", prefix_name[0]));
}
for item in &set {
writeln!(file_write, "{}", item)?;
}
file_write.flush()?;
}

for r in &results {
let mut file_path = out_dir.to_owned();
file_path.push(&r.name);
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ pub struct Customize {
pub async_client: bool,
/// Indicates whether to generate async code for server.
pub async_server: bool,
/// Gen mod rs in mod.rs
pub gen_mod: bool,
}
10 changes: 7 additions & 3 deletions example/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
//

use std::{
fs::File,
fs::{self, File},
io::{Read, Write},
};
use ttrpc_codegen::{Codegen, Customize, ProtobufCustomize};

fn main() {
fs::create_dir_all("protocols/sync").unwrap();
fs::create_dir_all("protocols/asynchronous").unwrap();

let mut protos = vec![
"protocols/protos/github.com/gogo/protobuf/gogoproto/gogo.proto",
"protocols/protos/github.com/kata-containers/agent/pkg/types/types.proto",
Expand All @@ -18,15 +21,15 @@ fn main() {
"protocols/protos/google/protobuf/empty.proto",
"protocols/protos/oci.proto",
];

let protobuf_customized = ProtobufCustomize::default().gen_mod_rs(false);
let protobuf_customized = ProtobufCustomize::default().gen_mod_rs(true);

Codegen::new()
.out_dir("protocols/sync")
.inputs(&protos)
.include("protocols/protos")
.rust_protobuf()
.customize(Customize {
gen_mod: true, //This could be add while the new ttrpc compiler support it.
..Default::default()
})
.rust_protobuf_customize(protobuf_customized.clone())
Expand All @@ -42,6 +45,7 @@ fn main() {
.include("protocols/protos")
.rust_protobuf()
.customize(Customize {
gen_mod: true, //This could be add while the new ttrpc compiler support it.
async_all: true,
..Default::default()
})
Expand Down
15 changes: 0 additions & 15 deletions example/protocols/asynchronous/mod.rs

This file was deleted.

13 changes: 0 additions & 13 deletions example/protocols/sync/mod.rs

This file was deleted.

0 comments on commit 0210c7a

Please sign in to comment.