Rust build script to copy a file to target directory

Viewed 2247

For my project, I would like to copy the file config.ron that is in the root of my project to the target directory when the project is built. I know that you can use include_str! to add the content of the file to the program at compile time, but I would like the file to stay in the root of the target folder so that it can be edited without the need to recompile the program.

I've currently been trying out a build script to accomplish this but I am have no luck.

use std::process::Command;
use std::env;

fn main() {
    let profile = std::env::var("PROFILE").unwrap();
    match profile.as_str() {
        "debug" => {
            Command::new("cmd")
                .args(&["copy", "/y"])
                .arg(&format!(r#"{}\config.ron"#, env::var("CARGO_MANIFEST_DIR").unwrap()))
                .arg(&format!(r#"{}\target\debug"#, env::var("CARGO_MANIFEST_DIR").unwrap()))
                .status()
                .expect("Copy failed to execute.");
            ()
        },
        "release" => {
            Command::new("cmd")
                .args(&["copy", "/y"])
                .arg(&format!(r#"{}\config.ron"#, env::var("CARGO_MANIFEST_DIR").unwrap()))
                .arg(&format!(r#"{}\target\release"#, env::var("CARGO_MANIFEST_DIR").unwrap()))
                .status()
                .expect("Copy failed to execute.");
            ()
        },
        _ => (),
    }
}

What would be the correct way to get this file copied to the target directory using a build script?

1 Answers
Related