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

Customising generated file name #173

Open
nevi-me opened this issue Mar 24, 2019 · 4 comments
Open

Customising generated file name #173

nevi-me opened this issue Mar 24, 2019 · 4 comments

Comments

@nevi-me
Copy link

nevi-me commented Mar 24, 2019

I'm using tower-grpc with prost, and I have proto files whose package names I can't change.

Where the package name is package foo.bar, I end up with foo.bar.rs where it would be better to have foo_bar.rs.

The . instead of _ forces me to include!() the file in my mods, which breaks autocomplete in VS Code.

Is there an option that I'm missing? I've already looked into tower-grpc-build, and it doesn't have any options for this as it inherits prost's Config.

I use OUT_DIR=src/gen/, so I ideally would like to exclude the need to use the include!() macro in my module imports. Thanks

@harscoet
Copy link

harscoet commented Jul 7, 2019

I use the following code in my build.rs file, it's really ugly but it works

use std::fs;
use std::io;
use std::path::Path;

fn rename_prost_generated_filenames(dir: &Path) -> io::Result<()> {
    if dir.is_dir() {
        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.is_file() {
                let file_stem_renamed = &path
                    .file_stem()
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .replace(".", "_");

                fs::rename(&path, dir.join(format!("{}.rs", file_stem_renamed)))?;
            }
        }
    }

    Ok(())
}

fn main() {
    let src = Path::new("src");
    let mut config = prost_build::Config::default();

    config.out_dir(src);
    config.compile_protos(&["protobuf/myproto.proto"], &["protobuf"]).unwrap();

    rename_prost_generated_filenames(&src).unwrap();
}

@nevi-me
Copy link
Author

nevi-me commented Jul 7, 2019

Thanks @harscoet, it's better than nothing. I'll try it out

@boan-anbo
Copy link

Thanks

I use the following code in my build.rs file, it's really ugly but it works

use std::fs;
use std::io;
use std::path::Path;

fn rename_prost_generated_filenames(dir: &Path) -> io::Result<()> {
    if dir.is_dir() {
        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.is_file() {
                let file_stem_renamed = &path
                    .file_stem()
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .replace(".", "_");

                fs::rename(&path, dir.join(format!("{}.rs", file_stem_renamed)))?;
            }
        }
    }

    Ok(())
}

fn main() {
    let src = Path::new("src");
    let mut config = prost_build::Config::default();

    config.out_dir(src);
    config.compile_protos(&["protobuf/myproto.proto"], &["protobuf"]).unwrap();

    rename_prost_generated_filenames(&src).unwrap();
}

Thanks! It'd be much better to have configuration option to rename the output names. Otherwise it's painful for versioning package names with things like ..

@boan-anbo
Copy link

I use the following code in my build.rs file, it's really ugly but it works

use std::fs;
use std::io;
use std::path::Path;

fn rename_prost_generated_filenames(dir: &Path) -> io::Result<()> {
    if dir.is_dir() {
        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.is_file() {
                let file_stem_renamed = &path
                    .file_stem()
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .replace(".", "_");

                fs::rename(&path, dir.join(format!("{}.rs", file_stem_renamed)))?;
            }
        }
    }

    Ok(())
}

fn main() {
    let src = Path::new("src");
    let mut config = prost_build::Config::default();

    config.out_dir(src);
    config.compile_protos(&["protobuf/myproto.proto"], &["protobuf"]).unwrap();

    rename_prost_generated_filenames(&src).unwrap();
}

To those who will use this super helpful solution: don't forget to modify the code to keep the extension as is, otherwise extensions like .bin will be renamed .rs

E.g.

let ext = path.extension().unwrap().to_str().unwrap();
rename(&path, dir.join(format!("{}.{}", file_stem_renamed, ext)))?;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants