creator_tools/commands/common/
gen_minimal_project.rs

1use crate::error::Result;
2
3use std::path::Path;
4use std::{
5    fs::{create_dir, File},
6    io::Write,
7};
8
9const CARGO_TOML_VALUE: &str = r#"
10[package]
11name = "example"
12edition = "2018"
13version = "0.1.0"
14
15[lib]
16name = "example"
17crate-type = ["lib", "cdylib"]
18path = "src/lib.rs"
19
20[dependencies]
21creator = { git = "https://github.com/creator-rs/creator" }
22"#;
23
24const LIB_RS_VALUE: &str = "#[creator::creator_main] pub fn main() {}";
25
26const MAIN_RS_VALUE: &str = "fn main(){example::main();}";
27
28/// Generates a new minimal project in given path.
29pub fn gen_minimal_project(out_dir: &Path) -> Result<String> {
30    // Create Cargo.toml file
31    let file_path = out_dir.join("Cargo.toml");
32    let mut file = File::create(file_path)?;
33    file.write_all(CARGO_TOML_VALUE.as_bytes())?;
34    // Create src folder
35    let src_path = out_dir.join("src/");
36    create_dir(src_path.clone())?;
37    // Create lib.rs
38    let lib_rs_path = src_path.join("lib.rs");
39    let mut lib_rs = File::create(lib_rs_path)?;
40    lib_rs.write_all(LIB_RS_VALUE.as_bytes())?;
41    // Create main.rs
42    let main_rs_path = src_path.join("main.rs");
43    let mut main_rs = File::create(main_rs_path)?;
44    main_rs.write_all(MAIN_RS_VALUE.as_bytes())?;
45    Ok("example".to_owned())
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_command_run() {
54        let dir = tempfile::tempdir().unwrap();
55        gen_minimal_project(dir.path()).unwrap();
56    }
57}