diff --git a/Cargo.toml b/Cargo.toml index c326ec9..ef30dfb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,6 @@ glcp = "0.1.2" serde = "1.0.158" serde_derive = "1.0.158" toml = "0.7.3" + +[build-dependencies] +dirs = "5.0.0" diff --git a/build.rs b/build.rs index e8eafda..469d392 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,9 @@ use std::env::var; -use dir::home_dir; +use std::path::PathBuf; +use std::include_str; +use std::fs; +use std::io::Write; +use dirs::home_dir; fn generate_config() { let mut home = home_dir().unwrap(); @@ -13,9 +17,18 @@ fn generate_config() { default_config_path } }; + if !config_path.exists() { + fs::create_dir_all(&config_path.parent().unwrap_or(&home_dir().expect("[error] Cannot find home directory."))) + .expect("[error] Could not create config directory."); + let mut new_config_file = + fs::File::create(&config_path).expect("[error] Could not create config file."); + new_config_file + .write_all(include_str!("src/resources/default.toml").as_bytes()) + .expect("[error] Could not write to config file."); + }; } fn main() { println!("cargo:rerun-if-env-changed=SHRTCUT_CONFIG_FILE"); - let _ = Configs::new(); + generate_config(); }