Skip to content
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

Add gen mod for more convenient to use #232

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]));
Tim-Zhang marked this conversation as resolved.
Show resolved Hide resolved
}
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.

Loading